blob: 8321b1492945ea40fce52288b8fcc2ffcae3d6f9 [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
Guido van Rossum21d335e1993-10-15 13:01:11 +000030
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000031/* Magic word to reject .pyc files generated by other Python versions.
32 It should change for each incompatible change to the bytecode.
33
34 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000035 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000036 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000037
Guido van Rossum45aecf42006-03-15 04:58:47 +000038 The magic numbers must be spaced apart at least 2 values, as the
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000039 -U interpeter flag will cause MAGIC+1 being used. They have been
40 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000041
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000042 There were a variety of old schemes for setting the magic number.
43 The current working scheme is to increment the previous value by
44 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000045
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000046 Known values:
47 Python 1.5: 20121
48 Python 1.5.1: 20121
49 Python 1.5.2: 20121
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000050 Python 1.6: 50428
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000051 Python 2.0: 50823
52 Python 2.0.1: 50823
53 Python 2.1: 60202
54 Python 2.1.1: 60202
55 Python 2.1.2: 60202
56 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000057 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000058 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000059 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000060 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000061 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000062 Python 2.4b1: 62061
Michael W. Hudsondf888462005-06-03 14:41:55 +000063 Python 2.5a0: 62071
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000064 Python 2.5a0: 62081 (ast-branch)
Guido van Rossumc2e20742006-02-27 22:32:47 +000065 Python 2.5a0: 62091 (with)
Guido van Rossumf6694362006-03-10 02:28:35 +000066 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
68 Python 2.5b3: 62111 (fix wrong code: x += yield)
69 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
70 storing constants that should have been removed)
Thomas Wouters89f507f2006-12-13 04:49:30 +000071 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
Christian Heimes99170a52007-12-19 02:07:34 +000072 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000073 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
Guido van Rossum45aecf42006-03-15 04:58:47 +000074 Python 3000: 3000
Brett Cannone2e23ef2006-08-25 05:05:30 +000075 3010 (removed UNARY_CONVERT)
Guido van Rossum86e58e22006-08-28 15:27:34 +000076 3020 (added BUILD_SET)
Guido van Rossum4f72a782006-10-27 23:31:49 +000077 3030 (added keyword-only parameters)
Guido van Rossum3203bdc2006-12-28 21:03:31 +000078 3040 (added signature annotations)
Guido van Rossum452bf512007-02-09 05:32:43 +000079 3050 (print becomes a function)
Guido van Rossum52cc1d82007-03-18 15:41:51 +000080 3060 (PEP 3115 metaclass syntax)
Benjamin Peterson0f6cae02009-12-10 02:09:08 +000081 3061 (string literals become unicode)
82 3071 (PEP 3109 raise changes)
83 3081 (PEP 3137 make __file__ and __name__ unicode)
84 3091 (kill str8 interning)
85 3101 (merge from 2.6a0, see 62151)
86 3103 (__file__ points to source file)
87 Python 3.0a4: 3111 (WITH_CLEANUP optimization).
88 Python 3.0a5: 3131 (lexical exception stacking, including POP_EXCEPT)
89 Python 3.1a0: 3141 (optimize list, set and dict comprehensions:
Antoine Pitrouf289ae62008-12-18 11:06:25 +000090 change LIST_APPEND and SET_ADD, add MAP_ADD)
Benjamin Peterson0f6cae02009-12-10 02:09:08 +000091 Python 3.1a0: 3151 (optimize conditional branches:
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +000092 introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
Benjamin Peterson876b2f22009-06-28 03:18:59 +000093 Python 3.2a0: 3160 (add SETUP_WITH)
Tim Peters36515e22001-11-18 04:06:29 +000094*/
Guido van Rossum3ddee711991-12-16 13:06:34 +000095
Benjamin Peterson876b2f22009-06-28 03:18:59 +000096#define MAGIC (3160 | ((long)'\r'<<16) | ((long)'\n'<<24))
Benjamin Peterson0f6cae02009-12-10 02:09:08 +000097/* Magic word as global */
Guido van Rossum96774c12000-05-01 20:19:08 +000098static long pyc_magic = MAGIC;
99
Guido van Rossum25ce5661997-08-02 03:10:38 +0000100/* See _PyImport_FixupExtension() below */
101static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102
Guido van Rossum771c6c81997-10-31 18:37:24 +0000103/* This table is defined in config.c: */
104extern struct _inittab _PyImport_Inittab[];
105
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000106/* Method from Parser/tokenizer.c */
Guido van Rossum40d20bc2007-10-22 00:09:51 +0000107extern char * PyTokenizer_FindEncoding(int);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000108
Guido van Rossum771c6c81997-10-31 18:37:24 +0000109struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +0000110
Guido van Rossumed1170e1999-12-20 21:23:41 +0000111/* these tables define the module suffixes that Python recognizes */
112struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000113
Guido van Rossumed1170e1999-12-20 21:23:41 +0000114static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +0000115 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000116#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +0000117 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000118#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000119 {".pyc", "rb", PY_COMPILED},
120 {0, 0}
121};
122
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000123
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000124/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125
126void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000127_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000129 const struct filedescr *scan;
130 struct filedescr *filetab;
131 int countD = 0;
132 int countS = 0;
133
134 /* prepare _PyImport_Filetab: copy entries from
135 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
136 */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000137#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000138 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
139 ++countD;
Guido van Rossum04110fb2007-08-24 16:32:05 +0000140#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000141 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
142 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000143 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000144 if (filetab == NULL)
145 Py_FatalError("Can't initialize import file table.");
Guido van Rossum04110fb2007-08-24 16:32:05 +0000146#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000147 memcpy(filetab, _PyImport_DynLoadFiletab,
148 countD * sizeof(struct filedescr));
Guido van Rossum04110fb2007-08-24 16:32:05 +0000149#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000150 memcpy(filetab + countD, _PyImport_StandardFiletab,
151 countS * sizeof(struct filedescr));
152 filetab[countD + countS].suffix = NULL;
153
154 _PyImport_Filetab = filetab;
155
Guido van Rossum0824f631997-03-11 18:37:35 +0000156 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000157 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
158 for (; filetab->suffix != NULL; filetab++) {
159 if (strcmp(filetab->suffix, ".pyc") == 0)
160 filetab->suffix = ".pyo";
Guido van Rossum0824f631997-03-11 18:37:35 +0000161 }
162 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163}
164
Guido van Rossum25ce5661997-08-02 03:10:38 +0000165void
Just van Rossum52e14d62002-12-30 22:08:05 +0000166_PyImportHooks_Init(void)
167{
168 PyObject *v, *path_hooks = NULL, *zimpimport;
169 int err = 0;
170
171 /* adding sys.path_hooks and sys.path_importer_cache, setting up
172 zipimport */
Christian Heimes9cd17752007-11-18 19:35:23 +0000173 if (PyType_Ready(&PyNullImporter_Type) < 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000174 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000175
176 if (Py_VerboseFlag)
177 PySys_WriteStderr("# installing zipimport hook\n");
178
179 v = PyList_New(0);
180 if (v == NULL)
181 goto error;
182 err = PySys_SetObject("meta_path", v);
183 Py_DECREF(v);
184 if (err)
185 goto error;
186 v = PyDict_New();
187 if (v == NULL)
188 goto error;
189 err = PySys_SetObject("path_importer_cache", v);
190 Py_DECREF(v);
191 if (err)
192 goto error;
193 path_hooks = PyList_New(0);
194 if (path_hooks == NULL)
195 goto error;
196 err = PySys_SetObject("path_hooks", path_hooks);
197 if (err) {
198 error:
199 PyErr_Print();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000200 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
201 "path_importer_cache, or NullImporter failed"
202 );
Just van Rossum52e14d62002-12-30 22:08:05 +0000203 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000204
Just van Rossum52e14d62002-12-30 22:08:05 +0000205 zimpimport = PyImport_ImportModule("zipimport");
206 if (zimpimport == NULL) {
207 PyErr_Clear(); /* No zip import module -- okay */
208 if (Py_VerboseFlag)
209 PySys_WriteStderr("# can't import zipimport\n");
210 }
211 else {
212 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
213 "zipimporter");
214 Py_DECREF(zimpimport);
215 if (zipimporter == NULL) {
216 PyErr_Clear(); /* No zipimporter object -- okay */
217 if (Py_VerboseFlag)
218 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000219 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000220 }
221 else {
222 /* sys.path_hooks.append(zipimporter) */
223 err = PyList_Append(path_hooks, zipimporter);
224 Py_DECREF(zipimporter);
225 if (err)
226 goto error;
227 if (Py_VerboseFlag)
228 PySys_WriteStderr(
229 "# installed zipimport hook\n");
230 }
231 }
232 Py_DECREF(path_hooks);
233}
234
235void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000236_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000237{
238 Py_XDECREF(extensions);
239 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000240 PyMem_DEL(_PyImport_Filetab);
241 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242}
243
244
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000245/* Locking primitives to prevent parallel imports of the same module
246 in different threads to return with a partially loaded module.
247 These calls are serialized by the global interpreter lock. */
248
249#ifdef WITH_THREAD
250
Guido van Rossum49b56061998-10-01 20:42:43 +0000251#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000252
Guido van Rossum65d5b571998-12-21 19:32:43 +0000253static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000254static long import_lock_thread = -1;
255static int import_lock_level = 0;
256
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000257void
258_PyImport_AcquireLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000259{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000260 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000261 if (me == -1)
262 return; /* Too bad */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000263 if (import_lock == NULL) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000264 import_lock = PyThread_allocate_lock();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000265 if (import_lock == NULL)
266 return; /* Nothing much we can do. */
267 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000268 if (import_lock_thread == me) {
269 import_lock_level++;
270 return;
271 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000272 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
273 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000274 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000275 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000276 PyEval_RestoreThread(tstate);
277 }
278 import_lock_thread = me;
279 import_lock_level = 1;
280}
281
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000282int
283_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000284{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000285 long me = PyThread_get_thread_ident();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000286 if (me == -1 || import_lock == NULL)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000287 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000288 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000289 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000290 import_lock_level--;
291 if (import_lock_level == 0) {
292 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000293 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000294 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000295 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000296}
297
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000298/* This function used to be called from PyOS_AfterFork to ensure that newly
299 created child processes do not share locks with the parent, but for some
300 reason only on AIX systems. Instead of re-initializing the lock, we now
301 acquire the import lock around fork() calls. */
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000302
303void
304_PyImport_ReInitLock(void)
305{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000306}
307
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000308#endif
309
Tim Peters69232342001-08-30 05:16:13 +0000310static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000311imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000312{
Tim Peters69232342001-08-30 05:16:13 +0000313#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000314 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000315#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000316 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000317#endif
318}
319
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000320static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000321imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000322{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000323#ifdef WITH_THREAD
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000324 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000325#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000326 Py_INCREF(Py_None);
327 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000328}
329
330static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000331imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000332{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000333#ifdef WITH_THREAD
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000334 if (_PyImport_ReleaseLock() < 0) {
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000335 PyErr_SetString(PyExc_RuntimeError,
336 "not holding the import lock");
337 return NULL;
338 }
339#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000340 Py_INCREF(Py_None);
341 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000342}
343
Guido van Rossumd8faa362007-04-27 19:54:29 +0000344static void
345imp_modules_reloading_clear(void)
346{
347 PyInterpreterState *interp = PyThreadState_Get()->interp;
348 if (interp->modules_reloading != NULL)
349 PyDict_Clear(interp->modules_reloading);
350}
351
Guido van Rossum25ce5661997-08-02 03:10:38 +0000352/* Helper for sys */
353
354PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000355PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000356{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000357 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000358 if (interp->modules == NULL)
359 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
360 return interp->modules;
361}
362
Guido van Rossum3f5da241990-12-20 15:06:42 +0000363
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000364/* List of names to clear in sys */
365static char* sys_deletes[] = {
Collin Winter670e6922007-03-21 02:57:17 +0000366 "path", "argv", "ps1", "ps2",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000367 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000368 "path_hooks", "path_importer_cache", "meta_path",
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000369 /* misc stuff */
370 "flags", "float_info",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000371 NULL
372};
373
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000374static char* sys_files[] = {
375 "stdin", "__stdin__",
376 "stdout", "__stdout__",
377 "stderr", "__stderr__",
378 NULL
379};
380
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000381
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000382/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000383
Guido van Rossum3f5da241990-12-20 15:06:42 +0000384void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000385PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000386{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000387 Py_ssize_t pos, ndone;
Guido van Rossum758eec01998-01-19 21:58:26 +0000388 char *name;
389 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000390 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000391 PyObject *modules = interp->modules;
392
393 if (modules == NULL)
394 return; /* Already done */
395
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000396 /* Delete some special variables first. These are common
397 places where user values hide and people complain when their
398 destructors fail. Since the modules containing them are
399 deleted *last* of all, they would come too late in the normal
400 destruction order. Sigh. */
401
Georg Brandl1a3284e2007-12-02 09:40:06 +0000402 value = PyDict_GetItemString(modules, "builtins");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000403 if (value != NULL && PyModule_Check(value)) {
404 dict = PyModule_GetDict(value);
405 if (Py_VerboseFlag)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000406 PySys_WriteStderr("# clear builtins._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000407 PyDict_SetItemString(dict, "_", Py_None);
408 }
409 value = PyDict_GetItemString(modules, "sys");
410 if (value != NULL && PyModule_Check(value)) {
411 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000412 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000413 dict = PyModule_GetDict(value);
414 for (p = sys_deletes; *p != NULL; p++) {
415 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000416 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000417 PyDict_SetItemString(dict, *p, Py_None);
418 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000419 for (p = sys_files; *p != NULL; p+=2) {
420 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000421 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000422 v = PyDict_GetItemString(dict, *(p+1));
423 if (v == NULL)
424 v = Py_None;
425 PyDict_SetItemString(dict, *p, v);
426 }
427 }
428
429 /* First, delete __main__ */
430 value = PyDict_GetItemString(modules, "__main__");
431 if (value != NULL && PyModule_Check(value)) {
432 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000433 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000434 _PyModule_Clear(value);
435 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000436 }
437
Georg Brandl1a3284e2007-12-02 09:40:06 +0000438 /* The special treatment of "builtins" here is because even
Guido van Rossum758eec01998-01-19 21:58:26 +0000439 when it's not referenced as a module, its dictionary is
440 referenced by almost every module's __builtins__. Since
441 deleting a module clears its dictionary (even if there are
Georg Brandl1a3284e2007-12-02 09:40:06 +0000442 references left to it), we need to delete the "builtins"
Guido van Rossum758eec01998-01-19 21:58:26 +0000443 module last. Likewise, we don't delete sys until the very
444 end because it is implicitly referenced (e.g. by print).
445
446 Also note that we 'delete' modules by replacing their entry
447 in the modules dict with None, rather than really deleting
448 them; this avoids a rehash of the modules dictionary and
449 also marks them as "non existent" so they won't be
450 re-imported. */
451
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000452 /* Next, repeatedly delete modules with a reference count of
Georg Brandl1a3284e2007-12-02 09:40:06 +0000453 one (skipping builtins and sys) and delete them */
Guido van Rossum758eec01998-01-19 21:58:26 +0000454 do {
455 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000456 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000457 while (PyDict_Next(modules, &pos, &key, &value)) {
458 if (value->ob_refcnt != 1)
459 continue;
Neal Norwitz80e7f272007-08-26 06:45:23 +0000460 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000461 name = _PyUnicode_AsString(key);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000462 if (strcmp(name, "builtins") == 0)
Guido van Rossum758eec01998-01-19 21:58:26 +0000463 continue;
464 if (strcmp(name, "sys") == 0)
465 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000466 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000467 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000468 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000469 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000470 PyDict_SetItem(modules, key, Py_None);
471 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000472 }
473 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000474 } while (ndone > 0);
475
Georg Brandl1a3284e2007-12-02 09:40:06 +0000476 /* Next, delete all modules (still skipping builtins and sys) */
Guido van Rossum758eec01998-01-19 21:58:26 +0000477 pos = 0;
478 while (PyDict_Next(modules, &pos, &key, &value)) {
Neal Norwitz80e7f272007-08-26 06:45:23 +0000479 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000480 name = _PyUnicode_AsString(key);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000481 if (strcmp(name, "builtins") == 0)
Guido van Rossum758eec01998-01-19 21:58:26 +0000482 continue;
483 if (strcmp(name, "sys") == 0)
484 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000485 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000486 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000487 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000488 PyDict_SetItem(modules, key, Py_None);
489 }
490 }
491
Georg Brandl1a3284e2007-12-02 09:40:06 +0000492 /* Next, delete sys and builtins (in that order) */
Guido van Rossum758eec01998-01-19 21:58:26 +0000493 value = PyDict_GetItemString(modules, "sys");
494 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000495 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000496 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000497 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000498 PyDict_SetItemString(modules, "sys", Py_None);
499 }
Georg Brandl1a3284e2007-12-02 09:40:06 +0000500 value = PyDict_GetItemString(modules, "builtins");
Guido van Rossum758eec01998-01-19 21:58:26 +0000501 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000502 if (Py_VerboseFlag)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000503 PySys_WriteStderr("# cleanup builtins\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000504 _PyModule_Clear(value);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000505 PyDict_SetItemString(modules, "builtins", Py_None);
Guido van Rossum758eec01998-01-19 21:58:26 +0000506 }
507
508 /* Finally, clear and delete the modules directory */
509 PyDict_Clear(modules);
510 interp->modules = NULL;
511 Py_DECREF(modules);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000512 Py_CLEAR(interp->modules_reloading);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000513}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000514
515
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000516/* Helper for pythonrun.c -- return magic number */
517
518long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000519PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000520{
Guido van Rossum96774c12000-05-01 20:19:08 +0000521 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000522}
523
524
Guido van Rossum25ce5661997-08-02 03:10:38 +0000525/* Magic for extension modules (built-in as well as dynamically
526 loaded). To prevent initializing an extension module more than
527 once, we keep a static dictionary 'extensions' keyed by module name
528 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000529 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000530 dictionary is stored by calling _PyImport_FixupExtension()
531 immediately after the module initialization function succeeds. A
532 copy can be retrieved from there by calling
Martin v. Löwis1a214512008-06-11 05:26:20 +0000533 _PyImport_FindExtension().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000534
Martin v. Löwis1a214512008-06-11 05:26:20 +0000535 Modules which do support multiple multiple initialization set
536 their m_size field to a non-negative number (indicating the size
537 of the module-specific state). They are still recorded in the
538 extensions dictionary, to avoid loading shared libraries twice.
539*/
540
541int
542_PyImport_FixupExtension(PyObject *mod, char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000543{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000544 PyObject *modules, *dict;
545 struct PyModuleDef *def;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000546 if (extensions == NULL) {
547 extensions = PyDict_New();
548 if (extensions == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000549 return -1;
550 }
551 if (mod == NULL || !PyModule_Check(mod)) {
552 PyErr_BadInternalCall();
553 return -1;
554 }
555 def = PyModule_GetDef(mod);
556 if (!def) {
557 PyErr_BadInternalCall();
558 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000559 }
560 modules = PyImport_GetModuleDict();
Martin v. Löwis1a214512008-06-11 05:26:20 +0000561 if (PyDict_SetItemString(modules, name, mod) < 0)
562 return -1;
563 if (_PyState_AddModule(mod, def) < 0) {
564 PyDict_DelItemString(modules, name);
565 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000566 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000567 if (def->m_size == -1) {
568 if (def->m_base.m_copy) {
569 /* Somebody already imported the module,
570 likely under a different name.
571 XXX this should really not happen. */
572 Py_DECREF(def->m_base.m_copy);
573 def->m_base.m_copy = NULL;
574 }
575 dict = PyModule_GetDict(mod);
576 if (dict == NULL)
577 return -1;
578 def->m_base.m_copy = PyDict_Copy(dict);
579 if (def->m_base.m_copy == NULL)
580 return -1;
581 }
582 PyDict_SetItemString(extensions, filename, (PyObject*)def);
583 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000584}
585
586PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000587_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000588{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000589 PyObject *mod, *mdict;
590 PyModuleDef* def;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000591 if (extensions == NULL)
592 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000593 def = (PyModuleDef*)PyDict_GetItemString(extensions, filename);
594 if (def == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000595 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000596 if (def->m_size == -1) {
597 /* Module does not support repeated initialization */
598 if (def->m_base.m_copy == NULL)
599 return NULL;
600 mod = PyImport_AddModule(name);
601 if (mod == NULL)
602 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000603 mdict = PyModule_GetDict(mod);
604 if (mdict == NULL)
605 return NULL;
606 if (PyDict_Update(mdict, def->m_base.m_copy))
607 return NULL;
608 }
609 else {
610 if (def->m_base.m_init == NULL)
611 return NULL;
612 mod = def->m_base.m_init();
613 if (mod == NULL)
614 return NULL;
615 PyDict_SetItemString(PyImport_GetModuleDict(), name, mod);
Benjamin Petersonad956532008-09-04 02:28:15 +0000616 Py_DECREF(mod);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000617 }
618 if (_PyState_AddModule(mod, def) < 0) {
619 PyDict_DelItemString(PyImport_GetModuleDict(), name);
620 Py_DECREF(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000621 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000622 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000623 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000624 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Martin v. Löwis1a214512008-06-11 05:26:20 +0000625 name, filename);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000626 return mod;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000627
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628}
629
630
631/* Get the module object corresponding to a module name.
632 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000633 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000634 Because the former action is most common, THIS DOES NOT RETURN A
635 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000636
Guido van Rossum79f25d91997-04-29 20:08:16 +0000637PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000638PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000639{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000640 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000641 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000642
Guido van Rossum25ce5661997-08-02 03:10:38 +0000643 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000645 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000646 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647 if (m == NULL)
648 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000649 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000650 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651 return NULL;
652 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000653 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000654
655 return m;
656}
657
Tim Peters1cd70172004-08-02 03:52:12 +0000658/* Remove name from sys.modules, if it's there. */
659static void
660_RemoveModule(const char *name)
661{
662 PyObject *modules = PyImport_GetModuleDict();
663 if (PyDict_GetItemString(modules, name) == NULL)
664 return;
665 if (PyDict_DelItemString(modules, name) < 0)
666 Py_FatalError("import: deleting existing key in"
667 "sys.modules failed");
668}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000669
Christian Heimes3b06e532008-01-07 20:12:44 +0000670static PyObject * get_sourcefile(const char *file);
671
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000672/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000673 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
674 * removed from sys.modules, to avoid leaving damaged module objects
675 * in sys.modules. The caller may wish to restore the original
676 * module object (if any) in this case; PyImport_ReloadModule is an
677 * example.
678 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000679PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000680PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000681{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000682 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
683}
684
685PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000686PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000687{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000688 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000689 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000690
Guido van Rossum79f25d91997-04-29 20:08:16 +0000691 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000692 if (m == NULL)
693 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000694 /* If the module is being reloaded, we get the old module back
695 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000696 d = PyModule_GetDict(m);
697 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
698 if (PyDict_SetItemString(d, "__builtins__",
699 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000700 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000701 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000702 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000703 v = NULL;
704 if (pathname != NULL) {
Christian Heimes3b06e532008-01-07 20:12:44 +0000705 v = get_sourcefile(pathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000706 if (v == NULL)
707 PyErr_Clear();
708 }
709 if (v == NULL) {
710 v = ((PyCodeObject *)co)->co_filename;
711 Py_INCREF(v);
712 }
713 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000715 Py_DECREF(v);
716
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000717 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000718 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000719 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000720 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000721
Guido van Rossum25ce5661997-08-02 03:10:38 +0000722 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000723 PyErr_Format(PyExc_ImportError,
724 "Loaded module %.200s not found in sys.modules",
725 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000726 return NULL;
727 }
728
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000730
731 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000732
733 error:
734 _RemoveModule(name);
735 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000736}
737
738
739/* Given a pathname for a Python source file, fill a buffer with the
740 pathname for the corresponding compiled file. Return the pathname
741 for the compiled file, or NULL if there's no space in the buffer.
742 Doesn't set an exception. */
743
744static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000745make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746{
Tim Petersc1731372001-08-04 08:12:36 +0000747 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000748 if (len+2 > buflen)
749 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000750
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000751#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000752 /* Treat .pyw as if it were .py. The case of ".pyw" must match
753 that used in _PyImport_StandardFiletab. */
754 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
755 --len; /* pretend 'w' isn't there */
756#endif
757 memcpy(buf, pathname, len);
758 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
759 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000760
761 return buf;
762}
763
764
765/* Given a pathname for a Python source file, its time of last
766 modification, and a pathname for a compiled file, check whether the
767 compiled file represents the same version of the source. If so,
768 return a FILE pointer for the compiled file, positioned just after
769 the header; if not, return NULL.
770 Doesn't set an exception. */
771
772static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000773check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000774{
775 FILE *fp;
776 long magic;
777 long pyc_mtime;
778
779 fp = fopen(cpathname, "rb");
780 if (fp == NULL)
781 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000782 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000783 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000784 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000785 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000786 fclose(fp);
787 return NULL;
788 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000789 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000790 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000791 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000792 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000793 fclose(fp);
794 return NULL;
795 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000796 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000797 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000798 return fp;
799}
800
801
802/* Read a code object from a file and check it for validity */
803
Guido van Rossum79f25d91997-04-29 20:08:16 +0000804static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000805read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000806{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000807 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000808
Tim Petersd9b9ac82001-01-28 00:27:39 +0000809 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000810 if (co == NULL)
811 return NULL;
812 if (!PyCode_Check(co)) {
813 PyErr_Format(PyExc_ImportError,
814 "Non-code object in %.200s", cpathname);
815 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000816 return NULL;
817 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000818 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000819}
820
821
822/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000823 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000824
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000826load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000827{
828 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000829 PyCodeObject *co;
830 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000831
Guido van Rossum79f25d91997-04-29 20:08:16 +0000832 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000833 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000834 PyErr_Format(PyExc_ImportError,
835 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000836 return NULL;
837 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000838 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000839 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000840 if (co == NULL)
841 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000843 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000844 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000845 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000847
848 return m;
849}
850
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000851/* Parse a source file and return the corresponding code object */
852
Guido van Rossum79f25d91997-04-29 20:08:16 +0000853static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000855{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 PyCodeObject *co = NULL;
857 mod_ty mod;
Christian Heimes4d6ec852008-03-26 22:34:47 +0000858 PyCompilerFlags flags;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000859 PyArena *arena = PyArena_New();
860 if (arena == NULL)
861 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000862
Christian Heimesb1b3efc2008-03-26 23:24:27 +0000863 flags.cf_flags = 0;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000864 mod = PyParser_ASTFromFile(fp, pathname, NULL,
Christian Heimes4d6ec852008-03-26 22:34:47 +0000865 Py_file_input, 0, 0, &flags,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000866 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000868 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000870 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000871 return co;
872}
873
874
Guido van Rossum55a83382000-09-20 20:31:38 +0000875/* Helper to open a bytecode file for writing in exclusive mode */
876
877static FILE *
Christian Heimes05e8be12008-02-23 18:30:17 +0000878open_exclusive(char *filename, mode_t mode)
Guido van Rossum55a83382000-09-20 20:31:38 +0000879{
880#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
881 /* Use O_EXCL to avoid a race condition when another process tries to
882 write the same file. When that happens, our open() call fails,
883 which is just fine (since it's only a cache).
884 XXX If the file exists and is writable but the directory is not
885 writable, the file will never be written. Oh well.
886 */
887 int fd;
888 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000889 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
890#ifdef O_BINARY
891 |O_BINARY /* necessary for Windows */
892#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000893#ifdef __VMS
Christian Heimes05e8be12008-02-23 18:30:17 +0000894 , mode, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000895#else
Christian Heimes05e8be12008-02-23 18:30:17 +0000896 , mode
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000897#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000898 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000899 if (fd < 0)
900 return NULL;
901 return fdopen(fd, "wb");
902#else
903 /* Best we can do -- on Windows this can't happen anyway */
904 return fopen(filename, "wb");
905#endif
906}
907
908
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000909/* Write a compiled module to a file, placing the time of last
910 modification of its source into the header.
911 Errors are ignored, if a write error occurs an attempt is made to
912 remove the file. */
913
914static void
Christian Heimes05e8be12008-02-23 18:30:17 +0000915write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000916{
917 FILE *fp;
Christian Heimes05e8be12008-02-23 18:30:17 +0000918 time_t mtime = srcstat->st_mtime;
Alexandre Vassalotti9d58e3e2009-07-17 10:55:50 +0000919#ifdef MS_WINDOWS /* since Windows uses different permissions */
920 mode_t mode = srcstat->st_mode & ~S_IEXEC;
921#else
922 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;
923#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000924
Christian Heimes05e8be12008-02-23 18:30:17 +0000925 fp = open_exclusive(cpathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000926 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000927 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000928 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000929 "# can't create %s\n", cpathname);
930 return;
931 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000932 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000933 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000934 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
935 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000936 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000937 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000938 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000939 /* Don't keep partial file */
940 fclose(fp);
941 (void) unlink(cpathname);
942 return;
943 }
944 /* Now write the true mtime */
945 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000946 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000947 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000948 fflush(fp);
949 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000951 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000952}
953
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000954static void
955update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
956{
957 PyObject *constants, *tmp;
958 Py_ssize_t i, n;
959
960 if (PyUnicode_Compare(co->co_filename, oldname))
961 return;
962
963 tmp = co->co_filename;
964 co->co_filename = newname;
965 Py_INCREF(co->co_filename);
966 Py_DECREF(tmp);
967
968 constants = co->co_consts;
969 n = PyTuple_GET_SIZE(constants);
970 for (i = 0; i < n; i++) {
971 tmp = PyTuple_GET_ITEM(constants, i);
972 if (PyCode_Check(tmp))
973 update_code_filenames((PyCodeObject *)tmp,
974 oldname, newname);
975 }
976}
977
978static int
979update_compiled_module(PyCodeObject *co, char *pathname)
980{
981 PyObject *oldname, *newname;
982
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000983 newname = PyUnicode_DecodeFSDefault(pathname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000984 if (newname == NULL)
985 return -1;
986
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000987 if (!PyUnicode_Compare(co->co_filename, newname)) {
988 Py_DECREF(newname);
989 return 0;
990 }
991
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000992 oldname = co->co_filename;
993 Py_INCREF(oldname);
994 update_code_filenames(co, oldname, newname);
995 Py_DECREF(oldname);
996 Py_DECREF(newname);
997 return 1;
998}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000999
1000/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001001 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
1002 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001003
Guido van Rossum79f25d91997-04-29 20:08:16 +00001004static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001005load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001006{
Christian Heimes05e8be12008-02-23 18:30:17 +00001007 struct stat st;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001008 FILE *fpc;
1009 char buf[MAXPATHLEN+1];
1010 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011 PyCodeObject *co;
1012 PyObject *m;
Christian Heimes05e8be12008-02-23 18:30:17 +00001013
1014 if (fstat(fileno(fp), &st) != 0) {
Neal Norwitz708e51a2005-10-03 04:48:15 +00001015 PyErr_Format(PyExc_RuntimeError,
Christian Heimes05e8be12008-02-23 18:30:17 +00001016 "unable to get file status from '%s'",
Neal Norwitz708e51a2005-10-03 04:48:15 +00001017 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +00001018 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +00001019 }
Fred Drake4c82b232000-06-30 16:18:57 +00001020#if SIZEOF_TIME_T > 4
1021 /* Python's .pyc timestamp handling presumes that the timestamp fits
1022 in 4 bytes. This will be fine until sometime in the year 2038,
1023 when a 4-byte signed time_t will overflow.
1024 */
Christian Heimes05e8be12008-02-23 18:30:17 +00001025 if (st.st_mtime >> 32) {
Fred Drake4c82b232000-06-30 16:18:57 +00001026 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +00001027 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +00001028 return NULL;
1029 }
1030#endif
Tim Peters36515e22001-11-18 04:06:29 +00001031 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +00001032 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001033 if (cpathname != NULL &&
Christian Heimes05e8be12008-02-23 18:30:17 +00001034 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001035 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001036 fclose(fpc);
1037 if (co == NULL)
1038 return NULL;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001039 if (update_compiled_module(co, pathname) < 0)
1040 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001041 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001042 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001043 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001044 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001045 }
1046 else {
1047 co = parse_source_module(pathname, fp);
1048 if (co == NULL)
1049 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001050 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001051 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001052 name, pathname);
Christian Heimes790c8232008-01-07 21:14:23 +00001053 if (cpathname) {
1054 PyObject *ro = PySys_GetObject("dont_write_bytecode");
1055 if (ro == NULL || !PyObject_IsTrue(ro))
Christian Heimes05e8be12008-02-23 18:30:17 +00001056 write_compiled_module(co, cpathname, &st);
Christian Heimes790c8232008-01-07 21:14:23 +00001057 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001058 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001059 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001060 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001061
1062 return m;
1063}
1064
Christian Heimes3b06e532008-01-07 20:12:44 +00001065/* Get source file -> unicode or None
1066 * Returns the path to the py file if available, else the given path
1067 */
1068static PyObject *
1069get_sourcefile(const char *file)
1070{
1071 char py[MAXPATHLEN + 1];
1072 Py_ssize_t len;
1073 PyObject *u;
1074 struct stat statbuf;
1075
1076 if (!file || !*file) {
1077 Py_RETURN_NONE;
1078 }
1079
1080 len = strlen(file);
Christian Heimes3540b502008-01-11 07:03:05 +00001081 /* match '*.py?' */
1082 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {
Christian Heimes3b06e532008-01-07 20:12:44 +00001083 return PyUnicode_DecodeFSDefault(file);
1084 }
1085
1086 strncpy(py, file, len-1);
Martin v. Löwis5021ebc2008-03-22 22:07:13 +00001087 py[len-1] = '\0';
Christian Heimes3b06e532008-01-07 20:12:44 +00001088 if (stat(py, &statbuf) == 0 &&
1089 S_ISREG(statbuf.st_mode)) {
1090 u = PyUnicode_DecodeFSDefault(py);
1091 }
1092 else {
1093 u = PyUnicode_DecodeFSDefault(file);
1094 }
1095 return u;
1096}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001097
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001098/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001099static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
1100static struct filedescr *find_module(char *, char *, PyObject *,
1101 char *, size_t, FILE **, PyObject **);
Christian Heimes3b06e532008-01-07 20:12:44 +00001102static struct _frozen * find_frozen(char *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001103
1104/* Load a package and return its module object WITH INCREMENTED
1105 REFERENCE COUNT */
1106
1107static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001108load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001109{
Tim Peters1cd70172004-08-02 03:52:12 +00001110 PyObject *m, *d;
1111 PyObject *file = NULL;
1112 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001113 int err;
1114 char buf[MAXPATHLEN+1];
1115 FILE *fp = NULL;
1116 struct filedescr *fdp;
1117
1118 m = PyImport_AddModule(name);
1119 if (m == NULL)
1120 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001121 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001122 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001123 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001124 d = PyModule_GetDict(m);
Christian Heimes3b06e532008-01-07 20:12:44 +00001125 file = get_sourcefile(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001126 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +00001127 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001128 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +00001129 if (path == NULL)
1130 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001131 err = PyDict_SetItemString(d, "__file__", file);
1132 if (err == 0)
1133 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +00001134 if (err != 0)
1135 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001136 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001137 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001138 if (fdp == NULL) {
1139 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1140 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +00001141 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001142 }
1143 else
1144 m = NULL;
1145 goto cleanup;
1146 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001147 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001148 if (fp != NULL)
1149 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001150 goto cleanup;
1151
1152 error:
1153 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001154 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001155 Py_XDECREF(path);
1156 Py_XDECREF(file);
1157 return m;
1158}
1159
1160
1161/* Helper to test for built-in module */
1162
1163static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001164is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001165{
1166 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001167 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1168 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1169 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001170 return -1;
1171 else
1172 return 1;
1173 }
1174 }
1175 return 0;
1176}
1177
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001178
Just van Rossum52e14d62002-12-30 22:08:05 +00001179/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1180 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001181 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001182 that can handle the path item. Return None if no hook could;
1183 this tells our caller it should fall back to the builtin
1184 import mechanism. Cache the result in path_importer_cache.
1185 Returns a borrowed reference. */
1186
1187static PyObject *
1188get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1189 PyObject *p)
1190{
1191 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001192 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001193
1194 /* These conditions are the caller's responsibility: */
1195 assert(PyList_Check(path_hooks));
1196 assert(PyDict_Check(path_importer_cache));
1197
1198 nhooks = PyList_Size(path_hooks);
1199 if (nhooks < 0)
1200 return NULL; /* Shouldn't happen */
1201
1202 importer = PyDict_GetItem(path_importer_cache, p);
1203 if (importer != NULL)
1204 return importer;
1205
1206 /* set path_importer_cache[p] to None to avoid recursion */
1207 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1208 return NULL;
1209
1210 for (j = 0; j < nhooks; j++) {
1211 PyObject *hook = PyList_GetItem(path_hooks, j);
1212 if (hook == NULL)
1213 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001214 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001215 if (importer != NULL)
1216 break;
1217
1218 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1219 return NULL;
1220 }
1221 PyErr_Clear();
1222 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001223 if (importer == NULL) {
1224 importer = PyObject_CallFunctionObjArgs(
Christian Heimes9cd17752007-11-18 19:35:23 +00001225 (PyObject *)&PyNullImporter_Type, p, NULL
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001226 );
1227 if (importer == NULL) {
1228 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1229 PyErr_Clear();
1230 return Py_None;
1231 }
1232 }
1233 }
1234 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001235 int err = PyDict_SetItem(path_importer_cache, p, importer);
1236 Py_DECREF(importer);
1237 if (err != 0)
1238 return NULL;
1239 }
1240 return importer;
1241}
1242
Christian Heimes9cd17752007-11-18 19:35:23 +00001243PyAPI_FUNC(PyObject *)
1244PyImport_GetImporter(PyObject *path) {
1245 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
1246
1247 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1248 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1249 importer = get_path_importer(path_importer_cache,
1250 path_hooks, path);
1251 }
1252 }
1253 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1254 return importer;
1255}
1256
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001257/* Search the path (default sys.path) for a module. Return the
1258 corresponding filedescr struct, and (via return arguments) the
1259 pathname and an open file. Return NULL if the module is not found. */
1260
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001261#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001262extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001263 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001264#endif
1265
Martin v. Löwis18e16552006-02-15 17:27:45 +00001266static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001267static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001268static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001269
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001270static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001271find_module(char *fullname, char *subname, PyObject *path, char *buf,
1272 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001273{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001274 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001275 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001276 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001277 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001278 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001279 PyObject *path_hooks, *path_importer_cache;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001280 struct stat statbuf;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001281 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1282 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1283 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001284 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001285#if defined(PYOS_OS2)
1286 size_t saved_len;
1287 size_t saved_namelen;
1288 char *saved_buf = NULL;
1289#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001290 if (p_loader != NULL)
1291 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001292
Just van Rossum52e14d62002-12-30 22:08:05 +00001293 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001294 PyErr_SetString(PyExc_OverflowError,
1295 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001296 return NULL;
1297 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001298 strcpy(name, subname);
1299
1300 /* sys.meta_path import hook */
1301 if (p_loader != NULL) {
1302 PyObject *meta_path;
1303
1304 meta_path = PySys_GetObject("meta_path");
1305 if (meta_path == NULL || !PyList_Check(meta_path)) {
1306 PyErr_SetString(PyExc_ImportError,
1307 "sys.meta_path must be a list of "
1308 "import hooks");
1309 return NULL;
1310 }
1311 Py_INCREF(meta_path); /* zap guard */
1312 npath = PyList_Size(meta_path);
1313 for (i = 0; i < npath; i++) {
1314 PyObject *loader;
1315 PyObject *hook = PyList_GetItem(meta_path, i);
1316 loader = PyObject_CallMethod(hook, "find_module",
1317 "sO", fullname,
1318 path != NULL ?
1319 path : Py_None);
1320 if (loader == NULL) {
1321 Py_DECREF(meta_path);
1322 return NULL; /* true error */
1323 }
1324 if (loader != Py_None) {
1325 /* a loader was found */
1326 *p_loader = loader;
1327 Py_DECREF(meta_path);
1328 return &importhookdescr;
1329 }
1330 Py_DECREF(loader);
1331 }
1332 Py_DECREF(meta_path);
1333 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001334
Benjamin Petersond968e272008-11-05 22:48:33 +00001335 if (find_frozen(fullname) != NULL) {
1336 strcpy(buf, fullname);
1337 return &fd_frozen;
Guido van Rossum0506a431998-08-11 15:07:39 +00001338 }
Benjamin Petersond968e272008-11-05 22:48:33 +00001339
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001340 if (path == NULL) {
1341 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001342 strcpy(buf, name);
1343 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001344 }
Guido van Rossumac279101996-08-22 23:10:58 +00001345#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001346 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1347 if (fp != NULL) {
1348 *p_fp = fp;
1349 return fdp;
1350 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001351#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001352 path = PySys_GetObject("path");
1353 }
Benjamin Petersond968e272008-11-05 22:48:33 +00001354
Guido van Rossum79f25d91997-04-29 20:08:16 +00001355 if (path == NULL || !PyList_Check(path)) {
1356 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001357 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001358 return NULL;
1359 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001360
1361 path_hooks = PySys_GetObject("path_hooks");
1362 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1363 PyErr_SetString(PyExc_ImportError,
1364 "sys.path_hooks must be a list of "
1365 "import hooks");
1366 return NULL;
1367 }
1368 path_importer_cache = PySys_GetObject("path_importer_cache");
1369 if (path_importer_cache == NULL ||
1370 !PyDict_Check(path_importer_cache)) {
1371 PyErr_SetString(PyExc_ImportError,
1372 "sys.path_importer_cache must be a dict");
1373 return NULL;
1374 }
1375
Guido van Rossum79f25d91997-04-29 20:08:16 +00001376 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001377 namelen = strlen(name);
1378 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001379 PyObject *v = PyList_GetItem(path, i);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001380 PyObject *origv = v;
Guido van Rossum6262cc72007-05-09 23:29:27 +00001381 const char *base;
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001382 Py_ssize_t size;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001383 if (!v)
1384 return NULL;
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001385 if (PyUnicode_Check(v)) {
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001386 v = PyUnicode_AsEncodedString(v,
1387 Py_FileSystemDefaultEncoding, NULL);
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001388 if (v == NULL)
1389 return NULL;
1390 }
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001391 else if (!PyBytes_Check(v))
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001392 continue;
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001393 else
1394 Py_INCREF(v);
1395
Christian Heimes72b710a2008-05-26 13:28:38 +00001396 base = PyBytes_AS_STRING(v);
1397 size = PyBytes_GET_SIZE(v);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001398 len = size;
Walter Dörwald3430d702002-06-17 10:43:59 +00001399 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001400 Py_DECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001401 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001402 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001403 strcpy(buf, base);
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001404 Py_DECREF(v);
1405
Walter Dörwald3430d702002-06-17 10:43:59 +00001406 if (strlen(buf) != len) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001407 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001408 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001409
1410 /* sys.path_hooks import hook */
1411 if (p_loader != NULL) {
1412 PyObject *importer;
1413
1414 importer = get_path_importer(path_importer_cache,
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001415 path_hooks, origv);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001416 if (importer == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001417 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001418 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001419 /* Note: importer is a borrowed reference */
1420 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001421 PyObject *loader;
1422 loader = PyObject_CallMethod(importer,
1423 "find_module",
1424 "s", fullname);
1425 if (loader == NULL)
1426 return NULL; /* error */
1427 if (loader != Py_None) {
1428 /* a loader was found */
1429 *p_loader = loader;
1430 return &importhookdescr;
1431 }
1432 Py_DECREF(loader);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001433 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001434 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001435 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001436 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001437
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001438 if (len > 0 && buf[len-1] != SEP
1439#ifdef ALTSEP
1440 && buf[len-1] != ALTSEP
1441#endif
1442 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001443 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001444 strcpy(buf+len, name);
1445 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001446
1447 /* Check for package import (buf holds a directory name,
1448 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001449#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001450 if (stat(buf, &statbuf) == 0 && /* it exists */
1451 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001452 case_ok(buf, len, namelen, name)) { /* case matches */
1453 if (find_init_module(buf)) { /* and has __init__.py */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001454 return &fd_package;
1455 }
1456 else {
1457 char warnstr[MAXPATHLEN+80];
1458 sprintf(warnstr, "Not importing directory "
1459 "'%.*s': missing __init__.py",
1460 MAXPATHLEN, buf);
Skip Montanaro46fc3372007-08-12 11:44:53 +00001461 if (PyErr_WarnEx(PyExc_ImportWarning,
1462 warnstr, 1)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001463 return NULL;
1464 }
1465 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001466 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001467#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001468#if defined(PYOS_OS2)
1469 /* take a snapshot of the module spec for restoration
1470 * after the 8 character DLL hackery
1471 */
1472 saved_buf = strdup(buf);
1473 saved_len = len;
1474 saved_namelen = namelen;
1475#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001476 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum04110fb2007-08-24 16:32:05 +00001477#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001478 /* OS/2 limits DLLs to 8 character names (w/o
1479 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001480 * so if the name is longer than that and its a
1481 * dynamically loaded module we're going to try,
1482 * truncate the name before trying
1483 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001484 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001485 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001486 const struct filedescr *scan;
1487 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001488 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001489 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001490 break;
1491 else
1492 scan++;
1493 }
1494 if (scan->suffix != NULL) {
1495 /* yes, so truncate the name */
1496 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001497 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001498 buf[len] = '\0';
1499 }
1500 }
1501#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001502 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001503 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001504 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001505 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001506 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001507 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001508 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001509 if (fp != NULL) {
1510 if (case_ok(buf, len, namelen, name))
1511 break;
1512 else { /* continue search */
1513 fclose(fp);
1514 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001515 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001516 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001517#if defined(PYOS_OS2)
1518 /* restore the saved snapshot */
1519 strcpy(buf, saved_buf);
1520 len = saved_len;
1521 namelen = saved_namelen;
1522#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001523 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001524#if defined(PYOS_OS2)
1525 /* don't need/want the module name snapshot anymore */
1526 if (saved_buf)
1527 {
1528 free(saved_buf);
1529 saved_buf = NULL;
1530 }
1531#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001532 if (fp != NULL)
1533 break;
1534 }
1535 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001536 PyErr_Format(PyExc_ImportError,
1537 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001538 return NULL;
1539 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001540 *p_fp = fp;
1541 return fdp;
1542}
1543
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001544/* Helpers for main.c
1545 * Find the source file corresponding to a named module
1546 */
1547struct filedescr *
1548_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1549 size_t buflen, FILE **p_fp, PyObject **p_loader)
1550{
1551 return find_module((char *) name, (char *) name, path,
1552 buf, buflen, p_fp, p_loader);
1553}
1554
1555PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1556{
1557 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1558}
1559
Martin v. Löwis18e16552006-02-15 17:27:45 +00001560/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001561 * The arguments here are tricky, best shown by example:
1562 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1563 * ^ ^ ^ ^
1564 * |--------------------- buf ---------------------|
1565 * |------------------- len ------------------|
1566 * |------ name -------|
1567 * |----- namelen -----|
1568 * buf is the full path, but len only counts up to (& exclusive of) the
1569 * extension. name is the module name, also exclusive of extension.
1570 *
1571 * We've already done a successful stat() or fopen() on buf, so know that
1572 * there's some match, possibly case-insensitive.
1573 *
Tim Peters50d8d372001-02-28 05:34:27 +00001574 * case_ok() is to return 1 if there's a case-sensitive match for
1575 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1576 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001577 *
Tim Peters50d8d372001-02-28 05:34:27 +00001578 * case_ok() is used to implement case-sensitive import semantics even
1579 * on platforms with case-insensitive filesystems. It's trivial to implement
1580 * for case-sensitive filesystems. It's pretty much a cross-platform
1581 * nightmare for systems with case-insensitive filesystems.
1582 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001583
Tim Peters50d8d372001-02-28 05:34:27 +00001584/* First we may need a pile of platform-specific header files; the sequence
1585 * of #if's here should match the sequence in the body of case_ok().
1586 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001587#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001588#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001589
Tim Peters50d8d372001-02-28 05:34:27 +00001590#elif defined(DJGPP)
1591#include <dir.h>
1592
Jason Tishler7961aa62005-05-20 00:56:54 +00001593#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001594#include <sys/types.h>
1595#include <dirent.h>
1596
Andrew MacIntyred9400542002-02-26 11:41:34 +00001597#elif defined(PYOS_OS2)
1598#define INCL_DOS
1599#define INCL_DOSERRORS
1600#define INCL_NOPMAPI
1601#include <os2.h>
Tim Peters50d8d372001-02-28 05:34:27 +00001602#endif
1603
Guido van Rossum0980bd91998-02-13 17:18:36 +00001604static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001605case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001606{
Tim Peters50d8d372001-02-28 05:34:27 +00001607/* Pick a platform-specific implementation; the sequence of #if's here should
1608 * match the sequence just above.
1609 */
1610
Jason Tishler7961aa62005-05-20 00:56:54 +00001611/* MS_WINDOWS */
1612#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001613 WIN32_FIND_DATA data;
1614 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001615
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001616 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001617 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001618
Guido van Rossum0980bd91998-02-13 17:18:36 +00001619 h = FindFirstFile(buf, &data);
1620 if (h == INVALID_HANDLE_VALUE) {
1621 PyErr_Format(PyExc_NameError,
1622 "Can't find file for module %.100s\n(filename %.300s)",
1623 name, buf);
1624 return 0;
1625 }
1626 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001627 return strncmp(data.cFileName, name, namelen) == 0;
1628
1629/* DJGPP */
1630#elif defined(DJGPP)
1631 struct ffblk ffblk;
1632 int done;
1633
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001634 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001635 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001636
1637 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1638 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001639 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001640 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001641 name, buf);
1642 return 0;
1643 }
Tim Peters50d8d372001-02-28 05:34:27 +00001644 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001645
Jason Tishler7961aa62005-05-20 00:56:54 +00001646/* new-fangled macintosh (macosx) or Cygwin */
1647#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001648 DIR *dirp;
1649 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001650 char dirname[MAXPATHLEN + 1];
1651 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001652
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001653 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001654 return 1;
1655
Tim Petersd1e87a82001-03-01 18:12:00 +00001656 /* Copy the dir component into dirname; substitute "." if empty */
1657 if (dirlen <= 0) {
1658 dirname[0] = '.';
1659 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001660 }
1661 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001662 assert(dirlen <= MAXPATHLEN);
1663 memcpy(dirname, buf, dirlen);
1664 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001665 }
1666 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001667 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001668 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001669 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001670 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001671 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001672#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001673 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001674#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001675 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001676#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001677 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001678 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001679 (void)closedir(dirp);
1680 return 1; /* Found */
1681 }
1682 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001683 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001684 }
Tim Peters430f5d42001-03-01 01:30:56 +00001685 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001686
Andrew MacIntyred9400542002-02-26 11:41:34 +00001687/* OS/2 */
1688#elif defined(PYOS_OS2)
1689 HDIR hdir = 1;
1690 ULONG srchcnt = 1;
1691 FILEFINDBUF3 ffbuf;
1692 APIRET rc;
1693
Christian Heimes790c8232008-01-07 21:14:23 +00001694 if (Py_GETENV("PYTHONCASEOK") != NULL)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001695 return 1;
1696
1697 rc = DosFindFirst(buf,
1698 &hdir,
1699 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1700 &ffbuf, sizeof(ffbuf),
1701 &srchcnt,
1702 FIL_STANDARD);
1703 if (rc != NO_ERROR)
1704 return 0;
1705 return strncmp(ffbuf.achName, name, namelen) == 0;
1706
Tim Peters50d8d372001-02-28 05:34:27 +00001707/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1708#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001709 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001710
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001711#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001712}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001713
Guido van Rossum0980bd91998-02-13 17:18:36 +00001714
Guido van Rossum197346f1997-10-31 18:38:52 +00001715#ifdef HAVE_STAT
1716/* Helper to look for __init__.py or __init__.py[co] in potential package */
1717static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001718find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001719{
Tim Peters0f9431f2001-07-05 03:47:53 +00001720 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001721 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001722 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001723 struct stat statbuf;
1724
Tim Peters0f9431f2001-07-05 03:47:53 +00001725/* For calling case_ok(buf, len, namelen, name):
1726 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1727 * ^ ^ ^ ^
1728 * |--------------------- buf ---------------------|
1729 * |------------------- len ------------------|
1730 * |------ name -------|
1731 * |----- namelen -----|
1732 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001733 if (save_len + 13 >= MAXPATHLEN)
1734 return 0;
1735 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001736 pname = buf + i;
1737 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001738 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001739 if (case_ok(buf,
1740 save_len + 9, /* len("/__init__") */
1741 8, /* len("__init__") */
1742 pname)) {
1743 buf[save_len] = '\0';
1744 return 1;
1745 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001746 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001747 i += strlen(pname);
1748 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001749 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001750 if (case_ok(buf,
1751 save_len + 9, /* len("/__init__") */
1752 8, /* len("__init__") */
1753 pname)) {
1754 buf[save_len] = '\0';
1755 return 1;
1756 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001757 }
1758 buf[save_len] = '\0';
1759 return 0;
1760}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001761
Guido van Rossum197346f1997-10-31 18:38:52 +00001762#endif /* HAVE_STAT */
1763
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001764
Tim Petersdbd9ba62000-07-09 03:09:57 +00001765static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001766
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001767/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001768 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001769
Guido van Rossum79f25d91997-04-29 20:08:16 +00001770static PyObject *
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001771load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001772{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001773 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001774 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001775 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001776
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001777 /* First check that there's an open file (if we need one) */
1778 switch (type) {
1779 case PY_SOURCE:
1780 case PY_COMPILED:
1781 if (fp == NULL) {
1782 PyErr_Format(PyExc_ValueError,
1783 "file object required for import (type code %d)",
1784 type);
1785 return NULL;
1786 }
1787 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001788
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001789 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001790
1791 case PY_SOURCE:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001792 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001793 break;
1794
1795 case PY_COMPILED:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001796 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001797 break;
1798
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001799#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001800 case C_EXTENSION:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001801 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001802 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001803#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001804
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001805 case PKG_DIRECTORY:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001806 m = load_package(name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001807 break;
1808
1809 case C_BUILTIN:
1810 case PY_FROZEN:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001811 if (pathname != NULL && pathname[0] != '\0')
1812 name = pathname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001813 if (type == C_BUILTIN)
1814 err = init_builtin(name);
1815 else
1816 err = PyImport_ImportFrozenModule(name);
1817 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001818 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001819 if (err == 0) {
1820 PyErr_Format(PyExc_ImportError,
1821 "Purported %s module %.200s not found",
1822 type == C_BUILTIN ?
1823 "builtin" : "frozen",
1824 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001825 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001826 }
1827 modules = PyImport_GetModuleDict();
1828 m = PyDict_GetItemString(modules, name);
1829 if (m == NULL) {
1830 PyErr_Format(
1831 PyExc_ImportError,
1832 "%s module %.200s not properly initialized",
1833 type == C_BUILTIN ?
1834 "builtin" : "frozen",
1835 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001836 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001837 }
1838 Py_INCREF(m);
1839 break;
1840
Just van Rossum52e14d62002-12-30 22:08:05 +00001841 case IMP_HOOK: {
1842 if (loader == NULL) {
1843 PyErr_SetString(PyExc_ImportError,
1844 "import hook without loader");
1845 return NULL;
1846 }
1847 m = PyObject_CallMethod(loader, "load_module", "s", name);
1848 break;
1849 }
1850
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001851 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001852 PyErr_Format(PyExc_ImportError,
1853 "Don't know how to import %.200s (type code %d)",
1854 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001855 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001856
1857 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001858
1859 return m;
1860}
1861
1862
1863/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001864 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001865 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001866
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001867static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001868init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001869{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001870 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001871
Greg Ward201baee2001-10-04 14:52:06 +00001872 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001873 return 1;
1874
Guido van Rossum771c6c81997-10-31 18:37:24 +00001875 for (p = PyImport_Inittab; p->name != NULL; p++) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00001876 PyObject *mod;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001877 if (strcmp(name, p->name) == 0) {
1878 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001879 PyErr_Format(PyExc_ImportError,
1880 "Cannot re-init internal module %.200s",
1881 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001882 return -1;
1883 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001884 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001885 PySys_WriteStderr("import %s # builtin\n", name);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001886 mod = (*p->initfunc)();
1887 if (mod == 0)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001888 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001889 if (_PyImport_FixupExtension(mod, name, name) < 0)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001890 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001891 /* FixupExtension has put the module into sys.modules,
1892 so we can release our own reference. */
1893 Py_DECREF(mod);
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001894 return 1;
1895 }
1896 }
1897 return 0;
1898}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001899
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001900
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001901/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001902
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001903static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001904find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001905{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001906 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001907
Benjamin Petersond968e272008-11-05 22:48:33 +00001908 if (!name)
1909 return NULL;
1910
Guido van Rossum79f25d91997-04-29 20:08:16 +00001911 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001912 if (p->name == NULL)
1913 return NULL;
1914 if (strcmp(p->name, name) == 0)
1915 break;
1916 }
1917 return p;
1918}
1919
Guido van Rossum79f25d91997-04-29 20:08:16 +00001920static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001921get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001922{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001923 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001924 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001925
1926 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001927 PyErr_Format(PyExc_ImportError,
1928 "No such frozen object named %.200s",
1929 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001930 return NULL;
1931 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001932 if (p->code == NULL) {
1933 PyErr_Format(PyExc_ImportError,
1934 "Excluded frozen object named %.200s",
1935 name);
1936 return NULL;
1937 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001938 size = p->size;
1939 if (size < 0)
1940 size = -size;
1941 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001942}
1943
Brett Cannon8d110132009-03-15 02:20:16 +00001944static PyObject *
1945is_frozen_package(char *name)
1946{
1947 struct _frozen *p = find_frozen(name);
1948 int size;
1949
1950 if (p == NULL) {
1951 PyErr_Format(PyExc_ImportError,
1952 "No such frozen object named %.200s",
1953 name);
1954 return NULL;
1955 }
1956
1957 size = p->size;
1958
1959 if (size < 0)
1960 Py_RETURN_TRUE;
1961 else
1962 Py_RETURN_FALSE;
1963}
1964
1965
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001966/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001967 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001968 an exception set if the initialization failed.
1969 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001970
1971int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001972PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001973{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001974 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001975 PyObject *co;
1976 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001977 int ispackage;
1978 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001979
1980 if (p == NULL)
1981 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001982 if (p->code == NULL) {
1983 PyErr_Format(PyExc_ImportError,
1984 "Excluded frozen object named %.200s",
1985 name);
1986 return -1;
1987 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001988 size = p->size;
1989 ispackage = (size < 0);
1990 if (ispackage)
1991 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001992 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001993 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001994 name, ispackage ? " package" : "");
1995 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001996 if (co == NULL)
1997 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001998 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001999 PyErr_Format(PyExc_TypeError,
2000 "frozen object %.200s is not a code object",
2001 name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002002 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002003 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00002004 if (ispackage) {
2005 /* Set __path__ to the package name */
Benjamin Petersond968e272008-11-05 22:48:33 +00002006 PyObject *d, *s, *l;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002007 int err;
2008 m = PyImport_AddModule(name);
2009 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002010 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002011 d = PyModule_GetDict(m);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002012 s = PyUnicode_InternFromString(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00002013 if (s == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002014 goto err_return;
Benjamin Petersond968e272008-11-05 22:48:33 +00002015 l = PyList_New(1);
2016 if (l == NULL) {
2017 Py_DECREF(s);
2018 goto err_return;
2019 }
2020 PyList_SET_ITEM(l, 0, s);
2021 err = PyDict_SetItemString(d, "__path__", l);
2022 Py_DECREF(l);
Guido van Rossuma5568d31998-03-05 03:45:08 +00002023 if (err != 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002024 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002025 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00002026 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002027 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002028 goto err_return;
2029 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002030 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002031 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002032err_return:
2033 Py_DECREF(co);
2034 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002035}
Guido van Rossum74e6a111994-08-29 12:54:38 +00002036
2037
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002038/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002039 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00002040
Guido van Rossum79f25d91997-04-29 20:08:16 +00002041PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002042PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00002043{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002044 PyObject *pname;
2045 PyObject *result;
2046
Martin v. Löwis5b222132007-06-10 09:51:05 +00002047 pname = PyUnicode_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00002048 if (pname == NULL)
2049 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002050 result = PyImport_Import(pname);
2051 Py_DECREF(pname);
2052 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002053}
2054
Christian Heimes072c0f12008-01-03 23:01:04 +00002055/* Import a module without blocking
2056 *
2057 * At first it tries to fetch the module from sys.modules. If the module was
2058 * never loaded before it loads it with PyImport_ImportModule() unless another
2059 * thread holds the import lock. In the latter case the function raises an
2060 * ImportError instead of blocking.
2061 *
2062 * Returns the module object with incremented ref count.
2063 */
2064PyObject *
2065PyImport_ImportModuleNoBlock(const char *name)
2066{
2067 PyObject *result;
2068 PyObject *modules;
2069 long me;
2070
2071 /* Try to get the module from sys.modules[name] */
2072 modules = PyImport_GetModuleDict();
2073 if (modules == NULL)
2074 return NULL;
2075
2076 result = PyDict_GetItemString(modules, name);
2077 if (result != NULL) {
2078 Py_INCREF(result);
2079 return result;
2080 }
2081 else {
2082 PyErr_Clear();
2083 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002084#ifdef WITH_THREAD
Christian Heimes072c0f12008-01-03 23:01:04 +00002085 /* check the import lock
2086 * me might be -1 but I ignore the error here, the lock function
2087 * takes care of the problem */
2088 me = PyThread_get_thread_ident();
2089 if (import_lock_thread == -1 || import_lock_thread == me) {
2090 /* no thread or me is holding the lock */
2091 return PyImport_ImportModule(name);
2092 }
2093 else {
2094 PyErr_Format(PyExc_ImportError,
2095 "Failed to import %.200s because the import lock"
2096 "is held by another thread.",
2097 name);
2098 return NULL;
2099 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002100#else
2101 return PyImport_ImportModule(name);
2102#endif
Christian Heimes072c0f12008-01-03 23:01:04 +00002103}
2104
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002105/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002106static PyObject *get_parent(PyObject *globals, char *buf,
2107 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002108static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002109 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002110static int mark_miss(char *name);
2111static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002112 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002113static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002114
2115/* The Magnum Opus of dotted-name import :-) */
2116
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002117static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002118import_module_level(char *name, PyObject *globals, PyObject *locals,
2119 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002120{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002121 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00002122 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002123 PyObject *parent, *head, *next, *tail;
2124
Christian Heimes454f37b2008-01-10 00:10:02 +00002125 if (strchr(name, '/') != NULL
2126#ifdef MS_WINDOWS
2127 || strchr(name, '\\') != NULL
2128#endif
2129 ) {
2130 PyErr_SetString(PyExc_ImportError,
2131 "Import by filename is not supported.");
2132 return NULL;
2133 }
2134
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002135 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002136 if (parent == NULL)
2137 return NULL;
2138
2139 head = load_next(parent, Py_None, &name, buf, &buflen);
2140 if (head == NULL)
2141 return NULL;
2142
2143 tail = head;
2144 Py_INCREF(tail);
2145 while (name) {
2146 next = load_next(tail, tail, &name, buf, &buflen);
2147 Py_DECREF(tail);
2148 if (next == NULL) {
2149 Py_DECREF(head);
2150 return NULL;
2151 }
2152 tail = next;
2153 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002154 if (tail == Py_None) {
2155 /* If tail is Py_None, both get_parent and load_next found
2156 an empty module name: someone called __import__("") or
2157 doctored faulty bytecode */
2158 Py_DECREF(tail);
2159 Py_DECREF(head);
2160 PyErr_SetString(PyExc_ValueError,
2161 "Empty module name");
2162 return NULL;
2163 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002164
2165 if (fromlist != NULL) {
2166 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2167 fromlist = NULL;
2168 }
2169
2170 if (fromlist == NULL) {
2171 Py_DECREF(tail);
2172 return head;
2173 }
2174
2175 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002176 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002177 Py_DECREF(tail);
2178 return NULL;
2179 }
2180
2181 return tail;
2182}
2183
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002184PyObject *
2185PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2186 PyObject *fromlist, int level)
2187{
2188 PyObject *result;
Benjamin Peterson0df35a92009-10-04 20:32:25 +00002189 _PyImport_AcquireLock();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002190 result = import_module_level(name, globals, locals, fromlist, level);
Benjamin Peterson0df35a92009-10-04 20:32:25 +00002191 if (_PyImport_ReleaseLock() < 0) {
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002192 Py_XDECREF(result);
2193 PyErr_SetString(PyExc_RuntimeError,
2194 "not holding the import lock");
2195 return NULL;
2196 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002197 return result;
2198}
2199
Fred Drake87590902004-05-28 20:21:36 +00002200/* Return the package that an import is being performed in. If globals comes
2201 from the module foo.bar.bat (not itself a package), this returns the
2202 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002203 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002204
2205 The *name* of the returned package is returned in buf, with the length of
2206 the name in *p_buflen.
2207
2208 If globals doesn't come from a package or a module in a package, or a
2209 corresponding entry is not found in sys.modules, Py_None is returned.
2210*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002211static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002212get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002213{
2214 static PyObject *namestr = NULL;
2215 static PyObject *pathstr = NULL;
Nick Coghlande10c852007-12-04 12:22:52 +00002216 static PyObject *pkgstr = NULL;
2217 PyObject *pkgname, *modname, *modpath, *modules, *parent;
Georg Brandl2ee470f2008-07-16 12:55:28 +00002218 int orig_level = level;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002219
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002220 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002221 return Py_None;
2222
2223 if (namestr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002224 namestr = PyUnicode_InternFromString("__name__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002225 if (namestr == NULL)
2226 return NULL;
2227 }
2228 if (pathstr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002229 pathstr = PyUnicode_InternFromString("__path__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002230 if (pathstr == NULL)
2231 return NULL;
2232 }
Nick Coghlande10c852007-12-04 12:22:52 +00002233 if (pkgstr == NULL) {
2234 pkgstr = PyUnicode_InternFromString("__package__");
2235 if (pkgstr == NULL)
2236 return NULL;
2237 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002238
2239 *buf = '\0';
2240 *p_buflen = 0;
Nick Coghlande10c852007-12-04 12:22:52 +00002241 pkgname = PyDict_GetItem(globals, pkgstr);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002242
Nick Coghlande10c852007-12-04 12:22:52 +00002243 if ((pkgname != NULL) && (pkgname != Py_None)) {
2244 /* __package__ is set, so use it */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002245 char *pkgname_str;
Nick Coghlande10c852007-12-04 12:22:52 +00002246 Py_ssize_t len;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002247
Nick Coghlande10c852007-12-04 12:22:52 +00002248 if (!PyUnicode_Check(pkgname)) {
2249 PyErr_SetString(PyExc_ValueError,
2250 "__package__ set to non-string");
2251 return NULL;
2252 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002253 pkgname_str = _PyUnicode_AsStringAndSize(pkgname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002254 if (len == 0) {
2255 if (level > 0) {
2256 PyErr_SetString(PyExc_ValueError,
2257 "Attempted relative import in non-package");
2258 return NULL;
2259 }
2260 return Py_None;
2261 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002262 if (len > MAXPATHLEN) {
2263 PyErr_SetString(PyExc_ValueError,
Nick Coghlande10c852007-12-04 12:22:52 +00002264 "Package name too long");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002265 return NULL;
2266 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002267 strcpy(buf, pkgname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002268 } else {
2269 /* __package__ not set, so figure it out and set it */
2270 modname = PyDict_GetItem(globals, namestr);
2271 if (modname == NULL || !PyUnicode_Check(modname))
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002272 return Py_None;
Nick Coghlande10c852007-12-04 12:22:52 +00002273
2274 modpath = PyDict_GetItem(globals, pathstr);
2275 if (modpath != NULL) {
2276 /* __path__ is set, so modname is already the package name */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002277 char *modname_str;
2278 Py_ssize_t len;
Nick Coghlande10c852007-12-04 12:22:52 +00002279 int error;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002280
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002281 modname_str = _PyUnicode_AsStringAndSize(modname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002282 if (len > MAXPATHLEN) {
2283 PyErr_SetString(PyExc_ValueError,
2284 "Module name too long");
2285 return NULL;
2286 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002287 strcpy(buf, modname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002288 error = PyDict_SetItem(globals, pkgstr, modname);
2289 if (error) {
2290 PyErr_SetString(PyExc_ValueError,
2291 "Could not set __package__");
2292 return NULL;
2293 }
2294 } else {
2295 /* Normal module, so work out the package name if any */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002296 char *start = _PyUnicode_AsString(modname);
Nick Coghlande10c852007-12-04 12:22:52 +00002297 char *lastdot = strrchr(start, '.');
2298 size_t len;
2299 int error;
2300 if (lastdot == NULL && level > 0) {
2301 PyErr_SetString(PyExc_ValueError,
2302 "Attempted relative import in non-package");
2303 return NULL;
2304 }
2305 if (lastdot == NULL) {
2306 error = PyDict_SetItem(globals, pkgstr, Py_None);
2307 if (error) {
2308 PyErr_SetString(PyExc_ValueError,
2309 "Could not set __package__");
2310 return NULL;
2311 }
2312 return Py_None;
2313 }
2314 len = lastdot - start;
2315 if (len >= MAXPATHLEN) {
2316 PyErr_SetString(PyExc_ValueError,
2317 "Module name too long");
2318 return NULL;
2319 }
2320 strncpy(buf, start, len);
2321 buf[len] = '\0';
2322 pkgname = PyUnicode_FromString(buf);
2323 if (pkgname == NULL) {
2324 return NULL;
2325 }
2326 error = PyDict_SetItem(globals, pkgstr, pkgname);
2327 Py_DECREF(pkgname);
2328 if (error) {
2329 PyErr_SetString(PyExc_ValueError,
2330 "Could not set __package__");
2331 return NULL;
2332 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002333 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002334 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002335 while (--level > 0) {
2336 char *dot = strrchr(buf, '.');
2337 if (dot == NULL) {
2338 PyErr_SetString(PyExc_ValueError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002339 "Attempted relative import beyond "
2340 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002341 return NULL;
2342 }
2343 *dot = '\0';
2344 }
2345 *p_buflen = strlen(buf);
2346
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002347 modules = PyImport_GetModuleDict();
2348 parent = PyDict_GetItemString(modules, buf);
Georg Brandl2ee470f2008-07-16 12:55:28 +00002349 if (parent == NULL) {
2350 if (orig_level < 1) {
2351 PyObject *err_msg = PyBytes_FromFormat(
2352 "Parent module '%.200s' not found "
2353 "while handling absolute import", buf);
2354 if (err_msg == NULL) {
2355 return NULL;
2356 }
2357 if (!PyErr_WarnEx(PyExc_RuntimeWarning,
2358 PyBytes_AsString(err_msg), 1)) {
2359 *buf = '\0';
2360 *p_buflen = 0;
2361 parent = Py_None;
2362 }
2363 Py_DECREF(err_msg);
2364 } else {
2365 PyErr_Format(PyExc_SystemError,
2366 "Parent module '%.200s' not loaded, "
2367 "cannot perform relative import", buf);
2368 }
2369 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002370 return parent;
2371 /* We expect, but can't guarantee, if parent != None, that:
2372 - parent.__name__ == buf
2373 - parent.__dict__ is globals
2374 If this is violated... Who cares? */
2375}
2376
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002377/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002378static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002379load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002380 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002381{
2382 char *name = *p_name;
2383 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002384 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002385 char *p;
2386 PyObject *result;
2387
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002388 if (strlen(name) == 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002389 /* completely empty module name should only happen in
2390 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002391 Py_INCREF(mod);
2392 *p_name = NULL;
2393 return mod;
2394 }
2395
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002396 if (dot == NULL) {
2397 *p_name = NULL;
2398 len = strlen(name);
2399 }
2400 else {
2401 *p_name = dot+1;
2402 len = dot-name;
2403 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002404 if (len == 0) {
2405 PyErr_SetString(PyExc_ValueError,
2406 "Empty module name");
2407 return NULL;
2408 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002409
2410 p = buf + *p_buflen;
2411 if (p != buf)
2412 *p++ = '.';
2413 if (p+len-buf >= MAXPATHLEN) {
2414 PyErr_SetString(PyExc_ValueError,
2415 "Module name too long");
2416 return NULL;
2417 }
2418 strncpy(p, name, len);
2419 p[len] = '\0';
2420 *p_buflen = p+len-buf;
2421
2422 result = import_submodule(mod, p, buf);
2423 if (result == Py_None && altmod != mod) {
2424 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002425 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002426 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002427 if (result != NULL && result != Py_None) {
2428 if (mark_miss(buf) != 0) {
2429 Py_DECREF(result);
2430 return NULL;
2431 }
2432 strncpy(buf, name, len);
2433 buf[len] = '\0';
2434 *p_buflen = len;
2435 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002436 }
2437 if (result == NULL)
2438 return NULL;
2439
2440 if (result == Py_None) {
2441 Py_DECREF(result);
2442 PyErr_Format(PyExc_ImportError,
2443 "No module named %.200s", name);
2444 return NULL;
2445 }
2446
2447 return result;
2448}
2449
2450static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002451mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002452{
2453 PyObject *modules = PyImport_GetModuleDict();
2454 return PyDict_SetItemString(modules, name, Py_None);
2455}
2456
2457static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002458ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002459 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002460{
2461 int i;
2462
2463 if (!PyObject_HasAttrString(mod, "__path__"))
2464 return 1;
2465
2466 for (i = 0; ; i++) {
2467 PyObject *item = PySequence_GetItem(fromlist, i);
2468 int hasit;
2469 if (item == NULL) {
2470 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2471 PyErr_Clear();
2472 return 1;
2473 }
2474 return 0;
2475 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002476 if (!PyUnicode_Check(item)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002477 PyErr_SetString(PyExc_TypeError,
Neal Norwitz80e7f272007-08-26 06:45:23 +00002478 "Item in ``from list'' not a string");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002479 Py_DECREF(item);
2480 return 0;
2481 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002482 if (PyUnicode_AS_UNICODE(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002483 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002484 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002485 /* See if the package defines __all__ */
2486 if (recursive)
2487 continue; /* Avoid endless recursion */
2488 all = PyObject_GetAttrString(mod, "__all__");
2489 if (all == NULL)
2490 PyErr_Clear();
2491 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002492 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002493 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002494 if (!ret)
2495 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002496 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002497 continue;
2498 }
2499 hasit = PyObject_HasAttr(mod, item);
2500 if (!hasit) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002501 PyObject *item8;
2502 char *subname;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002503 PyObject *submod;
2504 char *p;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002505 if (!Py_FileSystemDefaultEncoding) {
2506 item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item),
2507 PyUnicode_GetSize(item),
Martin v. Löwis427dbff2007-06-12 05:53:00 +00002508 NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002509 } else {
Guido van Rossum98297ee2007-11-06 21:34:58 +00002510 item8 = PyUnicode_AsEncodedString(item,
2511 Py_FileSystemDefaultEncoding, NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002512 }
2513 if (!item8) {
2514 PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
2515 return 0;
2516 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002517 subname = PyBytes_AS_STRING(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002518 if (buflen + strlen(subname) >= MAXPATHLEN) {
2519 PyErr_SetString(PyExc_ValueError,
2520 "Module name too long");
2521 Py_DECREF(item);
2522 return 0;
2523 }
2524 p = buf + buflen;
2525 *p++ = '.';
2526 strcpy(p, subname);
2527 submod = import_submodule(mod, subname, buf);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002528 Py_DECREF(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002529 Py_XDECREF(submod);
2530 if (submod == NULL) {
2531 Py_DECREF(item);
2532 return 0;
2533 }
2534 }
2535 Py_DECREF(item);
2536 }
2537
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002538 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002539}
2540
Neil Schemenauer00b09662003-06-16 21:03:07 +00002541static int
2542add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2543 PyObject *modules)
2544{
2545 if (mod == Py_None)
2546 return 1;
2547 /* Irrespective of the success of this load, make a
2548 reference to it in the parent package module. A copy gets
2549 saved in the modules dictionary under the full name, so get a
2550 reference from there, if need be. (The exception is when the
2551 load failed with a SyntaxError -- then there's no trace in
2552 sys.modules. In that case, of course, do nothing extra.) */
2553 if (submod == NULL) {
2554 submod = PyDict_GetItemString(modules, fullname);
2555 if (submod == NULL)
2556 return 1;
2557 }
2558 if (PyModule_Check(mod)) {
2559 /* We can't use setattr here since it can give a
2560 * spurious warning if the submodule name shadows a
2561 * builtin name */
2562 PyObject *dict = PyModule_GetDict(mod);
2563 if (!dict)
2564 return 0;
2565 if (PyDict_SetItemString(dict, subname, submod) < 0)
2566 return 0;
2567 }
2568 else {
2569 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2570 return 0;
2571 }
2572 return 1;
2573}
2574
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002575static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002576import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002577{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002578 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002579 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002580
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002581 /* Require:
2582 if mod == None: subname == fullname
2583 else: mod.__name__ + "." + subname == fullname
2584 */
2585
Tim Peters50d8d372001-02-28 05:34:27 +00002586 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002587 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002588 }
2589 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002590 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002591 char buf[MAXPATHLEN+1];
2592 struct filedescr *fdp;
2593 FILE *fp = NULL;
2594
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002595 if (mod == Py_None)
2596 path = NULL;
2597 else {
2598 path = PyObject_GetAttrString(mod, "__path__");
2599 if (path == NULL) {
2600 PyErr_Clear();
2601 Py_INCREF(Py_None);
2602 return Py_None;
2603 }
2604 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002605
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002606 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002607 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2608 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002609 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002610 if (fdp == NULL) {
2611 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2612 return NULL;
2613 PyErr_Clear();
2614 Py_INCREF(Py_None);
2615 return Py_None;
2616 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002617 m = load_module(fullname, fp, buf, fdp->type, loader);
2618 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002619 if (fp)
2620 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002621 if (!add_submodule(mod, m, fullname, subname, modules)) {
2622 Py_XDECREF(m);
2623 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002624 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002625 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002626
2627 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002628}
2629
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002630
2631/* Re-import a module of any kind and return its module object, WITH
2632 INCREMENTED REFERENCE COUNT */
2633
Guido van Rossum79f25d91997-04-29 20:08:16 +00002634PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002635PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002636{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002637 PyInterpreterState *interp = PyThreadState_Get()->interp;
2638 PyObject *modules_reloading = interp->modules_reloading;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002639 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossumd8faa362007-04-27 19:54:29 +00002640 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002641 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002642 char buf[MAXPATHLEN+1];
2643 struct filedescr *fdp;
2644 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002645 PyObject *newm;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002646
2647 if (modules_reloading == NULL) {
2648 Py_FatalError("PyImport_ReloadModule: "
Guido van Rossume7ba4952007-06-06 23:52:48 +00002649 "no modules_reloading dictionary!");
Guido van Rossumd8faa362007-04-27 19:54:29 +00002650 return NULL;
2651 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002652
Guido van Rossum79f25d91997-04-29 20:08:16 +00002653 if (m == NULL || !PyModule_Check(m)) {
2654 PyErr_SetString(PyExc_TypeError,
2655 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002656 return NULL;
2657 }
Neal Norwitz5616c6d2007-08-26 05:32:41 +00002658 name = (char*)PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002659 if (name == NULL)
2660 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002661 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002662 PyErr_Format(PyExc_ImportError,
2663 "reload(): module %.200s not in sys.modules",
2664 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002665 return NULL;
2666 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002667 existing_m = PyDict_GetItemString(modules_reloading, name);
2668 if (existing_m != NULL) {
2669 /* Due to a recursive reload, this module is already
2670 being reloaded. */
2671 Py_INCREF(existing_m);
2672 return existing_m;
2673 }
2674 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
2675 return NULL;
2676
Guido van Rossum222ef561997-09-06 19:41:09 +00002677 subname = strrchr(name, '.');
2678 if (subname == NULL)
2679 subname = name;
2680 else {
2681 PyObject *parentname, *parent;
Christian Heimesc4cb3b82007-11-04 12:10:01 +00002682 parentname = PyUnicode_FromStringAndSize(name, (subname-name));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002683 if (parentname == NULL) {
2684 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002685 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002686 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002687 parent = PyDict_GetItem(modules, parentname);
2688 if (parent == NULL) {
2689 PyErr_Format(PyExc_ImportError,
2690 "reload(): parent %.200s not in sys.modules",
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002691 _PyUnicode_AsString(parentname));
Georg Brandl0c55f292005-09-14 06:56:20 +00002692 Py_DECREF(parentname);
Guido van Rossume7ba4952007-06-06 23:52:48 +00002693 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002694 return NULL;
2695 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002696 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002697 subname++;
2698 path = PyObject_GetAttrString(parent, "__path__");
2699 if (path == NULL)
2700 PyErr_Clear();
2701 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002702 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002703 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002704 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002705
2706 if (fdp == NULL) {
2707 Py_XDECREF(loader);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002708 imp_modules_reloading_clear();
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002709 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002710 }
2711
2712 newm = load_module(name, fp, buf, fdp->type, loader);
2713 Py_XDECREF(loader);
2714
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002715 if (fp)
2716 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002717 if (newm == NULL) {
2718 /* load_module probably removed name from modules because of
2719 * the error. Put back the original module object. We're
2720 * going to return NULL in this case regardless of whether
2721 * replacing name succeeds, so the return value is ignored.
2722 */
2723 PyDict_SetItemString(modules, name, m);
2724 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002725 imp_modules_reloading_clear();
Tim Peters1cd70172004-08-02 03:52:12 +00002726 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002727}
2728
2729
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002730/* Higher-level import emulator which emulates the "import" statement
2731 more accurately -- it invokes the __import__() function from the
2732 builtins of the current globals. This means that the import is
2733 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002734 environment, e.g. by "rexec".
2735 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00002736 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00002737 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002738
2739PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002740PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002741{
2742 static PyObject *silly_list = NULL;
2743 static PyObject *builtins_str = NULL;
2744 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002745 PyObject *globals = NULL;
2746 PyObject *import = NULL;
2747 PyObject *builtins = NULL;
2748 PyObject *r = NULL;
2749
2750 /* Initialize constant string objects */
2751 if (silly_list == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002752 import_str = PyUnicode_InternFromString("__import__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002753 if (import_str == NULL)
2754 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002755 builtins_str = PyUnicode_InternFromString("__builtins__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002756 if (builtins_str == NULL)
2757 return NULL;
2758 silly_list = Py_BuildValue("[s]", "__doc__");
2759 if (silly_list == NULL)
2760 return NULL;
2761 }
2762
2763 /* Get the builtins from current globals */
2764 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002765 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002766 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002767 builtins = PyObject_GetItem(globals, builtins_str);
2768 if (builtins == NULL)
2769 goto err;
2770 }
2771 else {
2772 /* No globals -- use standard builtins, and fake globals */
2773 PyErr_Clear();
2774
Georg Brandl1a3284e2007-12-02 09:40:06 +00002775 builtins = PyImport_ImportModuleLevel("builtins",
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002776 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002777 if (builtins == NULL)
2778 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002779 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2780 if (globals == NULL)
2781 goto err;
2782 }
2783
2784 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002785 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002786 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002787 if (import == NULL)
2788 PyErr_SetObject(PyExc_KeyError, import_str);
2789 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002790 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002791 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002792 if (import == NULL)
2793 goto err;
2794
Christian Heimes072c0f12008-01-03 23:01:04 +00002795 /* Call the __import__ function with the proper argument list
2796 * Always use absolute import here. */
2797 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
2798 globals, silly_list, 0, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002799
2800 err:
2801 Py_XDECREF(globals);
2802 Py_XDECREF(builtins);
2803 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002804
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002805 return r;
2806}
2807
2808
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002809/* Module 'imp' provides Python access to the primitives used for
2810 importing modules.
2811*/
2812
Guido van Rossum79f25d91997-04-29 20:08:16 +00002813static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002814imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002815{
2816 char buf[4];
2817
Guido van Rossum96774c12000-05-01 20:19:08 +00002818 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2819 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2820 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2821 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002822
Christian Heimes72b710a2008-05-26 13:28:38 +00002823 return PyBytes_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002824}
2825
Guido van Rossum79f25d91997-04-29 20:08:16 +00002826static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002827imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002828{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002829 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002830 struct filedescr *fdp;
2831
Guido van Rossum79f25d91997-04-29 20:08:16 +00002832 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002833 if (list == NULL)
2834 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002835 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2836 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002837 fdp->suffix, fdp->mode, fdp->type);
2838 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002839 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002840 return NULL;
2841 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002842 if (PyList_Append(list, item) < 0) {
2843 Py_DECREF(list);
2844 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002845 return NULL;
2846 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002847 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002848 }
2849 return list;
2850}
2851
Guido van Rossum79f25d91997-04-29 20:08:16 +00002852static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002853call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002854{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002855 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002856 PyObject *fob, *ret;
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002857 PyObject *pathobj;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002858 struct filedescr *fdp;
2859 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002860 FILE *fp = NULL;
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002861 int fd = -1;
Brett Cannon3bb42d92007-10-20 03:43:15 +00002862 char *found_encoding = NULL;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002863 char *encoding = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002864
2865 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002866 if (path == Py_None)
2867 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002868 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002869 if (fdp == NULL)
2870 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002871 if (fp != NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002872 fd = fileno(fp);
2873 if (fd != -1)
2874 fd = dup(fd);
2875 fclose(fp);
2876 fp = NULL;
2877 }
2878 if (fd != -1) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002879 if (strchr(fdp->mode, 'b') == NULL) {
Brett Cannon3bb42d92007-10-20 03:43:15 +00002880 /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
2881 memory. */
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002882 found_encoding = PyTokenizer_FindEncoding(fd);
2883 lseek(fd, 0, 0); /* Reset position */
Amaury Forgeot d'Arc1b933ed2008-09-04 22:34:09 +00002884 if (found_encoding == NULL && PyErr_Occurred())
2885 return NULL;
Brett Cannon3bb42d92007-10-20 03:43:15 +00002886 encoding = (found_encoding != NULL) ? found_encoding :
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002887 (char*)PyUnicode_GetDefaultEncoding();
2888 }
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002889 fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002890 (char*)encoding, NULL, NULL, 1);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002891 if (fob == NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002892 close(fd);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002893 PyMem_FREE(found_encoding);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002894 return NULL;
2895 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002896 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002897 else {
2898 fob = Py_None;
2899 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002900 }
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002901 pathobj = PyUnicode_DecodeFSDefault(pathname);
2902 ret = Py_BuildValue("NN(ssi)",
2903 fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002904 PyMem_FREE(found_encoding);
2905
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002906 return ret;
2907}
2908
Guido van Rossum79f25d91997-04-29 20:08:16 +00002909static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002910imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002911{
2912 char *name;
Hirokazu Yamamoto90eaaf62009-01-30 03:15:05 +00002913 PyObject *ret, *path = NULL;
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002914 if (!PyArg_ParseTuple(args, "es|O:find_module",
2915 Py_FileSystemDefaultEncoding, &name,
2916 &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002917 return NULL;
Hirokazu Yamamoto90eaaf62009-01-30 03:15:05 +00002918 ret = call_find_module(name, path);
2919 PyMem_Free(name);
2920 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002921}
2922
2923static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002924imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002925{
2926 char *name;
2927 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002928 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002929 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002930 return NULL;
2931 ret = init_builtin(name);
2932 if (ret < 0)
2933 return NULL;
2934 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002935 Py_INCREF(Py_None);
2936 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002937 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002938 m = PyImport_AddModule(name);
2939 Py_XINCREF(m);
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_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002945{
2946 char *name;
2947 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002948 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002949 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002950 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002951 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002952 if (ret < 0)
2953 return NULL;
2954 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002955 Py_INCREF(Py_None);
2956 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002957 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002958 m = PyImport_AddModule(name);
2959 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002960 return m;
2961}
2962
Guido van Rossum79f25d91997-04-29 20:08:16 +00002963static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002964imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002965{
2966 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002967
Guido van Rossum43713e52000-02-29 13:59:29 +00002968 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002969 return NULL;
2970 return get_frozen_object(name);
2971}
2972
Guido van Rossum79f25d91997-04-29 20:08:16 +00002973static PyObject *
Brett Cannon8d110132009-03-15 02:20:16 +00002974imp_is_frozen_package(PyObject *self, PyObject *args)
2975{
2976 char *name;
2977
2978 if (!PyArg_ParseTuple(args, "s:is_frozen_package", &name))
2979 return NULL;
2980 return is_frozen_package(name);
2981}
2982
2983static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002984imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002985{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002986 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002987 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002988 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00002989 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002990}
2991
Guido van Rossum79f25d91997-04-29 20:08:16 +00002992static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002993imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002994{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002995 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002996 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002997 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002998 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002999 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00003000 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003001}
3002
3003static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003004get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003005{
3006 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003007 if (mode[0] == 'U')
3008 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003009 if (fob == NULL) {
3010 fp = fopen(pathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003011 }
3012 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003013 int fd = PyObject_AsFileDescriptor(fob);
3014 if (fd == -1)
3015 return NULL;
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003016 if (!_PyVerify_fd(fd))
3017 goto error;
3018 /* the FILE struct gets a new fd, so that it can be closed
3019 * independently of the file descriptor given
3020 */
3021 fd = dup(fd);
3022 if (fd == -1)
3023 goto error;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003024 fp = fdopen(fd, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003025 }
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003026 if (fp)
3027 return fp;
3028error:
3029 PyErr_SetFromErrno(PyExc_IOError);
3030 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003031}
3032
Guido van Rossum79f25d91997-04-29 20:08:16 +00003033static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003034imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003035{
3036 char *name;
3037 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003038 PyObject *fob = NULL;
3039 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003040 FILE *fp;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003041 if (!PyArg_ParseTuple(args, "ses|O:load_compiled",
3042 &name,
3043 Py_FileSystemDefaultEncoding, &pathname,
3044 &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003045 return NULL;
3046 fp = get_file(pathname, fob, "rb");
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003047 if (fp == NULL) {
3048 PyMem_Free(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003049 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003050 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003051 m = load_compiled_module(name, pathname, fp);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003052 fclose(fp);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003053 PyMem_Free(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003054 return m;
3055}
3056
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003057#ifdef HAVE_DYNAMIC_LOADING
3058
Guido van Rossum79f25d91997-04-29 20:08:16 +00003059static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003060imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003061{
3062 char *name;
3063 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003064 PyObject *fob = NULL;
3065 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00003066 FILE *fp = NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003067 if (!PyArg_ParseTuple(args, "ses|O:load_dynamic",
3068 &name,
3069 Py_FileSystemDefaultEncoding, &pathname,
3070 &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003071 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003072 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00003073 fp = get_file(pathname, fob, "r");
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003074 if (fp == NULL) {
3075 PyMem_Free(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003076 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003077 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003078 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003079 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003080 PyMem_Free(pathname);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003081 if (fp)
3082 fclose(fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00003083 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003084}
3085
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003086#endif /* HAVE_DYNAMIC_LOADING */
3087
Guido van Rossum79f25d91997-04-29 20:08:16 +00003088static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003089imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003090{
3091 char *name;
3092 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003093 PyObject *fob = NULL;
3094 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003095 FILE *fp;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003096 if (!PyArg_ParseTuple(args, "ses|O:load_source",
3097 &name,
3098 Py_FileSystemDefaultEncoding, &pathname,
3099 &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003100 return NULL;
3101 fp = get_file(pathname, fob, "r");
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003102 if (fp == NULL) {
3103 PyMem_Free(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003104 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003105 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003106 m = load_source_module(name, pathname, fp);
Kristján Valur Jónsson92af5d92009-03-31 17:47:50 +00003107 PyMem_Free(pathname);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003108 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003109 return m;
3110}
3111
Guido van Rossum79f25d91997-04-29 20:08:16 +00003112static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003113imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003114{
3115 char *name;
3116 PyObject *fob;
3117 char *pathname;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003118 PyObject * ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003119 char *suffix; /* Unused */
3120 char *mode;
3121 int type;
3122 FILE *fp;
3123
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003124 if (!PyArg_ParseTuple(args, "sOes(ssi):load_module",
3125 &name, &fob,
3126 Py_FileSystemDefaultEncoding, &pathname,
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003127 &suffix, &mode, &type))
3128 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003129 if (*mode) {
3130 /* Mode must start with 'r' or 'U' and must not contain '+'.
3131 Implicit in this test is the assumption that the mode
3132 may contain other modifiers like 'b' or 't'. */
3133
3134 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00003135 PyErr_Format(PyExc_ValueError,
3136 "invalid file open mode %.200s", mode);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003137 PyMem_Free(pathname);
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00003138 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003139 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003140 }
3141 if (fob == Py_None)
3142 fp = NULL;
3143 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003144 fp = get_file(NULL, fob, mode);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003145 if (fp == NULL) {
3146 PyMem_Free(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003147 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003148 }
3149 }
3150 ret = load_module(name, fp, pathname, type, NULL);
3151 PyMem_Free(pathname);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003152 if (fp)
3153 fclose(fp);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003154 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003155}
3156
3157static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003158imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003159{
3160 char *name;
3161 char *pathname;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003162 PyObject * ret;
3163 if (!PyArg_ParseTuple(args, "ses:load_package",
3164 &name, Py_FileSystemDefaultEncoding, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003165 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003166 ret = load_package(name, pathname);
3167 PyMem_Free(pathname);
3168 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003169}
3170
3171static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003172imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003173{
3174 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00003175 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003176 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003177 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003178}
3179
Christian Heimes13a7a212008-01-07 17:13:09 +00003180static PyObject *
3181imp_reload(PyObject *self, PyObject *v)
3182{
3183 return PyImport_ReloadModule(v);
3184}
3185
3186PyDoc_STRVAR(doc_reload,
3187"reload(module) -> module\n\
3188\n\
3189Reload the module. The module must have been successfully imported before.");
3190
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003191/* Doc strings */
3192
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003193PyDoc_STRVAR(doc_imp,
3194"This module provides the components needed to build your own\n\
3195__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003196
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003197PyDoc_STRVAR(doc_find_module,
3198"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003199Search for a module. If path is omitted or None, search for a\n\
3200built-in, frozen or special module and continue search in sys.path.\n\
3201The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003202package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003204PyDoc_STRVAR(doc_load_module,
3205"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003206Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003207The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003208
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003209PyDoc_STRVAR(doc_get_magic,
3210"get_magic() -> string\n\
3211Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003213PyDoc_STRVAR(doc_get_suffixes,
3214"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003215Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003216that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003217
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003218PyDoc_STRVAR(doc_new_module,
3219"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003220Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003221The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003223PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00003224"lock_held() -> boolean\n\
3225Return True if the import lock is currently held, else False.\n\
3226On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00003227
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003228PyDoc_STRVAR(doc_acquire_lock,
3229"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00003230Acquires the interpreter's import lock for the current thread.\n\
3231This lock should be used by import hooks to ensure thread-safety\n\
3232when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003233On platforms without threads, this function does nothing.");
3234
3235PyDoc_STRVAR(doc_release_lock,
3236"release_lock() -> None\n\
3237Release the interpreter's import lock.\n\
3238On platforms without threads, this function does nothing.");
3239
Guido van Rossum79f25d91997-04-29 20:08:16 +00003240static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00003241 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3242 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3243 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3244 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3245 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3246 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3247 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3248 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Christian Heimes13a7a212008-01-07 17:13:09 +00003249 {"reload", imp_reload, METH_O, doc_reload},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003250 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00003251 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
Brett Cannon8d110132009-03-15 02:20:16 +00003252 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00003253 {"init_builtin", imp_init_builtin, METH_VARARGS},
3254 {"init_frozen", imp_init_frozen, METH_VARARGS},
3255 {"is_builtin", imp_is_builtin, METH_VARARGS},
3256 {"is_frozen", imp_is_frozen, METH_VARARGS},
3257 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003258#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00003259 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003260#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00003261 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00003262 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003263 {NULL, NULL} /* sentinel */
3264};
3265
Guido van Rossum1a8791e1998-08-04 22:46:29 +00003266static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003267setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003268{
3269 PyObject *v;
3270 int err;
3271
Christian Heimes217cfd12007-12-02 14:31:20 +00003272 v = PyLong_FromLong((long)value);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003273 err = PyDict_SetItemString(d, name, v);
3274 Py_XDECREF(v);
3275 return err;
3276}
3277
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003278typedef struct {
3279 PyObject_HEAD
3280} NullImporter;
3281
3282static int
3283NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3284{
3285 char *path;
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003286 Py_ssize_t pathlen;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003287
3288 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3289 return -1;
3290
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00003291 if (!PyArg_ParseTuple(args, "es:NullImporter",
3292 Py_FileSystemDefaultEncoding, &path))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003293 return -1;
3294
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003295 pathlen = strlen(path);
3296 if (pathlen == 0) {
Georg Brandl8494d572008-07-19 10:13:15 +00003297 PyMem_Free(path);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003298 PyErr_SetString(PyExc_ImportError, "empty pathname");
3299 return -1;
3300 } else {
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003301#ifndef MS_WINDOWS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003302 struct stat statbuf;
3303 int rv;
3304
3305 rv = stat(path, &statbuf);
Georg Brandl8494d572008-07-19 10:13:15 +00003306 PyMem_Free(path);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003307 if (rv == 0) {
3308 /* it exists */
3309 if (S_ISDIR(statbuf.st_mode)) {
3310 /* it's a directory */
3311 PyErr_SetString(PyExc_ImportError,
3312 "existing directory");
3313 return -1;
3314 }
3315 }
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003316#else /* MS_WINDOWS */
3317 DWORD rv;
3318 /* see issue1293 and issue3677:
3319 * stat() on Windows doesn't recognise paths like
3320 * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
3321 */
3322 rv = GetFileAttributesA(path);
Kristján Valur Jónsson92cb4382009-01-24 10:33:25 +00003323 PyMem_Free(path);
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003324 if (rv != INVALID_FILE_ATTRIBUTES) {
3325 /* it exists */
3326 if (rv & FILE_ATTRIBUTE_DIRECTORY) {
3327 /* it's a directory */
3328 PyErr_SetString(PyExc_ImportError,
3329 "existing directory");
3330 return -1;
3331 }
3332 }
3333#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003334 }
3335 return 0;
3336}
3337
3338static PyObject *
3339NullImporter_find_module(NullImporter *self, PyObject *args)
3340{
3341 Py_RETURN_NONE;
3342}
3343
3344static PyMethodDef NullImporter_methods[] = {
3345 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3346 "Always return None"
3347 },
3348 {NULL} /* Sentinel */
3349};
3350
3351
Christian Heimes9cd17752007-11-18 19:35:23 +00003352PyTypeObject PyNullImporter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003353 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003354 "imp.NullImporter", /*tp_name*/
3355 sizeof(NullImporter), /*tp_basicsize*/
3356 0, /*tp_itemsize*/
3357 0, /*tp_dealloc*/
3358 0, /*tp_print*/
3359 0, /*tp_getattr*/
3360 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00003361 0, /*tp_reserved*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003362 0, /*tp_repr*/
3363 0, /*tp_as_number*/
3364 0, /*tp_as_sequence*/
3365 0, /*tp_as_mapping*/
3366 0, /*tp_hash */
3367 0, /*tp_call*/
3368 0, /*tp_str*/
3369 0, /*tp_getattro*/
3370 0, /*tp_setattro*/
3371 0, /*tp_as_buffer*/
3372 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3373 "Null importer object", /* tp_doc */
3374 0, /* tp_traverse */
3375 0, /* tp_clear */
3376 0, /* tp_richcompare */
3377 0, /* tp_weaklistoffset */
3378 0, /* tp_iter */
3379 0, /* tp_iternext */
3380 NullImporter_methods, /* tp_methods */
3381 0, /* tp_members */
3382 0, /* tp_getset */
3383 0, /* tp_base */
3384 0, /* tp_dict */
3385 0, /* tp_descr_get */
3386 0, /* tp_descr_set */
3387 0, /* tp_dictoffset */
3388 (initproc)NullImporter_init, /* tp_init */
3389 0, /* tp_alloc */
3390 PyType_GenericNew /* tp_new */
3391};
3392
Martin v. Löwis1a214512008-06-11 05:26:20 +00003393static struct PyModuleDef impmodule = {
3394 PyModuleDef_HEAD_INIT,
3395 "imp",
3396 doc_imp,
3397 0,
3398 imp_methods,
3399 NULL,
3400 NULL,
3401 NULL,
3402 NULL
3403};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003404
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003405PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003406PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003407{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003408 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003409
Christian Heimes9cd17752007-11-18 19:35:23 +00003410 if (PyType_Ready(&PyNullImporter_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003411 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003412
Martin v. Löwis1a214512008-06-11 05:26:20 +00003413 m = PyModule_Create(&impmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003414 if (m == NULL)
3415 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003416 d = PyModule_GetDict(m);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003417 if (d == NULL)
3418 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003419
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003420 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3421 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3422 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3423 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3424 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3425 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3426 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3427 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003428 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003429 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003430
Christian Heimes9cd17752007-11-18 19:35:23 +00003431 Py_INCREF(&PyNullImporter_Type);
3432 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
Martin v. Löwis1a214512008-06-11 05:26:20 +00003433 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003434 failure:
Martin v. Löwis1a214512008-06-11 05:26:20 +00003435 Py_XDECREF(m);
3436 return NULL;
3437
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003438}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003439
3440
Guido van Rossumb18618d2000-05-03 23:44:39 +00003441/* API for embedding applications that want to add their own entries
3442 to the table of built-in modules. This should normally be called
3443 *before* Py_Initialize(). When the table resize fails, -1 is
3444 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003445
3446 After a similar function by Just van Rossum. */
3447
3448int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003449PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003450{
3451 static struct _inittab *our_copy = NULL;
3452 struct _inittab *p;
3453 int i, n;
3454
3455 /* Count the number of entries in both tables */
3456 for (n = 0; newtab[n].name != NULL; n++)
3457 ;
3458 if (n == 0)
3459 return 0; /* Nothing to do */
3460 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3461 ;
3462
3463 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003464 p = our_copy;
3465 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003466 if (p == NULL)
3467 return -1;
3468
3469 /* Copy the tables into the new memory */
3470 if (our_copy != PyImport_Inittab)
3471 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3472 PyImport_Inittab = our_copy = p;
3473 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3474
3475 return 0;
3476}
3477
3478/* Shorthand to add a single entry given a name and a function */
3479
3480int
Brett Cannona826f322009-04-02 03:41:46 +00003481PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003482{
3483 struct _inittab newtab[2];
3484
3485 memset(newtab, '\0', sizeof newtab);
3486
Brett Cannona826f322009-04-02 03:41:46 +00003487 newtab[0].name = (char *)name;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003488 newtab[0].initfunc = initfunc;
3489
3490 return PyImport_ExtendInittab(newtab);
3491}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003492
3493#ifdef __cplusplus
3494}
3495#endif