blob: 62142aea26735022856aae1af636622a3e29ecc5 [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)
Guido van Rossum00bc0e02007-10-15 02:52:41 +000081 3070 (PEP 3109 raise changes)
82 3080 (PEP 3137 make __file__ and __name__ unicode)
Guido van Rossum98297ee2007-11-06 21:34:58 +000083 3090 (kill str8 interning)
Christian Heimes99170a52007-12-19 02:07:34 +000084 3100 (merge from 2.6a0, see 62151)
Christian Heimes3b06e532008-01-07 20:12:44 +000085 3102 (__file__ points to source file)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000086 Python 3.0a4: 3110 (WITH_CLEANUP optimization).
Benjamin Petersoneec3d712008-06-11 15:59:43 +000087 Python 3.0a5: 3130 (lexical exception stacking, including POP_EXCEPT)
Antoine Pitrouf289ae62008-12-18 11:06:25 +000088 Python 3.1a0: 3140 (optimize list, set and dict comprehensions:
89 change LIST_APPEND and SET_ADD, add MAP_ADD)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +000090 Python 3.1a0: 3150 (optimize conditional branches:
91 introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
Benjamin Peterson876b2f22009-06-28 03:18:59 +000092 Python 3.2a0: 3160 (add SETUP_WITH)
Tim Peters36515e22001-11-18 04:06:29 +000093*/
Guido van Rossum3ddee711991-12-16 13:06:34 +000094
Benjamin Peterson876b2f22009-06-28 03:18:59 +000095#define MAGIC (3160 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum96774c12000-05-01 20:19:08 +000096/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000097 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000098 compiler works which are enabled by command line switches. */
99static long pyc_magic = MAGIC;
100
Guido van Rossum25ce5661997-08-02 03:10:38 +0000101/* See _PyImport_FixupExtension() below */
102static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103
Guido van Rossum771c6c81997-10-31 18:37:24 +0000104/* This table is defined in config.c: */
105extern struct _inittab _PyImport_Inittab[];
106
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000107/* Method from Parser/tokenizer.c */
Guido van Rossum40d20bc2007-10-22 00:09:51 +0000108extern char * PyTokenizer_FindEncoding(int);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000109
Guido van Rossum771c6c81997-10-31 18:37:24 +0000110struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +0000111
Guido van Rossumed1170e1999-12-20 21:23:41 +0000112/* these tables define the module suffixes that Python recognizes */
113struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000114
Guido van Rossumed1170e1999-12-20 21:23:41 +0000115static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +0000116 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000117#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +0000118 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000119#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000120 {".pyc", "rb", PY_COMPILED},
121 {0, 0}
122};
123
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000124
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000125/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126
127void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000128_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000130 const struct filedescr *scan;
131 struct filedescr *filetab;
132 int countD = 0;
133 int countS = 0;
134
135 /* prepare _PyImport_Filetab: copy entries from
136 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
137 */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000138#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000139 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
140 ++countD;
Guido van Rossum04110fb2007-08-24 16:32:05 +0000141#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000142 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
143 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000144 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000145 if (filetab == NULL)
146 Py_FatalError("Can't initialize import file table.");
Guido van Rossum04110fb2007-08-24 16:32:05 +0000147#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000148 memcpy(filetab, _PyImport_DynLoadFiletab,
149 countD * sizeof(struct filedescr));
Guido van Rossum04110fb2007-08-24 16:32:05 +0000150#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000151 memcpy(filetab + countD, _PyImport_StandardFiletab,
152 countS * sizeof(struct filedescr));
153 filetab[countD + countS].suffix = NULL;
154
155 _PyImport_Filetab = filetab;
156
Guido van Rossum0824f631997-03-11 18:37:35 +0000157 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000158 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
159 for (; filetab->suffix != NULL; filetab++) {
160 if (strcmp(filetab->suffix, ".pyc") == 0)
161 filetab->suffix = ".pyo";
Guido van Rossum0824f631997-03-11 18:37:35 +0000162 }
163 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000164
Guido van Rossum572dbf82007-04-27 23:53:51 +0000165 {
Guido van Rossum96774c12000-05-01 20:19:08 +0000166 /* Fix the pyc_magic so that byte compiled code created
167 using the all-Unicode method doesn't interfere with
168 code created in normal operation mode. */
169 pyc_magic = MAGIC + 1;
170 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171}
172
Guido van Rossum25ce5661997-08-02 03:10:38 +0000173void
Just van Rossum52e14d62002-12-30 22:08:05 +0000174_PyImportHooks_Init(void)
175{
176 PyObject *v, *path_hooks = NULL, *zimpimport;
177 int err = 0;
178
179 /* adding sys.path_hooks and sys.path_importer_cache, setting up
180 zipimport */
Christian Heimes9cd17752007-11-18 19:35:23 +0000181 if (PyType_Ready(&PyNullImporter_Type) < 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000182 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000183
184 if (Py_VerboseFlag)
185 PySys_WriteStderr("# installing zipimport hook\n");
186
187 v = PyList_New(0);
188 if (v == NULL)
189 goto error;
190 err = PySys_SetObject("meta_path", v);
191 Py_DECREF(v);
192 if (err)
193 goto error;
194 v = PyDict_New();
195 if (v == NULL)
196 goto error;
197 err = PySys_SetObject("path_importer_cache", v);
198 Py_DECREF(v);
199 if (err)
200 goto error;
201 path_hooks = PyList_New(0);
202 if (path_hooks == NULL)
203 goto error;
204 err = PySys_SetObject("path_hooks", path_hooks);
205 if (err) {
206 error:
207 PyErr_Print();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000208 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
209 "path_importer_cache, or NullImporter failed"
210 );
Just van Rossum52e14d62002-12-30 22:08:05 +0000211 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000212
Just van Rossum52e14d62002-12-30 22:08:05 +0000213 zimpimport = PyImport_ImportModule("zipimport");
214 if (zimpimport == NULL) {
215 PyErr_Clear(); /* No zip import module -- okay */
216 if (Py_VerboseFlag)
217 PySys_WriteStderr("# can't import zipimport\n");
218 }
219 else {
220 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
221 "zipimporter");
222 Py_DECREF(zimpimport);
223 if (zipimporter == NULL) {
224 PyErr_Clear(); /* No zipimporter object -- okay */
225 if (Py_VerboseFlag)
226 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000227 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000228 }
229 else {
230 /* sys.path_hooks.append(zipimporter) */
231 err = PyList_Append(path_hooks, zipimporter);
232 Py_DECREF(zipimporter);
233 if (err)
234 goto error;
235 if (Py_VerboseFlag)
236 PySys_WriteStderr(
237 "# installed zipimport hook\n");
238 }
239 }
240 Py_DECREF(path_hooks);
241}
242
243void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000244_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000245{
246 Py_XDECREF(extensions);
247 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000248 PyMem_DEL(_PyImport_Filetab);
249 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000250}
251
252
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000253/* Locking primitives to prevent parallel imports of the same module
254 in different threads to return with a partially loaded module.
255 These calls are serialized by the global interpreter lock. */
256
257#ifdef WITH_THREAD
258
Guido van Rossum49b56061998-10-01 20:42:43 +0000259#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000260
Guido van Rossum65d5b571998-12-21 19:32:43 +0000261static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000262static long import_lock_thread = -1;
263static int import_lock_level = 0;
264
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000265void
266_PyImport_AcquireLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000267{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000268 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000269 if (me == -1)
270 return; /* Too bad */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000271 if (import_lock == NULL) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000272 import_lock = PyThread_allocate_lock();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000273 if (import_lock == NULL)
274 return; /* Nothing much we can do. */
275 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000276 if (import_lock_thread == me) {
277 import_lock_level++;
278 return;
279 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000280 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
281 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000282 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000283 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000284 PyEval_RestoreThread(tstate);
285 }
286 import_lock_thread = me;
287 import_lock_level = 1;
288}
289
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000290int
291_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000292{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000293 long me = PyThread_get_thread_ident();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000294 if (me == -1 || import_lock == NULL)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000295 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000296 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000297 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000298 import_lock_level--;
299 if (import_lock_level == 0) {
300 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000301 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000302 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000303 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000304}
305
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000306/* This function used to be called from PyOS_AfterFork to ensure that newly
307 created child processes do not share locks with the parent, but for some
308 reason only on AIX systems. Instead of re-initializing the lock, we now
309 acquire the import lock around fork() calls. */
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000310
311void
312_PyImport_ReInitLock(void)
313{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000314}
315
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000316#endif
317
Tim Peters69232342001-08-30 05:16:13 +0000318static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000319imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000320{
Tim Peters69232342001-08-30 05:16:13 +0000321#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000322 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000323#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000324 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000325#endif
326}
327
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000328static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000329imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000330{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000331#ifdef WITH_THREAD
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000332 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000333#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000334 Py_INCREF(Py_None);
335 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000336}
337
338static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000339imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000340{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000341#ifdef WITH_THREAD
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000342 if (_PyImport_ReleaseLock() < 0) {
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000343 PyErr_SetString(PyExc_RuntimeError,
344 "not holding the import lock");
345 return NULL;
346 }
347#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000348 Py_INCREF(Py_None);
349 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000350}
351
Guido van Rossumd8faa362007-04-27 19:54:29 +0000352static void
353imp_modules_reloading_clear(void)
354{
355 PyInterpreterState *interp = PyThreadState_Get()->interp;
356 if (interp->modules_reloading != NULL)
357 PyDict_Clear(interp->modules_reloading);
358}
359
Guido van Rossum25ce5661997-08-02 03:10:38 +0000360/* Helper for sys */
361
362PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000363PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000364{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000365 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000366 if (interp->modules == NULL)
367 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
368 return interp->modules;
369}
370
Guido van Rossum3f5da241990-12-20 15:06:42 +0000371
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000372/* List of names to clear in sys */
373static char* sys_deletes[] = {
Collin Winter670e6922007-03-21 02:57:17 +0000374 "path", "argv", "ps1", "ps2",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000375 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000376 "path_hooks", "path_importer_cache", "meta_path",
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000377 /* misc stuff */
378 "flags", "float_info",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000379 NULL
380};
381
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000382static char* sys_files[] = {
383 "stdin", "__stdin__",
384 "stdout", "__stdout__",
385 "stderr", "__stderr__",
386 NULL
387};
388
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000389
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000390/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000391
Guido van Rossum3f5da241990-12-20 15:06:42 +0000392void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000393PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000394{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000395 Py_ssize_t pos, ndone;
Guido van Rossum758eec01998-01-19 21:58:26 +0000396 char *name;
397 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000398 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000399 PyObject *modules = interp->modules;
400
401 if (modules == NULL)
402 return; /* Already done */
403
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000404 /* Delete some special variables first. These are common
405 places where user values hide and people complain when their
406 destructors fail. Since the modules containing them are
407 deleted *last* of all, they would come too late in the normal
408 destruction order. Sigh. */
409
Georg Brandl1a3284e2007-12-02 09:40:06 +0000410 value = PyDict_GetItemString(modules, "builtins");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000411 if (value != NULL && PyModule_Check(value)) {
412 dict = PyModule_GetDict(value);
413 if (Py_VerboseFlag)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000414 PySys_WriteStderr("# clear builtins._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000415 PyDict_SetItemString(dict, "_", Py_None);
416 }
417 value = PyDict_GetItemString(modules, "sys");
418 if (value != NULL && PyModule_Check(value)) {
419 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000420 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000421 dict = PyModule_GetDict(value);
422 for (p = sys_deletes; *p != NULL; p++) {
423 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000424 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000425 PyDict_SetItemString(dict, *p, Py_None);
426 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000427 for (p = sys_files; *p != NULL; p+=2) {
428 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000429 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000430 v = PyDict_GetItemString(dict, *(p+1));
431 if (v == NULL)
432 v = Py_None;
433 PyDict_SetItemString(dict, *p, v);
434 }
435 }
436
437 /* First, delete __main__ */
438 value = PyDict_GetItemString(modules, "__main__");
439 if (value != NULL && PyModule_Check(value)) {
440 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000441 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000442 _PyModule_Clear(value);
443 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000444 }
445
Georg Brandl1a3284e2007-12-02 09:40:06 +0000446 /* The special treatment of "builtins" here is because even
Guido van Rossum758eec01998-01-19 21:58:26 +0000447 when it's not referenced as a module, its dictionary is
448 referenced by almost every module's __builtins__. Since
449 deleting a module clears its dictionary (even if there are
Georg Brandl1a3284e2007-12-02 09:40:06 +0000450 references left to it), we need to delete the "builtins"
Guido van Rossum758eec01998-01-19 21:58:26 +0000451 module last. Likewise, we don't delete sys until the very
452 end because it is implicitly referenced (e.g. by print).
453
454 Also note that we 'delete' modules by replacing their entry
455 in the modules dict with None, rather than really deleting
456 them; this avoids a rehash of the modules dictionary and
457 also marks them as "non existent" so they won't be
458 re-imported. */
459
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000460 /* Next, repeatedly delete modules with a reference count of
Georg Brandl1a3284e2007-12-02 09:40:06 +0000461 one (skipping builtins and sys) and delete them */
Guido van Rossum758eec01998-01-19 21:58:26 +0000462 do {
463 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000464 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000465 while (PyDict_Next(modules, &pos, &key, &value)) {
466 if (value->ob_refcnt != 1)
467 continue;
Neal Norwitz80e7f272007-08-26 06:45:23 +0000468 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000469 name = _PyUnicode_AsString(key);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000470 if (strcmp(name, "builtins") == 0)
Guido van Rossum758eec01998-01-19 21:58:26 +0000471 continue;
472 if (strcmp(name, "sys") == 0)
473 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000474 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000475 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000476 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000477 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000478 PyDict_SetItem(modules, key, Py_None);
479 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000480 }
481 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000482 } while (ndone > 0);
483
Georg Brandl1a3284e2007-12-02 09:40:06 +0000484 /* Next, delete all modules (still skipping builtins and sys) */
Guido van Rossum758eec01998-01-19 21:58:26 +0000485 pos = 0;
486 while (PyDict_Next(modules, &pos, &key, &value)) {
Neal Norwitz80e7f272007-08-26 06:45:23 +0000487 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000488 name = _PyUnicode_AsString(key);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000489 if (strcmp(name, "builtins") == 0)
Guido van Rossum758eec01998-01-19 21:58:26 +0000490 continue;
491 if (strcmp(name, "sys") == 0)
492 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000493 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000494 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000495 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000496 PyDict_SetItem(modules, key, Py_None);
497 }
498 }
499
Georg Brandl1a3284e2007-12-02 09:40:06 +0000500 /* Next, delete sys and builtins (in that order) */
Guido van Rossum758eec01998-01-19 21:58:26 +0000501 value = PyDict_GetItemString(modules, "sys");
502 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000503 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000504 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000505 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000506 PyDict_SetItemString(modules, "sys", Py_None);
507 }
Georg Brandl1a3284e2007-12-02 09:40:06 +0000508 value = PyDict_GetItemString(modules, "builtins");
Guido van Rossum758eec01998-01-19 21:58:26 +0000509 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000510 if (Py_VerboseFlag)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000511 PySys_WriteStderr("# cleanup builtins\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000512 _PyModule_Clear(value);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000513 PyDict_SetItemString(modules, "builtins", Py_None);
Guido van Rossum758eec01998-01-19 21:58:26 +0000514 }
515
516 /* Finally, clear and delete the modules directory */
517 PyDict_Clear(modules);
518 interp->modules = NULL;
519 Py_DECREF(modules);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000520 Py_CLEAR(interp->modules_reloading);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000521}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000522
523
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000524/* Helper for pythonrun.c -- return magic number */
525
526long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000527PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000528{
Guido van Rossum96774c12000-05-01 20:19:08 +0000529 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000530}
531
532
Guido van Rossum25ce5661997-08-02 03:10:38 +0000533/* Magic for extension modules (built-in as well as dynamically
534 loaded). To prevent initializing an extension module more than
535 once, we keep a static dictionary 'extensions' keyed by module name
536 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000537 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000538 dictionary is stored by calling _PyImport_FixupExtension()
539 immediately after the module initialization function succeeds. A
540 copy can be retrieved from there by calling
Martin v. Löwis1a214512008-06-11 05:26:20 +0000541 _PyImport_FindExtension().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000542
Martin v. Löwis1a214512008-06-11 05:26:20 +0000543 Modules which do support multiple multiple initialization set
544 their m_size field to a non-negative number (indicating the size
545 of the module-specific state). They are still recorded in the
546 extensions dictionary, to avoid loading shared libraries twice.
547*/
548
549int
550_PyImport_FixupExtension(PyObject *mod, char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000551{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000552 PyObject *modules, *dict;
553 struct PyModuleDef *def;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000554 if (extensions == NULL) {
555 extensions = PyDict_New();
556 if (extensions == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000557 return -1;
558 }
559 if (mod == NULL || !PyModule_Check(mod)) {
560 PyErr_BadInternalCall();
561 return -1;
562 }
563 def = PyModule_GetDef(mod);
564 if (!def) {
565 PyErr_BadInternalCall();
566 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000567 }
568 modules = PyImport_GetModuleDict();
Martin v. Löwis1a214512008-06-11 05:26:20 +0000569 if (PyDict_SetItemString(modules, name, mod) < 0)
570 return -1;
571 if (_PyState_AddModule(mod, def) < 0) {
572 PyDict_DelItemString(modules, name);
573 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000574 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000575 if (def->m_size == -1) {
576 if (def->m_base.m_copy) {
577 /* Somebody already imported the module,
578 likely under a different name.
579 XXX this should really not happen. */
580 Py_DECREF(def->m_base.m_copy);
581 def->m_base.m_copy = NULL;
582 }
583 dict = PyModule_GetDict(mod);
584 if (dict == NULL)
585 return -1;
586 def->m_base.m_copy = PyDict_Copy(dict);
587 if (def->m_base.m_copy == NULL)
588 return -1;
589 }
590 PyDict_SetItemString(extensions, filename, (PyObject*)def);
591 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000592}
593
594PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000595_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000596{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000597 PyObject *mod, *mdict;
598 PyModuleDef* def;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000599 if (extensions == NULL)
600 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000601 def = (PyModuleDef*)PyDict_GetItemString(extensions, filename);
602 if (def == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000603 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000604 if (def->m_size == -1) {
605 /* Module does not support repeated initialization */
606 if (def->m_base.m_copy == NULL)
607 return NULL;
608 mod = PyImport_AddModule(name);
609 if (mod == NULL)
610 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000611 mdict = PyModule_GetDict(mod);
612 if (mdict == NULL)
613 return NULL;
614 if (PyDict_Update(mdict, def->m_base.m_copy))
615 return NULL;
616 }
617 else {
618 if (def->m_base.m_init == NULL)
619 return NULL;
620 mod = def->m_base.m_init();
621 if (mod == NULL)
622 return NULL;
623 PyDict_SetItemString(PyImport_GetModuleDict(), name, mod);
Benjamin Petersonad956532008-09-04 02:28:15 +0000624 Py_DECREF(mod);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000625 }
626 if (_PyState_AddModule(mod, def) < 0) {
627 PyDict_DelItemString(PyImport_GetModuleDict(), name);
628 Py_DECREF(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000629 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000630 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000631 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000632 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Martin v. Löwis1a214512008-06-11 05:26:20 +0000633 name, filename);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000634 return mod;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000635
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000636}
637
638
639/* Get the module object corresponding to a module name.
640 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000641 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000642 Because the former action is most common, THIS DOES NOT RETURN A
643 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000644
Guido van Rossum79f25d91997-04-29 20:08:16 +0000645PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000646PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000648 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000650
Guido van Rossum25ce5661997-08-02 03:10:38 +0000651 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000652 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000653 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655 if (m == NULL)
656 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000657 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000658 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000659 return NULL;
660 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000661 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000662
663 return m;
664}
665
Tim Peters1cd70172004-08-02 03:52:12 +0000666/* Remove name from sys.modules, if it's there. */
667static void
668_RemoveModule(const char *name)
669{
670 PyObject *modules = PyImport_GetModuleDict();
671 if (PyDict_GetItemString(modules, name) == NULL)
672 return;
673 if (PyDict_DelItemString(modules, name) < 0)
674 Py_FatalError("import: deleting existing key in"
675 "sys.modules failed");
676}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000677
Christian Heimes3b06e532008-01-07 20:12:44 +0000678static PyObject * get_sourcefile(const char *file);
679
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000680/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000681 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
682 * removed from sys.modules, to avoid leaving damaged module objects
683 * in sys.modules. The caller may wish to restore the original
684 * module object (if any) in this case; PyImport_ReloadModule is an
685 * example.
686 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000687PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000688PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000689{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000690 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
691}
692
693PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000694PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000695{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000696 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000697 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000698
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000700 if (m == NULL)
701 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000702 /* If the module is being reloaded, we get the old module back
703 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000704 d = PyModule_GetDict(m);
705 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
706 if (PyDict_SetItemString(d, "__builtins__",
707 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000708 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000709 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000710 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000711 v = NULL;
712 if (pathname != NULL) {
Christian Heimes3b06e532008-01-07 20:12:44 +0000713 v = get_sourcefile(pathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000714 if (v == NULL)
715 PyErr_Clear();
716 }
717 if (v == NULL) {
718 v = ((PyCodeObject *)co)->co_filename;
719 Py_INCREF(v);
720 }
721 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000723 Py_DECREF(v);
724
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000725 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000726 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000727 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000728 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000729
Guido van Rossum25ce5661997-08-02 03:10:38 +0000730 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000731 PyErr_Format(PyExc_ImportError,
732 "Loaded module %.200s not found in sys.modules",
733 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000734 return NULL;
735 }
736
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000738
739 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000740
741 error:
742 _RemoveModule(name);
743 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000744}
745
746
747/* Given a pathname for a Python source file, fill a buffer with the
748 pathname for the corresponding compiled file. Return the pathname
749 for the compiled file, or NULL if there's no space in the buffer.
750 Doesn't set an exception. */
751
752static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000753make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000754{
Tim Petersc1731372001-08-04 08:12:36 +0000755 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000756 if (len+2 > buflen)
757 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000758
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000759#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000760 /* Treat .pyw as if it were .py. The case of ".pyw" must match
761 that used in _PyImport_StandardFiletab. */
762 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
763 --len; /* pretend 'w' isn't there */
764#endif
765 memcpy(buf, pathname, len);
766 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
767 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000768
769 return buf;
770}
771
772
773/* Given a pathname for a Python source file, its time of last
774 modification, and a pathname for a compiled file, check whether the
775 compiled file represents the same version of the source. If so,
776 return a FILE pointer for the compiled file, positioned just after
777 the header; if not, return NULL.
778 Doesn't set an exception. */
779
780static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000781check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000782{
783 FILE *fp;
784 long magic;
785 long pyc_mtime;
786
787 fp = fopen(cpathname, "rb");
788 if (fp == NULL)
789 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000791 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000792 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000793 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000794 fclose(fp);
795 return NULL;
796 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000797 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000798 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000799 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000800 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000801 fclose(fp);
802 return NULL;
803 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000804 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000805 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000806 return fp;
807}
808
809
810/* Read a code object from a file and check it for validity */
811
Guido van Rossum79f25d91997-04-29 20:08:16 +0000812static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000813read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000814{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000815 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000816
Tim Petersd9b9ac82001-01-28 00:27:39 +0000817 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000818 if (co == NULL)
819 return NULL;
820 if (!PyCode_Check(co)) {
821 PyErr_Format(PyExc_ImportError,
822 "Non-code object in %.200s", cpathname);
823 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000824 return NULL;
825 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000826 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000827}
828
829
830/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000831 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000832
Guido van Rossum79f25d91997-04-29 20:08:16 +0000833static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000834load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000835{
836 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000837 PyCodeObject *co;
838 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000839
Guido van Rossum79f25d91997-04-29 20:08:16 +0000840 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000841 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000842 PyErr_Format(PyExc_ImportError,
843 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000844 return NULL;
845 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000847 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000848 if (co == NULL)
849 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000850 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000851 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000852 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000853 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000855
856 return m;
857}
858
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000859/* Parse a source file and return the corresponding code object */
860
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000863{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 PyCodeObject *co = NULL;
865 mod_ty mod;
Christian Heimes4d6ec852008-03-26 22:34:47 +0000866 PyCompilerFlags flags;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000867 PyArena *arena = PyArena_New();
868 if (arena == NULL)
869 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000870
Christian Heimesb1b3efc2008-03-26 23:24:27 +0000871 flags.cf_flags = 0;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000872 mod = PyParser_ASTFromFile(fp, pathname, NULL,
Christian Heimes4d6ec852008-03-26 22:34:47 +0000873 Py_file_input, 0, 0, &flags,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000874 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000876 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000878 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000879 return co;
880}
881
882
Guido van Rossum55a83382000-09-20 20:31:38 +0000883/* Helper to open a bytecode file for writing in exclusive mode */
884
885static FILE *
Christian Heimes05e8be12008-02-23 18:30:17 +0000886open_exclusive(char *filename, mode_t mode)
Guido van Rossum55a83382000-09-20 20:31:38 +0000887{
888#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
889 /* Use O_EXCL to avoid a race condition when another process tries to
890 write the same file. When that happens, our open() call fails,
891 which is just fine (since it's only a cache).
892 XXX If the file exists and is writable but the directory is not
893 writable, the file will never be written. Oh well.
894 */
895 int fd;
896 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000897 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
898#ifdef O_BINARY
899 |O_BINARY /* necessary for Windows */
900#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000901#ifdef __VMS
Christian Heimes05e8be12008-02-23 18:30:17 +0000902 , mode, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000903#else
Christian Heimes05e8be12008-02-23 18:30:17 +0000904 , mode
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000905#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000906 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000907 if (fd < 0)
908 return NULL;
909 return fdopen(fd, "wb");
910#else
911 /* Best we can do -- on Windows this can't happen anyway */
912 return fopen(filename, "wb");
913#endif
914}
915
916
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000917/* Write a compiled module to a file, placing the time of last
918 modification of its source into the header.
919 Errors are ignored, if a write error occurs an attempt is made to
920 remove the file. */
921
922static void
Christian Heimes05e8be12008-02-23 18:30:17 +0000923write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000924{
925 FILE *fp;
Christian Heimes05e8be12008-02-23 18:30:17 +0000926 time_t mtime = srcstat->st_mtime;
Alexandre Vassalotti9d58e3e2009-07-17 10:55:50 +0000927#ifdef MS_WINDOWS /* since Windows uses different permissions */
928 mode_t mode = srcstat->st_mode & ~S_IEXEC;
929#else
930 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;
931#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000932
Christian Heimes05e8be12008-02-23 18:30:17 +0000933 fp = open_exclusive(cpathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000934 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000935 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000936 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000937 "# can't create %s\n", cpathname);
938 return;
939 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000940 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000941 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000942 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
943 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000944 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000945 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000946 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000947 /* Don't keep partial file */
948 fclose(fp);
949 (void) unlink(cpathname);
950 return;
951 }
952 /* Now write the true mtime */
953 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000954 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000955 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000956 fflush(fp);
957 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000958 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000959 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000960}
961
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000962static void
963update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
964{
965 PyObject *constants, *tmp;
966 Py_ssize_t i, n;
967
968 if (PyUnicode_Compare(co->co_filename, oldname))
969 return;
970
971 tmp = co->co_filename;
972 co->co_filename = newname;
973 Py_INCREF(co->co_filename);
974 Py_DECREF(tmp);
975
976 constants = co->co_consts;
977 n = PyTuple_GET_SIZE(constants);
978 for (i = 0; i < n; i++) {
979 tmp = PyTuple_GET_ITEM(constants, i);
980 if (PyCode_Check(tmp))
981 update_code_filenames((PyCodeObject *)tmp,
982 oldname, newname);
983 }
984}
985
986static int
987update_compiled_module(PyCodeObject *co, char *pathname)
988{
989 PyObject *oldname, *newname;
990
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000991 newname = PyUnicode_DecodeFSDefault(pathname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000992 if (newname == NULL)
993 return -1;
994
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000995 if (!PyUnicode_Compare(co->co_filename, newname)) {
996 Py_DECREF(newname);
997 return 0;
998 }
999
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001000 oldname = co->co_filename;
1001 Py_INCREF(oldname);
1002 update_code_filenames(co, oldname, newname);
1003 Py_DECREF(oldname);
1004 Py_DECREF(newname);
1005 return 1;
1006}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001007
1008/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001009 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
1010 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001011
Guido van Rossum79f25d91997-04-29 20:08:16 +00001012static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001013load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001014{
Christian Heimes05e8be12008-02-23 18:30:17 +00001015 struct stat st;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001016 FILE *fpc;
1017 char buf[MAXPATHLEN+1];
1018 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001019 PyCodeObject *co;
1020 PyObject *m;
Christian Heimes05e8be12008-02-23 18:30:17 +00001021
1022 if (fstat(fileno(fp), &st) != 0) {
Neal Norwitz708e51a2005-10-03 04:48:15 +00001023 PyErr_Format(PyExc_RuntimeError,
Christian Heimes05e8be12008-02-23 18:30:17 +00001024 "unable to get file status from '%s'",
Neal Norwitz708e51a2005-10-03 04:48:15 +00001025 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +00001026 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +00001027 }
Fred Drake4c82b232000-06-30 16:18:57 +00001028#if SIZEOF_TIME_T > 4
1029 /* Python's .pyc timestamp handling presumes that the timestamp fits
1030 in 4 bytes. This will be fine until sometime in the year 2038,
1031 when a 4-byte signed time_t will overflow.
1032 */
Christian Heimes05e8be12008-02-23 18:30:17 +00001033 if (st.st_mtime >> 32) {
Fred Drake4c82b232000-06-30 16:18:57 +00001034 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +00001035 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +00001036 return NULL;
1037 }
1038#endif
Tim Peters36515e22001-11-18 04:06:29 +00001039 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +00001040 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001041 if (cpathname != NULL &&
Christian Heimes05e8be12008-02-23 18:30:17 +00001042 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001043 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001044 fclose(fpc);
1045 if (co == NULL)
1046 return NULL;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001047 if (update_compiled_module(co, pathname) < 0)
1048 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001049 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001050 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001051 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001052 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001053 }
1054 else {
1055 co = parse_source_module(pathname, fp);
1056 if (co == NULL)
1057 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001058 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001059 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001060 name, pathname);
Christian Heimes790c8232008-01-07 21:14:23 +00001061 if (cpathname) {
1062 PyObject *ro = PySys_GetObject("dont_write_bytecode");
1063 if (ro == NULL || !PyObject_IsTrue(ro))
Christian Heimes05e8be12008-02-23 18:30:17 +00001064 write_compiled_module(co, cpathname, &st);
Christian Heimes790c8232008-01-07 21:14:23 +00001065 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001066 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001067 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001068 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001069
1070 return m;
1071}
1072
Christian Heimes3b06e532008-01-07 20:12:44 +00001073/* Get source file -> unicode or None
1074 * Returns the path to the py file if available, else the given path
1075 */
1076static PyObject *
1077get_sourcefile(const char *file)
1078{
1079 char py[MAXPATHLEN + 1];
1080 Py_ssize_t len;
1081 PyObject *u;
1082 struct stat statbuf;
1083
1084 if (!file || !*file) {
1085 Py_RETURN_NONE;
1086 }
1087
1088 len = strlen(file);
Christian Heimes3540b502008-01-11 07:03:05 +00001089 /* match '*.py?' */
1090 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {
Christian Heimes3b06e532008-01-07 20:12:44 +00001091 return PyUnicode_DecodeFSDefault(file);
1092 }
1093
1094 strncpy(py, file, len-1);
Martin v. Löwis5021ebc2008-03-22 22:07:13 +00001095 py[len-1] = '\0';
Christian Heimes3b06e532008-01-07 20:12:44 +00001096 if (stat(py, &statbuf) == 0 &&
1097 S_ISREG(statbuf.st_mode)) {
1098 u = PyUnicode_DecodeFSDefault(py);
1099 }
1100 else {
1101 u = PyUnicode_DecodeFSDefault(file);
1102 }
1103 return u;
1104}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001105
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001106/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001107static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
1108static struct filedescr *find_module(char *, char *, PyObject *,
1109 char *, size_t, FILE **, PyObject **);
Christian Heimes3b06e532008-01-07 20:12:44 +00001110static struct _frozen * find_frozen(char *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001111
1112/* Load a package and return its module object WITH INCREMENTED
1113 REFERENCE COUNT */
1114
1115static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001116load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001117{
Tim Peters1cd70172004-08-02 03:52:12 +00001118 PyObject *m, *d;
1119 PyObject *file = NULL;
1120 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001121 int err;
1122 char buf[MAXPATHLEN+1];
1123 FILE *fp = NULL;
1124 struct filedescr *fdp;
1125
1126 m = PyImport_AddModule(name);
1127 if (m == NULL)
1128 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001129 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001130 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001131 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001132 d = PyModule_GetDict(m);
Christian Heimes3b06e532008-01-07 20:12:44 +00001133 file = get_sourcefile(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001134 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +00001135 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001136 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +00001137 if (path == NULL)
1138 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001139 err = PyDict_SetItemString(d, "__file__", file);
1140 if (err == 0)
1141 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +00001142 if (err != 0)
1143 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001144 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001145 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001146 if (fdp == NULL) {
1147 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1148 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +00001149 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001150 }
1151 else
1152 m = NULL;
1153 goto cleanup;
1154 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001155 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001156 if (fp != NULL)
1157 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001158 goto cleanup;
1159
1160 error:
1161 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001162 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001163 Py_XDECREF(path);
1164 Py_XDECREF(file);
1165 return m;
1166}
1167
1168
1169/* Helper to test for built-in module */
1170
1171static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001172is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001173{
1174 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001175 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1176 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1177 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001178 return -1;
1179 else
1180 return 1;
1181 }
1182 }
1183 return 0;
1184}
1185
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001186
Just van Rossum52e14d62002-12-30 22:08:05 +00001187/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1188 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001189 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001190 that can handle the path item. Return None if no hook could;
1191 this tells our caller it should fall back to the builtin
1192 import mechanism. Cache the result in path_importer_cache.
1193 Returns a borrowed reference. */
1194
1195static PyObject *
1196get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1197 PyObject *p)
1198{
1199 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001200 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001201
1202 /* These conditions are the caller's responsibility: */
1203 assert(PyList_Check(path_hooks));
1204 assert(PyDict_Check(path_importer_cache));
1205
1206 nhooks = PyList_Size(path_hooks);
1207 if (nhooks < 0)
1208 return NULL; /* Shouldn't happen */
1209
1210 importer = PyDict_GetItem(path_importer_cache, p);
1211 if (importer != NULL)
1212 return importer;
1213
1214 /* set path_importer_cache[p] to None to avoid recursion */
1215 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1216 return NULL;
1217
1218 for (j = 0; j < nhooks; j++) {
1219 PyObject *hook = PyList_GetItem(path_hooks, j);
1220 if (hook == NULL)
1221 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001222 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001223 if (importer != NULL)
1224 break;
1225
1226 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1227 return NULL;
1228 }
1229 PyErr_Clear();
1230 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001231 if (importer == NULL) {
1232 importer = PyObject_CallFunctionObjArgs(
Christian Heimes9cd17752007-11-18 19:35:23 +00001233 (PyObject *)&PyNullImporter_Type, p, NULL
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001234 );
1235 if (importer == NULL) {
1236 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1237 PyErr_Clear();
1238 return Py_None;
1239 }
1240 }
1241 }
1242 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001243 int err = PyDict_SetItem(path_importer_cache, p, importer);
1244 Py_DECREF(importer);
1245 if (err != 0)
1246 return NULL;
1247 }
1248 return importer;
1249}
1250
Christian Heimes9cd17752007-11-18 19:35:23 +00001251PyAPI_FUNC(PyObject *)
1252PyImport_GetImporter(PyObject *path) {
1253 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
1254
1255 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1256 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1257 importer = get_path_importer(path_importer_cache,
1258 path_hooks, path);
1259 }
1260 }
1261 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1262 return importer;
1263}
1264
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001265/* Search the path (default sys.path) for a module. Return the
1266 corresponding filedescr struct, and (via return arguments) the
1267 pathname and an open file. Return NULL if the module is not found. */
1268
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001269#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001270extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001271 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001272#endif
1273
Martin v. Löwis18e16552006-02-15 17:27:45 +00001274static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001275static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001276static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001277
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001278static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001279find_module(char *fullname, char *subname, PyObject *path, char *buf,
1280 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001281{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001282 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001283 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001284 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001285 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001286 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001287 PyObject *path_hooks, *path_importer_cache;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001288 struct stat statbuf;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001289 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1290 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1291 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001292 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001293#if defined(PYOS_OS2)
1294 size_t saved_len;
1295 size_t saved_namelen;
1296 char *saved_buf = NULL;
1297#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001298 if (p_loader != NULL)
1299 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001300
Just van Rossum52e14d62002-12-30 22:08:05 +00001301 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001302 PyErr_SetString(PyExc_OverflowError,
1303 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001304 return NULL;
1305 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001306 strcpy(name, subname);
1307
1308 /* sys.meta_path import hook */
1309 if (p_loader != NULL) {
1310 PyObject *meta_path;
1311
1312 meta_path = PySys_GetObject("meta_path");
1313 if (meta_path == NULL || !PyList_Check(meta_path)) {
1314 PyErr_SetString(PyExc_ImportError,
1315 "sys.meta_path must be a list of "
1316 "import hooks");
1317 return NULL;
1318 }
1319 Py_INCREF(meta_path); /* zap guard */
1320 npath = PyList_Size(meta_path);
1321 for (i = 0; i < npath; i++) {
1322 PyObject *loader;
1323 PyObject *hook = PyList_GetItem(meta_path, i);
1324 loader = PyObject_CallMethod(hook, "find_module",
1325 "sO", fullname,
1326 path != NULL ?
1327 path : Py_None);
1328 if (loader == NULL) {
1329 Py_DECREF(meta_path);
1330 return NULL; /* true error */
1331 }
1332 if (loader != Py_None) {
1333 /* a loader was found */
1334 *p_loader = loader;
1335 Py_DECREF(meta_path);
1336 return &importhookdescr;
1337 }
1338 Py_DECREF(loader);
1339 }
1340 Py_DECREF(meta_path);
1341 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001342
Benjamin Petersond968e272008-11-05 22:48:33 +00001343 if (find_frozen(fullname) != NULL) {
1344 strcpy(buf, fullname);
1345 return &fd_frozen;
Guido van Rossum0506a431998-08-11 15:07:39 +00001346 }
Benjamin Petersond968e272008-11-05 22:48:33 +00001347
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001348 if (path == NULL) {
1349 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001350 strcpy(buf, name);
1351 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001352 }
Guido van Rossumac279101996-08-22 23:10:58 +00001353#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001354 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1355 if (fp != NULL) {
1356 *p_fp = fp;
1357 return fdp;
1358 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001359#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001360 path = PySys_GetObject("path");
1361 }
Benjamin Petersond968e272008-11-05 22:48:33 +00001362
Guido van Rossum79f25d91997-04-29 20:08:16 +00001363 if (path == NULL || !PyList_Check(path)) {
1364 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001365 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001366 return NULL;
1367 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001368
1369 path_hooks = PySys_GetObject("path_hooks");
1370 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1371 PyErr_SetString(PyExc_ImportError,
1372 "sys.path_hooks must be a list of "
1373 "import hooks");
1374 return NULL;
1375 }
1376 path_importer_cache = PySys_GetObject("path_importer_cache");
1377 if (path_importer_cache == NULL ||
1378 !PyDict_Check(path_importer_cache)) {
1379 PyErr_SetString(PyExc_ImportError,
1380 "sys.path_importer_cache must be a dict");
1381 return NULL;
1382 }
1383
Guido van Rossum79f25d91997-04-29 20:08:16 +00001384 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001385 namelen = strlen(name);
1386 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001387 PyObject *v = PyList_GetItem(path, i);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001388 PyObject *origv = v;
Guido van Rossum6262cc72007-05-09 23:29:27 +00001389 const char *base;
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001390 Py_ssize_t size;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001391 if (!v)
1392 return NULL;
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001393 if (PyUnicode_Check(v)) {
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001394 v = PyUnicode_AsEncodedString(v,
1395 Py_FileSystemDefaultEncoding, NULL);
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001396 if (v == NULL)
1397 return NULL;
1398 }
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001399 else if (!PyBytes_Check(v))
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001400 continue;
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001401 else
1402 Py_INCREF(v);
1403
Christian Heimes72b710a2008-05-26 13:28:38 +00001404 base = PyBytes_AS_STRING(v);
1405 size = PyBytes_GET_SIZE(v);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001406 len = size;
Walter Dörwald3430d702002-06-17 10:43:59 +00001407 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001408 Py_DECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001409 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001410 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001411 strcpy(buf, base);
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001412 Py_DECREF(v);
1413
Walter Dörwald3430d702002-06-17 10:43:59 +00001414 if (strlen(buf) != len) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001415 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001416 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001417
1418 /* sys.path_hooks import hook */
1419 if (p_loader != NULL) {
1420 PyObject *importer;
1421
1422 importer = get_path_importer(path_importer_cache,
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001423 path_hooks, origv);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001424 if (importer == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001425 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001426 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001427 /* Note: importer is a borrowed reference */
1428 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001429 PyObject *loader;
1430 loader = PyObject_CallMethod(importer,
1431 "find_module",
1432 "s", fullname);
1433 if (loader == NULL)
1434 return NULL; /* error */
1435 if (loader != Py_None) {
1436 /* a loader was found */
1437 *p_loader = loader;
1438 return &importhookdescr;
1439 }
1440 Py_DECREF(loader);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001441 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001442 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001443 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001444 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001445
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001446 if (len > 0 && buf[len-1] != SEP
1447#ifdef ALTSEP
1448 && buf[len-1] != ALTSEP
1449#endif
1450 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001451 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001452 strcpy(buf+len, name);
1453 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001454
1455 /* Check for package import (buf holds a directory name,
1456 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001457#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001458 if (stat(buf, &statbuf) == 0 && /* it exists */
1459 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001460 case_ok(buf, len, namelen, name)) { /* case matches */
1461 if (find_init_module(buf)) { /* and has __init__.py */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001462 return &fd_package;
1463 }
1464 else {
1465 char warnstr[MAXPATHLEN+80];
1466 sprintf(warnstr, "Not importing directory "
1467 "'%.*s': missing __init__.py",
1468 MAXPATHLEN, buf);
Skip Montanaro46fc3372007-08-12 11:44:53 +00001469 if (PyErr_WarnEx(PyExc_ImportWarning,
1470 warnstr, 1)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001471 return NULL;
1472 }
1473 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001474 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001475#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001476#if defined(PYOS_OS2)
1477 /* take a snapshot of the module spec for restoration
1478 * after the 8 character DLL hackery
1479 */
1480 saved_buf = strdup(buf);
1481 saved_len = len;
1482 saved_namelen = namelen;
1483#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001484 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum04110fb2007-08-24 16:32:05 +00001485#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001486 /* OS/2 limits DLLs to 8 character names (w/o
1487 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001488 * so if the name is longer than that and its a
1489 * dynamically loaded module we're going to try,
1490 * truncate the name before trying
1491 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001492 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001493 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001494 const struct filedescr *scan;
1495 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001496 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001497 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001498 break;
1499 else
1500 scan++;
1501 }
1502 if (scan->suffix != NULL) {
1503 /* yes, so truncate the name */
1504 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001505 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001506 buf[len] = '\0';
1507 }
1508 }
1509#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001510 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001511 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001512 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001513 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001514 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001515 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001516 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001517 if (fp != NULL) {
1518 if (case_ok(buf, len, namelen, name))
1519 break;
1520 else { /* continue search */
1521 fclose(fp);
1522 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001523 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001524 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001525#if defined(PYOS_OS2)
1526 /* restore the saved snapshot */
1527 strcpy(buf, saved_buf);
1528 len = saved_len;
1529 namelen = saved_namelen;
1530#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001531 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001532#if defined(PYOS_OS2)
1533 /* don't need/want the module name snapshot anymore */
1534 if (saved_buf)
1535 {
1536 free(saved_buf);
1537 saved_buf = NULL;
1538 }
1539#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001540 if (fp != NULL)
1541 break;
1542 }
1543 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001544 PyErr_Format(PyExc_ImportError,
1545 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001546 return NULL;
1547 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001548 *p_fp = fp;
1549 return fdp;
1550}
1551
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001552/* Helpers for main.c
1553 * Find the source file corresponding to a named module
1554 */
1555struct filedescr *
1556_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1557 size_t buflen, FILE **p_fp, PyObject **p_loader)
1558{
1559 return find_module((char *) name, (char *) name, path,
1560 buf, buflen, p_fp, p_loader);
1561}
1562
1563PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1564{
1565 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1566}
1567
Martin v. Löwis18e16552006-02-15 17:27:45 +00001568/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001569 * The arguments here are tricky, best shown by example:
1570 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1571 * ^ ^ ^ ^
1572 * |--------------------- buf ---------------------|
1573 * |------------------- len ------------------|
1574 * |------ name -------|
1575 * |----- namelen -----|
1576 * buf is the full path, but len only counts up to (& exclusive of) the
1577 * extension. name is the module name, also exclusive of extension.
1578 *
1579 * We've already done a successful stat() or fopen() on buf, so know that
1580 * there's some match, possibly case-insensitive.
1581 *
Tim Peters50d8d372001-02-28 05:34:27 +00001582 * case_ok() is to return 1 if there's a case-sensitive match for
1583 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1584 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001585 *
Tim Peters50d8d372001-02-28 05:34:27 +00001586 * case_ok() is used to implement case-sensitive import semantics even
1587 * on platforms with case-insensitive filesystems. It's trivial to implement
1588 * for case-sensitive filesystems. It's pretty much a cross-platform
1589 * nightmare for systems with case-insensitive filesystems.
1590 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001591
Tim Peters50d8d372001-02-28 05:34:27 +00001592/* First we may need a pile of platform-specific header files; the sequence
1593 * of #if's here should match the sequence in the body of case_ok().
1594 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001595#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001596#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001597
Tim Peters50d8d372001-02-28 05:34:27 +00001598#elif defined(DJGPP)
1599#include <dir.h>
1600
Jason Tishler7961aa62005-05-20 00:56:54 +00001601#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001602#include <sys/types.h>
1603#include <dirent.h>
1604
Andrew MacIntyred9400542002-02-26 11:41:34 +00001605#elif defined(PYOS_OS2)
1606#define INCL_DOS
1607#define INCL_DOSERRORS
1608#define INCL_NOPMAPI
1609#include <os2.h>
Tim Peters50d8d372001-02-28 05:34:27 +00001610#endif
1611
Guido van Rossum0980bd91998-02-13 17:18:36 +00001612static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001613case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001614{
Tim Peters50d8d372001-02-28 05:34:27 +00001615/* Pick a platform-specific implementation; the sequence of #if's here should
1616 * match the sequence just above.
1617 */
1618
Jason Tishler7961aa62005-05-20 00:56:54 +00001619/* MS_WINDOWS */
1620#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001621 WIN32_FIND_DATA data;
1622 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001623
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001624 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001625 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001626
Guido van Rossum0980bd91998-02-13 17:18:36 +00001627 h = FindFirstFile(buf, &data);
1628 if (h == INVALID_HANDLE_VALUE) {
1629 PyErr_Format(PyExc_NameError,
1630 "Can't find file for module %.100s\n(filename %.300s)",
1631 name, buf);
1632 return 0;
1633 }
1634 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001635 return strncmp(data.cFileName, name, namelen) == 0;
1636
1637/* DJGPP */
1638#elif defined(DJGPP)
1639 struct ffblk ffblk;
1640 int done;
1641
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001642 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001643 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001644
1645 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1646 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001647 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001648 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001649 name, buf);
1650 return 0;
1651 }
Tim Peters50d8d372001-02-28 05:34:27 +00001652 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001653
Jason Tishler7961aa62005-05-20 00:56:54 +00001654/* new-fangled macintosh (macosx) or Cygwin */
1655#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001656 DIR *dirp;
1657 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001658 char dirname[MAXPATHLEN + 1];
1659 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001660
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001661 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001662 return 1;
1663
Tim Petersd1e87a82001-03-01 18:12:00 +00001664 /* Copy the dir component into dirname; substitute "." if empty */
1665 if (dirlen <= 0) {
1666 dirname[0] = '.';
1667 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001668 }
1669 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001670 assert(dirlen <= MAXPATHLEN);
1671 memcpy(dirname, buf, dirlen);
1672 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001673 }
1674 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001675 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001676 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001677 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001678 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001679 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001680#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001681 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001682#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001683 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001684#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001685 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001686 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001687 (void)closedir(dirp);
1688 return 1; /* Found */
1689 }
1690 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001691 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001692 }
Tim Peters430f5d42001-03-01 01:30:56 +00001693 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001694
Andrew MacIntyred9400542002-02-26 11:41:34 +00001695/* OS/2 */
1696#elif defined(PYOS_OS2)
1697 HDIR hdir = 1;
1698 ULONG srchcnt = 1;
1699 FILEFINDBUF3 ffbuf;
1700 APIRET rc;
1701
Christian Heimes790c8232008-01-07 21:14:23 +00001702 if (Py_GETENV("PYTHONCASEOK") != NULL)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001703 return 1;
1704
1705 rc = DosFindFirst(buf,
1706 &hdir,
1707 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1708 &ffbuf, sizeof(ffbuf),
1709 &srchcnt,
1710 FIL_STANDARD);
1711 if (rc != NO_ERROR)
1712 return 0;
1713 return strncmp(ffbuf.achName, name, namelen) == 0;
1714
Tim Peters50d8d372001-02-28 05:34:27 +00001715/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1716#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001717 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001718
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001719#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001720}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001721
Guido van Rossum0980bd91998-02-13 17:18:36 +00001722
Guido van Rossum197346f1997-10-31 18:38:52 +00001723#ifdef HAVE_STAT
1724/* Helper to look for __init__.py or __init__.py[co] in potential package */
1725static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001726find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001727{
Tim Peters0f9431f2001-07-05 03:47:53 +00001728 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001729 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001730 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001731 struct stat statbuf;
1732
Tim Peters0f9431f2001-07-05 03:47:53 +00001733/* For calling case_ok(buf, len, namelen, name):
1734 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1735 * ^ ^ ^ ^
1736 * |--------------------- buf ---------------------|
1737 * |------------------- len ------------------|
1738 * |------ name -------|
1739 * |----- namelen -----|
1740 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001741 if (save_len + 13 >= MAXPATHLEN)
1742 return 0;
1743 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001744 pname = buf + i;
1745 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001746 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001747 if (case_ok(buf,
1748 save_len + 9, /* len("/__init__") */
1749 8, /* len("__init__") */
1750 pname)) {
1751 buf[save_len] = '\0';
1752 return 1;
1753 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001754 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001755 i += strlen(pname);
1756 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001757 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001758 if (case_ok(buf,
1759 save_len + 9, /* len("/__init__") */
1760 8, /* len("__init__") */
1761 pname)) {
1762 buf[save_len] = '\0';
1763 return 1;
1764 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001765 }
1766 buf[save_len] = '\0';
1767 return 0;
1768}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001769
Guido van Rossum197346f1997-10-31 18:38:52 +00001770#endif /* HAVE_STAT */
1771
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001772
Tim Petersdbd9ba62000-07-09 03:09:57 +00001773static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001774
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001775/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001776 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001777
Guido van Rossum79f25d91997-04-29 20:08:16 +00001778static PyObject *
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001779load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001780{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001781 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001782 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001783 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001784
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001785 /* First check that there's an open file (if we need one) */
1786 switch (type) {
1787 case PY_SOURCE:
1788 case PY_COMPILED:
1789 if (fp == NULL) {
1790 PyErr_Format(PyExc_ValueError,
1791 "file object required for import (type code %d)",
1792 type);
1793 return NULL;
1794 }
1795 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001796
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001797 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001798
1799 case PY_SOURCE:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001800 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001801 break;
1802
1803 case PY_COMPILED:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001804 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001805 break;
1806
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001807#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001808 case C_EXTENSION:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001809 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001810 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001811#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001812
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001813 case PKG_DIRECTORY:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001814 m = load_package(name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001815 break;
1816
1817 case C_BUILTIN:
1818 case PY_FROZEN:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001819 if (pathname != NULL && pathname[0] != '\0')
1820 name = pathname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001821 if (type == C_BUILTIN)
1822 err = init_builtin(name);
1823 else
1824 err = PyImport_ImportFrozenModule(name);
1825 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001826 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001827 if (err == 0) {
1828 PyErr_Format(PyExc_ImportError,
1829 "Purported %s module %.200s not found",
1830 type == C_BUILTIN ?
1831 "builtin" : "frozen",
1832 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001833 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001834 }
1835 modules = PyImport_GetModuleDict();
1836 m = PyDict_GetItemString(modules, name);
1837 if (m == NULL) {
1838 PyErr_Format(
1839 PyExc_ImportError,
1840 "%s module %.200s not properly initialized",
1841 type == C_BUILTIN ?
1842 "builtin" : "frozen",
1843 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001844 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001845 }
1846 Py_INCREF(m);
1847 break;
1848
Just van Rossum52e14d62002-12-30 22:08:05 +00001849 case IMP_HOOK: {
1850 if (loader == NULL) {
1851 PyErr_SetString(PyExc_ImportError,
1852 "import hook without loader");
1853 return NULL;
1854 }
1855 m = PyObject_CallMethod(loader, "load_module", "s", name);
1856 break;
1857 }
1858
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001859 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001860 PyErr_Format(PyExc_ImportError,
1861 "Don't know how to import %.200s (type code %d)",
1862 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001863 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001864
1865 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001866
1867 return m;
1868}
1869
1870
1871/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001872 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001873 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001874
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001875static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001876init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001877{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001878 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001879
Greg Ward201baee2001-10-04 14:52:06 +00001880 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001881 return 1;
1882
Guido van Rossum771c6c81997-10-31 18:37:24 +00001883 for (p = PyImport_Inittab; p->name != NULL; p++) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00001884 PyObject *mod;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001885 if (strcmp(name, p->name) == 0) {
1886 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001887 PyErr_Format(PyExc_ImportError,
1888 "Cannot re-init internal module %.200s",
1889 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001890 return -1;
1891 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001892 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001893 PySys_WriteStderr("import %s # builtin\n", name);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001894 mod = (*p->initfunc)();
1895 if (mod == 0)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001896 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001897 if (_PyImport_FixupExtension(mod, name, name) < 0)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001898 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001899 /* FixupExtension has put the module into sys.modules,
1900 so we can release our own reference. */
1901 Py_DECREF(mod);
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001902 return 1;
1903 }
1904 }
1905 return 0;
1906}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001907
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001908
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001909/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001910
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001911static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001912find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001913{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001914 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001915
Benjamin Petersond968e272008-11-05 22:48:33 +00001916 if (!name)
1917 return NULL;
1918
Guido van Rossum79f25d91997-04-29 20:08:16 +00001919 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001920 if (p->name == NULL)
1921 return NULL;
1922 if (strcmp(p->name, name) == 0)
1923 break;
1924 }
1925 return p;
1926}
1927
Guido van Rossum79f25d91997-04-29 20:08:16 +00001928static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001929get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001930{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001931 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001932 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001933
1934 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001935 PyErr_Format(PyExc_ImportError,
1936 "No such frozen object named %.200s",
1937 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001938 return NULL;
1939 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001940 if (p->code == NULL) {
1941 PyErr_Format(PyExc_ImportError,
1942 "Excluded frozen object named %.200s",
1943 name);
1944 return NULL;
1945 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001946 size = p->size;
1947 if (size < 0)
1948 size = -size;
1949 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001950}
1951
Brett Cannon8d110132009-03-15 02:20:16 +00001952static PyObject *
1953is_frozen_package(char *name)
1954{
1955 struct _frozen *p = find_frozen(name);
1956 int size;
1957
1958 if (p == NULL) {
1959 PyErr_Format(PyExc_ImportError,
1960 "No such frozen object named %.200s",
1961 name);
1962 return NULL;
1963 }
1964
1965 size = p->size;
1966
1967 if (size < 0)
1968 Py_RETURN_TRUE;
1969 else
1970 Py_RETURN_FALSE;
1971}
1972
1973
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001974/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001975 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001976 an exception set if the initialization failed.
1977 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001978
1979int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001980PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001981{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001982 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001983 PyObject *co;
1984 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001985 int ispackage;
1986 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001987
1988 if (p == NULL)
1989 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001990 if (p->code == NULL) {
1991 PyErr_Format(PyExc_ImportError,
1992 "Excluded frozen object named %.200s",
1993 name);
1994 return -1;
1995 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001996 size = p->size;
1997 ispackage = (size < 0);
1998 if (ispackage)
1999 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002000 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00002001 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00002002 name, ispackage ? " package" : "");
2003 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002004 if (co == NULL)
2005 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002006 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002007 PyErr_Format(PyExc_TypeError,
2008 "frozen object %.200s is not a code object",
2009 name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002010 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002011 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00002012 if (ispackage) {
2013 /* Set __path__ to the package name */
Benjamin Petersond968e272008-11-05 22:48:33 +00002014 PyObject *d, *s, *l;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002015 int err;
2016 m = PyImport_AddModule(name);
2017 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002018 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002019 d = PyModule_GetDict(m);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002020 s = PyUnicode_InternFromString(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00002021 if (s == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002022 goto err_return;
Benjamin Petersond968e272008-11-05 22:48:33 +00002023 l = PyList_New(1);
2024 if (l == NULL) {
2025 Py_DECREF(s);
2026 goto err_return;
2027 }
2028 PyList_SET_ITEM(l, 0, s);
2029 err = PyDict_SetItemString(d, "__path__", l);
2030 Py_DECREF(l);
Guido van Rossuma5568d31998-03-05 03:45:08 +00002031 if (err != 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002032 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002033 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00002034 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002035 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002036 goto err_return;
2037 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002038 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002039 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002040err_return:
2041 Py_DECREF(co);
2042 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002043}
Guido van Rossum74e6a111994-08-29 12:54:38 +00002044
2045
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002046/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002047 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00002048
Guido van Rossum79f25d91997-04-29 20:08:16 +00002049PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002050PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00002051{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002052 PyObject *pname;
2053 PyObject *result;
2054
Martin v. Löwis5b222132007-06-10 09:51:05 +00002055 pname = PyUnicode_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00002056 if (pname == NULL)
2057 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002058 result = PyImport_Import(pname);
2059 Py_DECREF(pname);
2060 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002061}
2062
Christian Heimes072c0f12008-01-03 23:01:04 +00002063/* Import a module without blocking
2064 *
2065 * At first it tries to fetch the module from sys.modules. If the module was
2066 * never loaded before it loads it with PyImport_ImportModule() unless another
2067 * thread holds the import lock. In the latter case the function raises an
2068 * ImportError instead of blocking.
2069 *
2070 * Returns the module object with incremented ref count.
2071 */
2072PyObject *
2073PyImport_ImportModuleNoBlock(const char *name)
2074{
2075 PyObject *result;
2076 PyObject *modules;
2077 long me;
2078
2079 /* Try to get the module from sys.modules[name] */
2080 modules = PyImport_GetModuleDict();
2081 if (modules == NULL)
2082 return NULL;
2083
2084 result = PyDict_GetItemString(modules, name);
2085 if (result != NULL) {
2086 Py_INCREF(result);
2087 return result;
2088 }
2089 else {
2090 PyErr_Clear();
2091 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002092#ifdef WITH_THREAD
Christian Heimes072c0f12008-01-03 23:01:04 +00002093 /* check the import lock
2094 * me might be -1 but I ignore the error here, the lock function
2095 * takes care of the problem */
2096 me = PyThread_get_thread_ident();
2097 if (import_lock_thread == -1 || import_lock_thread == me) {
2098 /* no thread or me is holding the lock */
2099 return PyImport_ImportModule(name);
2100 }
2101 else {
2102 PyErr_Format(PyExc_ImportError,
2103 "Failed to import %.200s because the import lock"
2104 "is held by another thread.",
2105 name);
2106 return NULL;
2107 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002108#else
2109 return PyImport_ImportModule(name);
2110#endif
Christian Heimes072c0f12008-01-03 23:01:04 +00002111}
2112
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002113/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002114static PyObject *get_parent(PyObject *globals, char *buf,
2115 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002116static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002117 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002118static int mark_miss(char *name);
2119static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002120 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002121static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002122
2123/* The Magnum Opus of dotted-name import :-) */
2124
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002125static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002126import_module_level(char *name, PyObject *globals, PyObject *locals,
2127 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002128{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002129 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00002130 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002131 PyObject *parent, *head, *next, *tail;
2132
Christian Heimes454f37b2008-01-10 00:10:02 +00002133 if (strchr(name, '/') != NULL
2134#ifdef MS_WINDOWS
2135 || strchr(name, '\\') != NULL
2136#endif
2137 ) {
2138 PyErr_SetString(PyExc_ImportError,
2139 "Import by filename is not supported.");
2140 return NULL;
2141 }
2142
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002143 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002144 if (parent == NULL)
2145 return NULL;
2146
2147 head = load_next(parent, Py_None, &name, buf, &buflen);
2148 if (head == NULL)
2149 return NULL;
2150
2151 tail = head;
2152 Py_INCREF(tail);
2153 while (name) {
2154 next = load_next(tail, tail, &name, buf, &buflen);
2155 Py_DECREF(tail);
2156 if (next == NULL) {
2157 Py_DECREF(head);
2158 return NULL;
2159 }
2160 tail = next;
2161 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002162 if (tail == Py_None) {
2163 /* If tail is Py_None, both get_parent and load_next found
2164 an empty module name: someone called __import__("") or
2165 doctored faulty bytecode */
2166 Py_DECREF(tail);
2167 Py_DECREF(head);
2168 PyErr_SetString(PyExc_ValueError,
2169 "Empty module name");
2170 return NULL;
2171 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002172
2173 if (fromlist != NULL) {
2174 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2175 fromlist = NULL;
2176 }
2177
2178 if (fromlist == NULL) {
2179 Py_DECREF(tail);
2180 return head;
2181 }
2182
2183 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002184 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002185 Py_DECREF(tail);
2186 return NULL;
2187 }
2188
2189 return tail;
2190}
2191
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002192PyObject *
2193PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2194 PyObject *fromlist, int level)
2195{
2196 PyObject *result;
Benjamin Peterson0df35a92009-10-04 20:32:25 +00002197 _PyImport_AcquireLock();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002198 result = import_module_level(name, globals, locals, fromlist, level);
Benjamin Peterson0df35a92009-10-04 20:32:25 +00002199 if (_PyImport_ReleaseLock() < 0) {
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002200 Py_XDECREF(result);
2201 PyErr_SetString(PyExc_RuntimeError,
2202 "not holding the import lock");
2203 return NULL;
2204 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002205 return result;
2206}
2207
Fred Drake87590902004-05-28 20:21:36 +00002208/* Return the package that an import is being performed in. If globals comes
2209 from the module foo.bar.bat (not itself a package), this returns the
2210 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002211 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002212
2213 The *name* of the returned package is returned in buf, with the length of
2214 the name in *p_buflen.
2215
2216 If globals doesn't come from a package or a module in a package, or a
2217 corresponding entry is not found in sys.modules, Py_None is returned.
2218*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002219static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002220get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002221{
2222 static PyObject *namestr = NULL;
2223 static PyObject *pathstr = NULL;
Nick Coghlande10c852007-12-04 12:22:52 +00002224 static PyObject *pkgstr = NULL;
2225 PyObject *pkgname, *modname, *modpath, *modules, *parent;
Georg Brandl2ee470f2008-07-16 12:55:28 +00002226 int orig_level = level;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002227
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002228 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002229 return Py_None;
2230
2231 if (namestr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002232 namestr = PyUnicode_InternFromString("__name__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002233 if (namestr == NULL)
2234 return NULL;
2235 }
2236 if (pathstr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002237 pathstr = PyUnicode_InternFromString("__path__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002238 if (pathstr == NULL)
2239 return NULL;
2240 }
Nick Coghlande10c852007-12-04 12:22:52 +00002241 if (pkgstr == NULL) {
2242 pkgstr = PyUnicode_InternFromString("__package__");
2243 if (pkgstr == NULL)
2244 return NULL;
2245 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002246
2247 *buf = '\0';
2248 *p_buflen = 0;
Nick Coghlande10c852007-12-04 12:22:52 +00002249 pkgname = PyDict_GetItem(globals, pkgstr);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002250
Nick Coghlande10c852007-12-04 12:22:52 +00002251 if ((pkgname != NULL) && (pkgname != Py_None)) {
2252 /* __package__ is set, so use it */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002253 char *pkgname_str;
Nick Coghlande10c852007-12-04 12:22:52 +00002254 Py_ssize_t len;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002255
Nick Coghlande10c852007-12-04 12:22:52 +00002256 if (!PyUnicode_Check(pkgname)) {
2257 PyErr_SetString(PyExc_ValueError,
2258 "__package__ set to non-string");
2259 return NULL;
2260 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002261 pkgname_str = _PyUnicode_AsStringAndSize(pkgname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002262 if (len == 0) {
2263 if (level > 0) {
2264 PyErr_SetString(PyExc_ValueError,
2265 "Attempted relative import in non-package");
2266 return NULL;
2267 }
2268 return Py_None;
2269 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002270 if (len > MAXPATHLEN) {
2271 PyErr_SetString(PyExc_ValueError,
Nick Coghlande10c852007-12-04 12:22:52 +00002272 "Package name too long");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002273 return NULL;
2274 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002275 strcpy(buf, pkgname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002276 } else {
2277 /* __package__ not set, so figure it out and set it */
2278 modname = PyDict_GetItem(globals, namestr);
2279 if (modname == NULL || !PyUnicode_Check(modname))
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002280 return Py_None;
Nick Coghlande10c852007-12-04 12:22:52 +00002281
2282 modpath = PyDict_GetItem(globals, pathstr);
2283 if (modpath != NULL) {
2284 /* __path__ is set, so modname is already the package name */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002285 char *modname_str;
2286 Py_ssize_t len;
Nick Coghlande10c852007-12-04 12:22:52 +00002287 int error;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002288
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002289 modname_str = _PyUnicode_AsStringAndSize(modname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002290 if (len > MAXPATHLEN) {
2291 PyErr_SetString(PyExc_ValueError,
2292 "Module name too long");
2293 return NULL;
2294 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002295 strcpy(buf, modname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002296 error = PyDict_SetItem(globals, pkgstr, modname);
2297 if (error) {
2298 PyErr_SetString(PyExc_ValueError,
2299 "Could not set __package__");
2300 return NULL;
2301 }
2302 } else {
2303 /* Normal module, so work out the package name if any */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002304 char *start = _PyUnicode_AsString(modname);
Nick Coghlande10c852007-12-04 12:22:52 +00002305 char *lastdot = strrchr(start, '.');
2306 size_t len;
2307 int error;
2308 if (lastdot == NULL && level > 0) {
2309 PyErr_SetString(PyExc_ValueError,
2310 "Attempted relative import in non-package");
2311 return NULL;
2312 }
2313 if (lastdot == NULL) {
2314 error = PyDict_SetItem(globals, pkgstr, Py_None);
2315 if (error) {
2316 PyErr_SetString(PyExc_ValueError,
2317 "Could not set __package__");
2318 return NULL;
2319 }
2320 return Py_None;
2321 }
2322 len = lastdot - start;
2323 if (len >= MAXPATHLEN) {
2324 PyErr_SetString(PyExc_ValueError,
2325 "Module name too long");
2326 return NULL;
2327 }
2328 strncpy(buf, start, len);
2329 buf[len] = '\0';
2330 pkgname = PyUnicode_FromString(buf);
2331 if (pkgname == NULL) {
2332 return NULL;
2333 }
2334 error = PyDict_SetItem(globals, pkgstr, pkgname);
2335 Py_DECREF(pkgname);
2336 if (error) {
2337 PyErr_SetString(PyExc_ValueError,
2338 "Could not set __package__");
2339 return NULL;
2340 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002341 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002342 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002343 while (--level > 0) {
2344 char *dot = strrchr(buf, '.');
2345 if (dot == NULL) {
2346 PyErr_SetString(PyExc_ValueError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002347 "Attempted relative import beyond "
2348 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002349 return NULL;
2350 }
2351 *dot = '\0';
2352 }
2353 *p_buflen = strlen(buf);
2354
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002355 modules = PyImport_GetModuleDict();
2356 parent = PyDict_GetItemString(modules, buf);
Georg Brandl2ee470f2008-07-16 12:55:28 +00002357 if (parent == NULL) {
2358 if (orig_level < 1) {
2359 PyObject *err_msg = PyBytes_FromFormat(
2360 "Parent module '%.200s' not found "
2361 "while handling absolute import", buf);
2362 if (err_msg == NULL) {
2363 return NULL;
2364 }
2365 if (!PyErr_WarnEx(PyExc_RuntimeWarning,
2366 PyBytes_AsString(err_msg), 1)) {
2367 *buf = '\0';
2368 *p_buflen = 0;
2369 parent = Py_None;
2370 }
2371 Py_DECREF(err_msg);
2372 } else {
2373 PyErr_Format(PyExc_SystemError,
2374 "Parent module '%.200s' not loaded, "
2375 "cannot perform relative import", buf);
2376 }
2377 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002378 return parent;
2379 /* We expect, but can't guarantee, if parent != None, that:
2380 - parent.__name__ == buf
2381 - parent.__dict__ is globals
2382 If this is violated... Who cares? */
2383}
2384
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002385/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002386static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002387load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002388 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002389{
2390 char *name = *p_name;
2391 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002392 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002393 char *p;
2394 PyObject *result;
2395
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002396 if (strlen(name) == 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002397 /* completely empty module name should only happen in
2398 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002399 Py_INCREF(mod);
2400 *p_name = NULL;
2401 return mod;
2402 }
2403
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002404 if (dot == NULL) {
2405 *p_name = NULL;
2406 len = strlen(name);
2407 }
2408 else {
2409 *p_name = dot+1;
2410 len = dot-name;
2411 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002412 if (len == 0) {
2413 PyErr_SetString(PyExc_ValueError,
2414 "Empty module name");
2415 return NULL;
2416 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002417
2418 p = buf + *p_buflen;
2419 if (p != buf)
2420 *p++ = '.';
2421 if (p+len-buf >= MAXPATHLEN) {
2422 PyErr_SetString(PyExc_ValueError,
2423 "Module name too long");
2424 return NULL;
2425 }
2426 strncpy(p, name, len);
2427 p[len] = '\0';
2428 *p_buflen = p+len-buf;
2429
2430 result = import_submodule(mod, p, buf);
2431 if (result == Py_None && altmod != mod) {
2432 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002433 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002434 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002435 if (result != NULL && result != Py_None) {
2436 if (mark_miss(buf) != 0) {
2437 Py_DECREF(result);
2438 return NULL;
2439 }
2440 strncpy(buf, name, len);
2441 buf[len] = '\0';
2442 *p_buflen = len;
2443 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002444 }
2445 if (result == NULL)
2446 return NULL;
2447
2448 if (result == Py_None) {
2449 Py_DECREF(result);
2450 PyErr_Format(PyExc_ImportError,
2451 "No module named %.200s", name);
2452 return NULL;
2453 }
2454
2455 return result;
2456}
2457
2458static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002459mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002460{
2461 PyObject *modules = PyImport_GetModuleDict();
2462 return PyDict_SetItemString(modules, name, Py_None);
2463}
2464
2465static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002466ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002467 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002468{
2469 int i;
2470
2471 if (!PyObject_HasAttrString(mod, "__path__"))
2472 return 1;
2473
2474 for (i = 0; ; i++) {
2475 PyObject *item = PySequence_GetItem(fromlist, i);
2476 int hasit;
2477 if (item == NULL) {
2478 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2479 PyErr_Clear();
2480 return 1;
2481 }
2482 return 0;
2483 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002484 if (!PyUnicode_Check(item)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002485 PyErr_SetString(PyExc_TypeError,
Neal Norwitz80e7f272007-08-26 06:45:23 +00002486 "Item in ``from list'' not a string");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002487 Py_DECREF(item);
2488 return 0;
2489 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002490 if (PyUnicode_AS_UNICODE(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002491 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002492 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002493 /* See if the package defines __all__ */
2494 if (recursive)
2495 continue; /* Avoid endless recursion */
2496 all = PyObject_GetAttrString(mod, "__all__");
2497 if (all == NULL)
2498 PyErr_Clear();
2499 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002500 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002501 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002502 if (!ret)
2503 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002504 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002505 continue;
2506 }
2507 hasit = PyObject_HasAttr(mod, item);
2508 if (!hasit) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002509 PyObject *item8;
2510 char *subname;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002511 PyObject *submod;
2512 char *p;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002513 if (!Py_FileSystemDefaultEncoding) {
2514 item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item),
2515 PyUnicode_GetSize(item),
Martin v. Löwis427dbff2007-06-12 05:53:00 +00002516 NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002517 } else {
Guido van Rossum98297ee2007-11-06 21:34:58 +00002518 item8 = PyUnicode_AsEncodedString(item,
2519 Py_FileSystemDefaultEncoding, NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002520 }
2521 if (!item8) {
2522 PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
2523 return 0;
2524 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002525 subname = PyBytes_AS_STRING(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002526 if (buflen + strlen(subname) >= MAXPATHLEN) {
2527 PyErr_SetString(PyExc_ValueError,
2528 "Module name too long");
2529 Py_DECREF(item);
2530 return 0;
2531 }
2532 p = buf + buflen;
2533 *p++ = '.';
2534 strcpy(p, subname);
2535 submod = import_submodule(mod, subname, buf);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002536 Py_DECREF(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002537 Py_XDECREF(submod);
2538 if (submod == NULL) {
2539 Py_DECREF(item);
2540 return 0;
2541 }
2542 }
2543 Py_DECREF(item);
2544 }
2545
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002546 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002547}
2548
Neil Schemenauer00b09662003-06-16 21:03:07 +00002549static int
2550add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2551 PyObject *modules)
2552{
2553 if (mod == Py_None)
2554 return 1;
2555 /* Irrespective of the success of this load, make a
2556 reference to it in the parent package module. A copy gets
2557 saved in the modules dictionary under the full name, so get a
2558 reference from there, if need be. (The exception is when the
2559 load failed with a SyntaxError -- then there's no trace in
2560 sys.modules. In that case, of course, do nothing extra.) */
2561 if (submod == NULL) {
2562 submod = PyDict_GetItemString(modules, fullname);
2563 if (submod == NULL)
2564 return 1;
2565 }
2566 if (PyModule_Check(mod)) {
2567 /* We can't use setattr here since it can give a
2568 * spurious warning if the submodule name shadows a
2569 * builtin name */
2570 PyObject *dict = PyModule_GetDict(mod);
2571 if (!dict)
2572 return 0;
2573 if (PyDict_SetItemString(dict, subname, submod) < 0)
2574 return 0;
2575 }
2576 else {
2577 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2578 return 0;
2579 }
2580 return 1;
2581}
2582
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002583static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002584import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002585{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002586 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002587 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002588
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002589 /* Require:
2590 if mod == None: subname == fullname
2591 else: mod.__name__ + "." + subname == fullname
2592 */
2593
Tim Peters50d8d372001-02-28 05:34:27 +00002594 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002595 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002596 }
2597 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002598 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002599 char buf[MAXPATHLEN+1];
2600 struct filedescr *fdp;
2601 FILE *fp = NULL;
2602
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002603 if (mod == Py_None)
2604 path = NULL;
2605 else {
2606 path = PyObject_GetAttrString(mod, "__path__");
2607 if (path == NULL) {
2608 PyErr_Clear();
2609 Py_INCREF(Py_None);
2610 return Py_None;
2611 }
2612 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002613
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002614 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002615 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2616 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002617 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002618 if (fdp == NULL) {
2619 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2620 return NULL;
2621 PyErr_Clear();
2622 Py_INCREF(Py_None);
2623 return Py_None;
2624 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002625 m = load_module(fullname, fp, buf, fdp->type, loader);
2626 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002627 if (fp)
2628 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002629 if (!add_submodule(mod, m, fullname, subname, modules)) {
2630 Py_XDECREF(m);
2631 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002632 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002633 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002634
2635 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002636}
2637
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002638
2639/* Re-import a module of any kind and return its module object, WITH
2640 INCREMENTED REFERENCE COUNT */
2641
Guido van Rossum79f25d91997-04-29 20:08:16 +00002642PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002643PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002644{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002645 PyInterpreterState *interp = PyThreadState_Get()->interp;
2646 PyObject *modules_reloading = interp->modules_reloading;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002647 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossumd8faa362007-04-27 19:54:29 +00002648 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002649 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002650 char buf[MAXPATHLEN+1];
2651 struct filedescr *fdp;
2652 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002653 PyObject *newm;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002654
2655 if (modules_reloading == NULL) {
2656 Py_FatalError("PyImport_ReloadModule: "
Guido van Rossume7ba4952007-06-06 23:52:48 +00002657 "no modules_reloading dictionary!");
Guido van Rossumd8faa362007-04-27 19:54:29 +00002658 return NULL;
2659 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002660
Guido van Rossum79f25d91997-04-29 20:08:16 +00002661 if (m == NULL || !PyModule_Check(m)) {
2662 PyErr_SetString(PyExc_TypeError,
2663 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002664 return NULL;
2665 }
Neal Norwitz5616c6d2007-08-26 05:32:41 +00002666 name = (char*)PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002667 if (name == NULL)
2668 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002669 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002670 PyErr_Format(PyExc_ImportError,
2671 "reload(): module %.200s not in sys.modules",
2672 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002673 return NULL;
2674 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002675 existing_m = PyDict_GetItemString(modules_reloading, name);
2676 if (existing_m != NULL) {
2677 /* Due to a recursive reload, this module is already
2678 being reloaded. */
2679 Py_INCREF(existing_m);
2680 return existing_m;
2681 }
2682 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
2683 return NULL;
2684
Guido van Rossum222ef561997-09-06 19:41:09 +00002685 subname = strrchr(name, '.');
2686 if (subname == NULL)
2687 subname = name;
2688 else {
2689 PyObject *parentname, *parent;
Christian Heimesc4cb3b82007-11-04 12:10:01 +00002690 parentname = PyUnicode_FromStringAndSize(name, (subname-name));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002691 if (parentname == NULL) {
2692 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002693 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002694 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002695 parent = PyDict_GetItem(modules, parentname);
2696 if (parent == NULL) {
2697 PyErr_Format(PyExc_ImportError,
2698 "reload(): parent %.200s not in sys.modules",
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002699 _PyUnicode_AsString(parentname));
Georg Brandl0c55f292005-09-14 06:56:20 +00002700 Py_DECREF(parentname);
Guido van Rossume7ba4952007-06-06 23:52:48 +00002701 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002702 return NULL;
2703 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002704 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002705 subname++;
2706 path = PyObject_GetAttrString(parent, "__path__");
2707 if (path == NULL)
2708 PyErr_Clear();
2709 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002710 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002711 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002712 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002713
2714 if (fdp == NULL) {
2715 Py_XDECREF(loader);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002716 imp_modules_reloading_clear();
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002717 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002718 }
2719
2720 newm = load_module(name, fp, buf, fdp->type, loader);
2721 Py_XDECREF(loader);
2722
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002723 if (fp)
2724 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002725 if (newm == NULL) {
2726 /* load_module probably removed name from modules because of
2727 * the error. Put back the original module object. We're
2728 * going to return NULL in this case regardless of whether
2729 * replacing name succeeds, so the return value is ignored.
2730 */
2731 PyDict_SetItemString(modules, name, m);
2732 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002733 imp_modules_reloading_clear();
Tim Peters1cd70172004-08-02 03:52:12 +00002734 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002735}
2736
2737
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002738/* Higher-level import emulator which emulates the "import" statement
2739 more accurately -- it invokes the __import__() function from the
2740 builtins of the current globals. This means that the import is
2741 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002742 environment, e.g. by "rexec".
2743 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00002744 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00002745 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002746
2747PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002748PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002749{
2750 static PyObject *silly_list = NULL;
2751 static PyObject *builtins_str = NULL;
2752 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002753 PyObject *globals = NULL;
2754 PyObject *import = NULL;
2755 PyObject *builtins = NULL;
2756 PyObject *r = NULL;
2757
2758 /* Initialize constant string objects */
2759 if (silly_list == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002760 import_str = PyUnicode_InternFromString("__import__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002761 if (import_str == NULL)
2762 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002763 builtins_str = PyUnicode_InternFromString("__builtins__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002764 if (builtins_str == NULL)
2765 return NULL;
2766 silly_list = Py_BuildValue("[s]", "__doc__");
2767 if (silly_list == NULL)
2768 return NULL;
2769 }
2770
2771 /* Get the builtins from current globals */
2772 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002773 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002774 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002775 builtins = PyObject_GetItem(globals, builtins_str);
2776 if (builtins == NULL)
2777 goto err;
2778 }
2779 else {
2780 /* No globals -- use standard builtins, and fake globals */
2781 PyErr_Clear();
2782
Georg Brandl1a3284e2007-12-02 09:40:06 +00002783 builtins = PyImport_ImportModuleLevel("builtins",
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002784 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002785 if (builtins == NULL)
2786 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002787 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2788 if (globals == NULL)
2789 goto err;
2790 }
2791
2792 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002793 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002794 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002795 if (import == NULL)
2796 PyErr_SetObject(PyExc_KeyError, import_str);
2797 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002798 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002799 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002800 if (import == NULL)
2801 goto err;
2802
Christian Heimes072c0f12008-01-03 23:01:04 +00002803 /* Call the __import__ function with the proper argument list
2804 * Always use absolute import here. */
2805 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
2806 globals, silly_list, 0, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002807
2808 err:
2809 Py_XDECREF(globals);
2810 Py_XDECREF(builtins);
2811 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002812
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002813 return r;
2814}
2815
2816
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002817/* Module 'imp' provides Python access to the primitives used for
2818 importing modules.
2819*/
2820
Guido van Rossum79f25d91997-04-29 20:08:16 +00002821static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002822imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002823{
2824 char buf[4];
2825
Guido van Rossum96774c12000-05-01 20:19:08 +00002826 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2827 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2828 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2829 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002830
Christian Heimes72b710a2008-05-26 13:28:38 +00002831 return PyBytes_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002832}
2833
Guido van Rossum79f25d91997-04-29 20:08:16 +00002834static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002835imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002836{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002837 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002838 struct filedescr *fdp;
2839
Guido van Rossum79f25d91997-04-29 20:08:16 +00002840 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002841 if (list == NULL)
2842 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002843 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2844 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002845 fdp->suffix, fdp->mode, fdp->type);
2846 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002847 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002848 return NULL;
2849 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002850 if (PyList_Append(list, item) < 0) {
2851 Py_DECREF(list);
2852 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002853 return NULL;
2854 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002855 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002856 }
2857 return list;
2858}
2859
Guido van Rossum79f25d91997-04-29 20:08:16 +00002860static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002861call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002862{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002863 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002864 PyObject *fob, *ret;
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002865 PyObject *pathobj;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002866 struct filedescr *fdp;
2867 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002868 FILE *fp = NULL;
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002869 int fd = -1;
Brett Cannon3bb42d92007-10-20 03:43:15 +00002870 char *found_encoding = NULL;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002871 char *encoding = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002872
2873 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002874 if (path == Py_None)
2875 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002876 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002877 if (fdp == NULL)
2878 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002879 if (fp != NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002880 fd = fileno(fp);
2881 if (fd != -1)
2882 fd = dup(fd);
2883 fclose(fp);
2884 fp = NULL;
2885 }
2886 if (fd != -1) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002887 if (strchr(fdp->mode, 'b') == NULL) {
Brett Cannon3bb42d92007-10-20 03:43:15 +00002888 /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
2889 memory. */
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002890 found_encoding = PyTokenizer_FindEncoding(fd);
2891 lseek(fd, 0, 0); /* Reset position */
Amaury Forgeot d'Arc1b933ed2008-09-04 22:34:09 +00002892 if (found_encoding == NULL && PyErr_Occurred())
2893 return NULL;
Brett Cannon3bb42d92007-10-20 03:43:15 +00002894 encoding = (found_encoding != NULL) ? found_encoding :
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002895 (char*)PyUnicode_GetDefaultEncoding();
2896 }
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002897 fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002898 (char*)encoding, NULL, NULL, 1);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002899 if (fob == NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002900 close(fd);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002901 PyMem_FREE(found_encoding);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002902 return NULL;
2903 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002904 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002905 else {
2906 fob = Py_None;
2907 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002908 }
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002909 pathobj = PyUnicode_DecodeFSDefault(pathname);
2910 ret = Py_BuildValue("NN(ssi)",
2911 fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002912 PyMem_FREE(found_encoding);
2913
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002914 return ret;
2915}
2916
Guido van Rossum79f25d91997-04-29 20:08:16 +00002917static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002918imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002919{
2920 char *name;
Hirokazu Yamamoto90eaaf62009-01-30 03:15:05 +00002921 PyObject *ret, *path = NULL;
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002922 if (!PyArg_ParseTuple(args, "es|O:find_module",
2923 Py_FileSystemDefaultEncoding, &name,
2924 &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002925 return NULL;
Hirokazu Yamamoto90eaaf62009-01-30 03:15:05 +00002926 ret = call_find_module(name, path);
2927 PyMem_Free(name);
2928 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002929}
2930
2931static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002932imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002933{
2934 char *name;
2935 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002936 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002937 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002938 return NULL;
2939 ret = init_builtin(name);
2940 if (ret < 0)
2941 return NULL;
2942 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002943 Py_INCREF(Py_None);
2944 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002945 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002946 m = PyImport_AddModule(name);
2947 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002948 return m;
2949}
2950
Guido van Rossum79f25d91997-04-29 20:08:16 +00002951static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002952imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002953{
2954 char *name;
2955 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002956 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002957 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002958 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002959 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002960 if (ret < 0)
2961 return NULL;
2962 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002963 Py_INCREF(Py_None);
2964 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002965 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002966 m = PyImport_AddModule(name);
2967 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002968 return m;
2969}
2970
Guido van Rossum79f25d91997-04-29 20:08:16 +00002971static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002972imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002973{
2974 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002975
Guido van Rossum43713e52000-02-29 13:59:29 +00002976 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002977 return NULL;
2978 return get_frozen_object(name);
2979}
2980
Guido van Rossum79f25d91997-04-29 20:08:16 +00002981static PyObject *
Brett Cannon8d110132009-03-15 02:20:16 +00002982imp_is_frozen_package(PyObject *self, PyObject *args)
2983{
2984 char *name;
2985
2986 if (!PyArg_ParseTuple(args, "s:is_frozen_package", &name))
2987 return NULL;
2988 return is_frozen_package(name);
2989}
2990
2991static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002992imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002993{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002994 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002995 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002996 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00002997 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002998}
2999
Guido van Rossum79f25d91997-04-29 20:08:16 +00003000static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003001imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003002{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003003 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00003004 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00003005 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003006 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00003007 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00003008 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003009}
3010
3011static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003012get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003013{
3014 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003015 if (mode[0] == 'U')
3016 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003017 if (fob == NULL) {
3018 fp = fopen(pathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003019 }
3020 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003021 int fd = PyObject_AsFileDescriptor(fob);
3022 if (fd == -1)
3023 return NULL;
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003024 if (!_PyVerify_fd(fd))
3025 goto error;
3026 /* the FILE struct gets a new fd, so that it can be closed
3027 * independently of the file descriptor given
3028 */
3029 fd = dup(fd);
3030 if (fd == -1)
3031 goto error;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003032 fp = fdopen(fd, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003033 }
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003034 if (fp)
3035 return fp;
3036error:
3037 PyErr_SetFromErrno(PyExc_IOError);
3038 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003039}
3040
Guido van Rossum79f25d91997-04-29 20:08:16 +00003041static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003042imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003043{
3044 char *name;
3045 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003046 PyObject *fob = NULL;
3047 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003048 FILE *fp;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003049 if (!PyArg_ParseTuple(args, "ses|O:load_compiled",
3050 &name,
3051 Py_FileSystemDefaultEncoding, &pathname,
3052 &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003053 return NULL;
3054 fp = get_file(pathname, fob, "rb");
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003055 if (fp == NULL) {
3056 PyMem_Free(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003057 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003058 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003059 m = load_compiled_module(name, pathname, fp);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003060 fclose(fp);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003061 PyMem_Free(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003062 return m;
3063}
3064
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003065#ifdef HAVE_DYNAMIC_LOADING
3066
Guido van Rossum79f25d91997-04-29 20:08:16 +00003067static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003068imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003069{
3070 char *name;
3071 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003072 PyObject *fob = NULL;
3073 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00003074 FILE *fp = NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003075 if (!PyArg_ParseTuple(args, "ses|O:load_dynamic",
3076 &name,
3077 Py_FileSystemDefaultEncoding, &pathname,
3078 &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003079 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003080 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00003081 fp = get_file(pathname, fob, "r");
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003082 if (fp == NULL) {
3083 PyMem_Free(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003084 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003085 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003086 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003087 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003088 PyMem_Free(pathname);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003089 if (fp)
3090 fclose(fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00003091 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003092}
3093
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003094#endif /* HAVE_DYNAMIC_LOADING */
3095
Guido van Rossum79f25d91997-04-29 20:08:16 +00003096static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003097imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003098{
3099 char *name;
3100 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003101 PyObject *fob = NULL;
3102 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003103 FILE *fp;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003104 if (!PyArg_ParseTuple(args, "ses|O:load_source",
3105 &name,
3106 Py_FileSystemDefaultEncoding, &pathname,
3107 &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003108 return NULL;
3109 fp = get_file(pathname, fob, "r");
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003110 if (fp == NULL) {
3111 PyMem_Free(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003112 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003113 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003114 m = load_source_module(name, pathname, fp);
Kristján Valur Jónsson92af5d92009-03-31 17:47:50 +00003115 PyMem_Free(pathname);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003116 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003117 return m;
3118}
3119
Guido van Rossum79f25d91997-04-29 20:08:16 +00003120static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003121imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003122{
3123 char *name;
3124 PyObject *fob;
3125 char *pathname;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003126 PyObject * ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003127 char *suffix; /* Unused */
3128 char *mode;
3129 int type;
3130 FILE *fp;
3131
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003132 if (!PyArg_ParseTuple(args, "sOes(ssi):load_module",
3133 &name, &fob,
3134 Py_FileSystemDefaultEncoding, &pathname,
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003135 &suffix, &mode, &type))
3136 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003137 if (*mode) {
3138 /* Mode must start with 'r' or 'U' and must not contain '+'.
3139 Implicit in this test is the assumption that the mode
3140 may contain other modifiers like 'b' or 't'. */
3141
3142 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00003143 PyErr_Format(PyExc_ValueError,
3144 "invalid file open mode %.200s", mode);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003145 PyMem_Free(pathname);
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00003146 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003147 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003148 }
3149 if (fob == Py_None)
3150 fp = NULL;
3151 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003152 fp = get_file(NULL, fob, mode);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003153 if (fp == NULL) {
3154 PyMem_Free(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003155 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003156 }
3157 }
3158 ret = load_module(name, fp, pathname, type, NULL);
3159 PyMem_Free(pathname);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003160 if (fp)
3161 fclose(fp);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003162 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003163}
3164
3165static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003166imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003167{
3168 char *name;
3169 char *pathname;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003170 PyObject * ret;
3171 if (!PyArg_ParseTuple(args, "ses:load_package",
3172 &name, Py_FileSystemDefaultEncoding, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003173 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003174 ret = load_package(name, pathname);
3175 PyMem_Free(pathname);
3176 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003177}
3178
3179static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003180imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003181{
3182 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00003183 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003184 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003185 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003186}
3187
Christian Heimes13a7a212008-01-07 17:13:09 +00003188static PyObject *
3189imp_reload(PyObject *self, PyObject *v)
3190{
3191 return PyImport_ReloadModule(v);
3192}
3193
3194PyDoc_STRVAR(doc_reload,
3195"reload(module) -> module\n\
3196\n\
3197Reload the module. The module must have been successfully imported before.");
3198
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003199/* Doc strings */
3200
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003201PyDoc_STRVAR(doc_imp,
3202"This module provides the components needed to build your own\n\
3203__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003204
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003205PyDoc_STRVAR(doc_find_module,
3206"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003207Search for a module. If path is omitted or None, search for a\n\
3208built-in, frozen or special module and continue search in sys.path.\n\
3209The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003210package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003211
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003212PyDoc_STRVAR(doc_load_module,
3213"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003214Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003215The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003217PyDoc_STRVAR(doc_get_magic,
3218"get_magic() -> string\n\
3219Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003220
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003221PyDoc_STRVAR(doc_get_suffixes,
3222"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003223Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003224that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003225
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003226PyDoc_STRVAR(doc_new_module,
3227"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003228Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003229The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003230
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003231PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00003232"lock_held() -> boolean\n\
3233Return True if the import lock is currently held, else False.\n\
3234On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00003235
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003236PyDoc_STRVAR(doc_acquire_lock,
3237"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00003238Acquires the interpreter's import lock for the current thread.\n\
3239This lock should be used by import hooks to ensure thread-safety\n\
3240when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003241On platforms without threads, this function does nothing.");
3242
3243PyDoc_STRVAR(doc_release_lock,
3244"release_lock() -> None\n\
3245Release the interpreter's import lock.\n\
3246On platforms without threads, this function does nothing.");
3247
Guido van Rossum79f25d91997-04-29 20:08:16 +00003248static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00003249 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3250 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3251 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3252 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3253 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3254 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3255 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3256 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Christian Heimes13a7a212008-01-07 17:13:09 +00003257 {"reload", imp_reload, METH_O, doc_reload},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003258 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00003259 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
Brett Cannon8d110132009-03-15 02:20:16 +00003260 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00003261 {"init_builtin", imp_init_builtin, METH_VARARGS},
3262 {"init_frozen", imp_init_frozen, METH_VARARGS},
3263 {"is_builtin", imp_is_builtin, METH_VARARGS},
3264 {"is_frozen", imp_is_frozen, METH_VARARGS},
3265 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003266#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00003267 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003268#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00003269 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00003270 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003271 {NULL, NULL} /* sentinel */
3272};
3273
Guido van Rossum1a8791e1998-08-04 22:46:29 +00003274static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003275setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003276{
3277 PyObject *v;
3278 int err;
3279
Christian Heimes217cfd12007-12-02 14:31:20 +00003280 v = PyLong_FromLong((long)value);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003281 err = PyDict_SetItemString(d, name, v);
3282 Py_XDECREF(v);
3283 return err;
3284}
3285
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003286typedef struct {
3287 PyObject_HEAD
3288} NullImporter;
3289
3290static int
3291NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3292{
3293 char *path;
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003294 Py_ssize_t pathlen;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003295
3296 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3297 return -1;
3298
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00003299 if (!PyArg_ParseTuple(args, "es:NullImporter",
3300 Py_FileSystemDefaultEncoding, &path))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003301 return -1;
3302
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003303 pathlen = strlen(path);
3304 if (pathlen == 0) {
Georg Brandl8494d572008-07-19 10:13:15 +00003305 PyMem_Free(path);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003306 PyErr_SetString(PyExc_ImportError, "empty pathname");
3307 return -1;
3308 } else {
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003309#ifndef MS_WINDOWS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003310 struct stat statbuf;
3311 int rv;
3312
3313 rv = stat(path, &statbuf);
Georg Brandl8494d572008-07-19 10:13:15 +00003314 PyMem_Free(path);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003315 if (rv == 0) {
3316 /* it exists */
3317 if (S_ISDIR(statbuf.st_mode)) {
3318 /* it's a directory */
3319 PyErr_SetString(PyExc_ImportError,
3320 "existing directory");
3321 return -1;
3322 }
3323 }
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003324#else /* MS_WINDOWS */
3325 DWORD rv;
3326 /* see issue1293 and issue3677:
3327 * stat() on Windows doesn't recognise paths like
3328 * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
3329 */
3330 rv = GetFileAttributesA(path);
Kristján Valur Jónsson92cb4382009-01-24 10:33:25 +00003331 PyMem_Free(path);
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003332 if (rv != INVALID_FILE_ATTRIBUTES) {
3333 /* it exists */
3334 if (rv & FILE_ATTRIBUTE_DIRECTORY) {
3335 /* it's a directory */
3336 PyErr_SetString(PyExc_ImportError,
3337 "existing directory");
3338 return -1;
3339 }
3340 }
3341#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003342 }
3343 return 0;
3344}
3345
3346static PyObject *
3347NullImporter_find_module(NullImporter *self, PyObject *args)
3348{
3349 Py_RETURN_NONE;
3350}
3351
3352static PyMethodDef NullImporter_methods[] = {
3353 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3354 "Always return None"
3355 },
3356 {NULL} /* Sentinel */
3357};
3358
3359
Christian Heimes9cd17752007-11-18 19:35:23 +00003360PyTypeObject PyNullImporter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003361 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003362 "imp.NullImporter", /*tp_name*/
3363 sizeof(NullImporter), /*tp_basicsize*/
3364 0, /*tp_itemsize*/
3365 0, /*tp_dealloc*/
3366 0, /*tp_print*/
3367 0, /*tp_getattr*/
3368 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00003369 0, /*tp_reserved*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003370 0, /*tp_repr*/
3371 0, /*tp_as_number*/
3372 0, /*tp_as_sequence*/
3373 0, /*tp_as_mapping*/
3374 0, /*tp_hash */
3375 0, /*tp_call*/
3376 0, /*tp_str*/
3377 0, /*tp_getattro*/
3378 0, /*tp_setattro*/
3379 0, /*tp_as_buffer*/
3380 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3381 "Null importer object", /* tp_doc */
3382 0, /* tp_traverse */
3383 0, /* tp_clear */
3384 0, /* tp_richcompare */
3385 0, /* tp_weaklistoffset */
3386 0, /* tp_iter */
3387 0, /* tp_iternext */
3388 NullImporter_methods, /* tp_methods */
3389 0, /* tp_members */
3390 0, /* tp_getset */
3391 0, /* tp_base */
3392 0, /* tp_dict */
3393 0, /* tp_descr_get */
3394 0, /* tp_descr_set */
3395 0, /* tp_dictoffset */
3396 (initproc)NullImporter_init, /* tp_init */
3397 0, /* tp_alloc */
3398 PyType_GenericNew /* tp_new */
3399};
3400
Martin v. Löwis1a214512008-06-11 05:26:20 +00003401static struct PyModuleDef impmodule = {
3402 PyModuleDef_HEAD_INIT,
3403 "imp",
3404 doc_imp,
3405 0,
3406 imp_methods,
3407 NULL,
3408 NULL,
3409 NULL,
3410 NULL
3411};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003412
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003413PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003414PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003415{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003416 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003417
Christian Heimes9cd17752007-11-18 19:35:23 +00003418 if (PyType_Ready(&PyNullImporter_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003419 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003420
Martin v. Löwis1a214512008-06-11 05:26:20 +00003421 m = PyModule_Create(&impmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003422 if (m == NULL)
3423 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003424 d = PyModule_GetDict(m);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003425 if (d == NULL)
3426 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003427
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003428 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3429 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3430 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3431 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3432 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3433 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3434 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3435 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003436 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003437 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003438
Christian Heimes9cd17752007-11-18 19:35:23 +00003439 Py_INCREF(&PyNullImporter_Type);
3440 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
Martin v. Löwis1a214512008-06-11 05:26:20 +00003441 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003442 failure:
Martin v. Löwis1a214512008-06-11 05:26:20 +00003443 Py_XDECREF(m);
3444 return NULL;
3445
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003446}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003447
3448
Guido van Rossumb18618d2000-05-03 23:44:39 +00003449/* API for embedding applications that want to add their own entries
3450 to the table of built-in modules. This should normally be called
3451 *before* Py_Initialize(). When the table resize fails, -1 is
3452 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003453
3454 After a similar function by Just van Rossum. */
3455
3456int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003457PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003458{
3459 static struct _inittab *our_copy = NULL;
3460 struct _inittab *p;
3461 int i, n;
3462
3463 /* Count the number of entries in both tables */
3464 for (n = 0; newtab[n].name != NULL; n++)
3465 ;
3466 if (n == 0)
3467 return 0; /* Nothing to do */
3468 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3469 ;
3470
3471 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003472 p = our_copy;
3473 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003474 if (p == NULL)
3475 return -1;
3476
3477 /* Copy the tables into the new memory */
3478 if (our_copy != PyImport_Inittab)
3479 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3480 PyImport_Inittab = our_copy = p;
3481 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3482
3483 return 0;
3484}
3485
3486/* Shorthand to add a single entry given a name and a function */
3487
3488int
Brett Cannona826f322009-04-02 03:41:46 +00003489PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003490{
3491 struct _inittab newtab[2];
3492
3493 memset(newtab, '\0', sizeof newtab);
3494
Brett Cannona826f322009-04-02 03:41:46 +00003495 newtab[0].name = (char *)name;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003496 newtab[0].initfunc = initfunc;
3497
3498 return PyImport_ExtendInittab(newtab);
3499}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003500
3501#ifdef __cplusplus
3502}
3503#endif