blob: 1129a7f9f8dc3f6098d6bbc09aa7a1eab910874b [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)
Tim Peters36515e22001-11-18 04:06:29 +000092*/
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +000093#define MAGIC (3150 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000094
Guido van Rossum96774c12000-05-01 20:19:08 +000095/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000096 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000097 compiler works which are enabled by command line switches. */
98static long pyc_magic = MAGIC;
99
Guido van Rossum25ce5661997-08-02 03:10:38 +0000100/* See _PyImport_FixupExtension() below */
101static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102
Guido van Rossum771c6c81997-10-31 18:37:24 +0000103/* This table is defined in config.c: */
104extern struct _inittab _PyImport_Inittab[];
105
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000106/* Method from Parser/tokenizer.c */
Guido van Rossum40d20bc2007-10-22 00:09:51 +0000107extern char * PyTokenizer_FindEncoding(int);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000108
Guido van Rossum771c6c81997-10-31 18:37:24 +0000109struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +0000110
Guido van Rossumed1170e1999-12-20 21:23:41 +0000111/* these tables define the module suffixes that Python recognizes */
112struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000113
Guido van Rossumed1170e1999-12-20 21:23:41 +0000114static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +0000115 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000116#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +0000117 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000118#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000119 {".pyc", "rb", PY_COMPILED},
120 {0, 0}
121};
122
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000123
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000124/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125
126void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000127_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000129 const struct filedescr *scan;
130 struct filedescr *filetab;
131 int countD = 0;
132 int countS = 0;
133
134 /* prepare _PyImport_Filetab: copy entries from
135 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
136 */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000137#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000138 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
139 ++countD;
Guido van Rossum04110fb2007-08-24 16:32:05 +0000140#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000141 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
142 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000143 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000144 if (filetab == NULL)
145 Py_FatalError("Can't initialize import file table.");
Guido van Rossum04110fb2007-08-24 16:32:05 +0000146#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000147 memcpy(filetab, _PyImport_DynLoadFiletab,
148 countD * sizeof(struct filedescr));
Guido van Rossum04110fb2007-08-24 16:32:05 +0000149#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000150 memcpy(filetab + countD, _PyImport_StandardFiletab,
151 countS * sizeof(struct filedescr));
152 filetab[countD + countS].suffix = NULL;
153
154 _PyImport_Filetab = filetab;
155
Guido van Rossum0824f631997-03-11 18:37:35 +0000156 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000157 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
158 for (; filetab->suffix != NULL; filetab++) {
159 if (strcmp(filetab->suffix, ".pyc") == 0)
160 filetab->suffix = ".pyo";
Guido van Rossum0824f631997-03-11 18:37:35 +0000161 }
162 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000163
Guido van Rossum572dbf82007-04-27 23:53:51 +0000164 {
Guido van Rossum96774c12000-05-01 20:19:08 +0000165 /* Fix the pyc_magic so that byte compiled code created
166 using the all-Unicode method doesn't interfere with
167 code created in normal operation mode. */
168 pyc_magic = MAGIC + 1;
169 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
Guido van Rossum25ce5661997-08-02 03:10:38 +0000172void
Just van Rossum52e14d62002-12-30 22:08:05 +0000173_PyImportHooks_Init(void)
174{
175 PyObject *v, *path_hooks = NULL, *zimpimport;
176 int err = 0;
177
178 /* adding sys.path_hooks and sys.path_importer_cache, setting up
179 zipimport */
Christian Heimes9cd17752007-11-18 19:35:23 +0000180 if (PyType_Ready(&PyNullImporter_Type) < 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000181 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000182
183 if (Py_VerboseFlag)
184 PySys_WriteStderr("# installing zipimport hook\n");
185
186 v = PyList_New(0);
187 if (v == NULL)
188 goto error;
189 err = PySys_SetObject("meta_path", v);
190 Py_DECREF(v);
191 if (err)
192 goto error;
193 v = PyDict_New();
194 if (v == NULL)
195 goto error;
196 err = PySys_SetObject("path_importer_cache", v);
197 Py_DECREF(v);
198 if (err)
199 goto error;
200 path_hooks = PyList_New(0);
201 if (path_hooks == NULL)
202 goto error;
203 err = PySys_SetObject("path_hooks", path_hooks);
204 if (err) {
205 error:
206 PyErr_Print();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000207 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
208 "path_importer_cache, or NullImporter failed"
209 );
Just van Rossum52e14d62002-12-30 22:08:05 +0000210 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000211
Just van Rossum52e14d62002-12-30 22:08:05 +0000212 zimpimport = PyImport_ImportModule("zipimport");
213 if (zimpimport == NULL) {
214 PyErr_Clear(); /* No zip import module -- okay */
215 if (Py_VerboseFlag)
216 PySys_WriteStderr("# can't import zipimport\n");
217 }
218 else {
219 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
220 "zipimporter");
221 Py_DECREF(zimpimport);
222 if (zipimporter == NULL) {
223 PyErr_Clear(); /* No zipimporter object -- okay */
224 if (Py_VerboseFlag)
225 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000226 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000227 }
228 else {
229 /* sys.path_hooks.append(zipimporter) */
230 err = PyList_Append(path_hooks, zipimporter);
231 Py_DECREF(zipimporter);
232 if (err)
233 goto error;
234 if (Py_VerboseFlag)
235 PySys_WriteStderr(
236 "# installed zipimport hook\n");
237 }
238 }
239 Py_DECREF(path_hooks);
240}
241
242void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000243_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000244{
245 Py_XDECREF(extensions);
246 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000247 PyMem_DEL(_PyImport_Filetab);
248 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000249}
250
251
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000252/* Locking primitives to prevent parallel imports of the same module
253 in different threads to return with a partially loaded module.
254 These calls are serialized by the global interpreter lock. */
255
256#ifdef WITH_THREAD
257
Guido van Rossum49b56061998-10-01 20:42:43 +0000258#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000259
Guido van Rossum65d5b571998-12-21 19:32:43 +0000260static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000261static long import_lock_thread = -1;
262static int import_lock_level = 0;
263
Benjamin Peterson51838562009-10-04 20:35:30 +0000264void
265_PyImport_AcquireLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000266{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000267 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000268 if (me == -1)
269 return; /* Too bad */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000270 if (import_lock == NULL) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000271 import_lock = PyThread_allocate_lock();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000272 if (import_lock == NULL)
273 return; /* Nothing much we can do. */
274 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000275 if (import_lock_thread == me) {
276 import_lock_level++;
277 return;
278 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000279 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
280 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000281 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000282 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000283 PyEval_RestoreThread(tstate);
284 }
285 import_lock_thread = me;
286 import_lock_level = 1;
287}
288
Benjamin Peterson51838562009-10-04 20:35:30 +0000289int
290_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000291{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000292 long me = PyThread_get_thread_ident();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000293 if (me == -1 || import_lock == NULL)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000294 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000295 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000296 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000297 import_lock_level--;
298 if (import_lock_level == 0) {
299 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000300 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000301 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000302 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000303}
304
Benjamin Peterson51838562009-10-04 20:35:30 +0000305/* This function used to be called from PyOS_AfterFork to ensure that newly
306 created child processes do not share locks with the parent, but for some
307 reason only on AIX systems. Instead of re-initializing the lock, we now
308 acquire the import lock around fork() calls. */
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000309
310void
311_PyImport_ReInitLock(void)
312{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000313}
314
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000315#endif
316
Tim Peters69232342001-08-30 05:16:13 +0000317static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000318imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000319{
Tim Peters69232342001-08-30 05:16:13 +0000320#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000321 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000322#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000323 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000324#endif
325}
326
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000327static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000328imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000329{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000330#ifdef WITH_THREAD
Benjamin Peterson51838562009-10-04 20:35:30 +0000331 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000332#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000333 Py_INCREF(Py_None);
334 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000335}
336
337static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000338imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000339{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000340#ifdef WITH_THREAD
Benjamin Peterson51838562009-10-04 20:35:30 +0000341 if (_PyImport_ReleaseLock() < 0) {
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000342 PyErr_SetString(PyExc_RuntimeError,
343 "not holding the import lock");
344 return NULL;
345 }
346#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000347 Py_INCREF(Py_None);
348 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000349}
350
Guido van Rossumd8faa362007-04-27 19:54:29 +0000351static void
352imp_modules_reloading_clear(void)
353{
354 PyInterpreterState *interp = PyThreadState_Get()->interp;
355 if (interp->modules_reloading != NULL)
356 PyDict_Clear(interp->modules_reloading);
357}
358
Guido van Rossum25ce5661997-08-02 03:10:38 +0000359/* Helper for sys */
360
361PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000362PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000363{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000364 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000365 if (interp->modules == NULL)
366 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
367 return interp->modules;
368}
369
Guido van Rossum3f5da241990-12-20 15:06:42 +0000370
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000371/* List of names to clear in sys */
372static char* sys_deletes[] = {
Collin Winter670e6922007-03-21 02:57:17 +0000373 "path", "argv", "ps1", "ps2",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000374 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000375 "path_hooks", "path_importer_cache", "meta_path",
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000376 /* misc stuff */
377 "flags", "float_info",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000378 NULL
379};
380
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000381static char* sys_files[] = {
382 "stdin", "__stdin__",
383 "stdout", "__stdout__",
384 "stderr", "__stderr__",
385 NULL
386};
387
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000388
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000389/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000390
Guido van Rossum3f5da241990-12-20 15:06:42 +0000391void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000392PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000393{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000394 Py_ssize_t pos, ndone;
Guido van Rossum758eec01998-01-19 21:58:26 +0000395 char *name;
396 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000397 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000398 PyObject *modules = interp->modules;
399
400 if (modules == NULL)
401 return; /* Already done */
402
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000403 /* Delete some special variables first. These are common
404 places where user values hide and people complain when their
405 destructors fail. Since the modules containing them are
406 deleted *last* of all, they would come too late in the normal
407 destruction order. Sigh. */
408
Georg Brandl1a3284e2007-12-02 09:40:06 +0000409 value = PyDict_GetItemString(modules, "builtins");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000410 if (value != NULL && PyModule_Check(value)) {
411 dict = PyModule_GetDict(value);
412 if (Py_VerboseFlag)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000413 PySys_WriteStderr("# clear builtins._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000414 PyDict_SetItemString(dict, "_", Py_None);
415 }
416 value = PyDict_GetItemString(modules, "sys");
417 if (value != NULL && PyModule_Check(value)) {
418 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000419 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000420 dict = PyModule_GetDict(value);
421 for (p = sys_deletes; *p != NULL; p++) {
422 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000423 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000424 PyDict_SetItemString(dict, *p, Py_None);
425 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000426 for (p = sys_files; *p != NULL; p+=2) {
427 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000428 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000429 v = PyDict_GetItemString(dict, *(p+1));
430 if (v == NULL)
431 v = Py_None;
432 PyDict_SetItemString(dict, *p, v);
433 }
434 }
435
436 /* First, delete __main__ */
437 value = PyDict_GetItemString(modules, "__main__");
438 if (value != NULL && PyModule_Check(value)) {
439 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000440 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000441 _PyModule_Clear(value);
442 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000443 }
444
Georg Brandl1a3284e2007-12-02 09:40:06 +0000445 /* The special treatment of "builtins" here is because even
Guido van Rossum758eec01998-01-19 21:58:26 +0000446 when it's not referenced as a module, its dictionary is
447 referenced by almost every module's __builtins__. Since
448 deleting a module clears its dictionary (even if there are
Georg Brandl1a3284e2007-12-02 09:40:06 +0000449 references left to it), we need to delete the "builtins"
Guido van Rossum758eec01998-01-19 21:58:26 +0000450 module last. Likewise, we don't delete sys until the very
451 end because it is implicitly referenced (e.g. by print).
452
453 Also note that we 'delete' modules by replacing their entry
454 in the modules dict with None, rather than really deleting
455 them; this avoids a rehash of the modules dictionary and
456 also marks them as "non existent" so they won't be
457 re-imported. */
458
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000459 /* Next, repeatedly delete modules with a reference count of
Georg Brandl1a3284e2007-12-02 09:40:06 +0000460 one (skipping builtins and sys) and delete them */
Guido van Rossum758eec01998-01-19 21:58:26 +0000461 do {
462 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000463 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000464 while (PyDict_Next(modules, &pos, &key, &value)) {
465 if (value->ob_refcnt != 1)
466 continue;
Neal Norwitz80e7f272007-08-26 06:45:23 +0000467 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000468 name = _PyUnicode_AsString(key);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000469 if (strcmp(name, "builtins") == 0)
Guido van Rossum758eec01998-01-19 21:58:26 +0000470 continue;
471 if (strcmp(name, "sys") == 0)
472 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000473 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000474 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000475 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000476 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000477 PyDict_SetItem(modules, key, Py_None);
478 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000479 }
480 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000481 } while (ndone > 0);
482
Georg Brandl1a3284e2007-12-02 09:40:06 +0000483 /* Next, delete all modules (still skipping builtins and sys) */
Guido van Rossum758eec01998-01-19 21:58:26 +0000484 pos = 0;
485 while (PyDict_Next(modules, &pos, &key, &value)) {
Neal Norwitz80e7f272007-08-26 06:45:23 +0000486 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000487 name = _PyUnicode_AsString(key);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000488 if (strcmp(name, "builtins") == 0)
Guido van Rossum758eec01998-01-19 21:58:26 +0000489 continue;
490 if (strcmp(name, "sys") == 0)
491 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000492 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000493 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000494 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000495 PyDict_SetItem(modules, key, Py_None);
496 }
497 }
498
Georg Brandl1a3284e2007-12-02 09:40:06 +0000499 /* Next, delete sys and builtins (in that order) */
Guido van Rossum758eec01998-01-19 21:58:26 +0000500 value = PyDict_GetItemString(modules, "sys");
501 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000502 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000503 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000504 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000505 PyDict_SetItemString(modules, "sys", Py_None);
506 }
Georg Brandl1a3284e2007-12-02 09:40:06 +0000507 value = PyDict_GetItemString(modules, "builtins");
Guido van Rossum758eec01998-01-19 21:58:26 +0000508 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000509 if (Py_VerboseFlag)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000510 PySys_WriteStderr("# cleanup builtins\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000511 _PyModule_Clear(value);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000512 PyDict_SetItemString(modules, "builtins", Py_None);
Guido van Rossum758eec01998-01-19 21:58:26 +0000513 }
514
515 /* Finally, clear and delete the modules directory */
516 PyDict_Clear(modules);
517 interp->modules = NULL;
518 Py_DECREF(modules);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000519 Py_CLEAR(interp->modules_reloading);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000520}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000521
522
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000523/* Helper for pythonrun.c -- return magic number */
524
525long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000526PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000527{
Guido van Rossum96774c12000-05-01 20:19:08 +0000528 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000529}
530
531
Guido van Rossum25ce5661997-08-02 03:10:38 +0000532/* Magic for extension modules (built-in as well as dynamically
533 loaded). To prevent initializing an extension module more than
534 once, we keep a static dictionary 'extensions' keyed by module name
535 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000536 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000537 dictionary is stored by calling _PyImport_FixupExtension()
538 immediately after the module initialization function succeeds. A
539 copy can be retrieved from there by calling
Martin v. Löwis1a214512008-06-11 05:26:20 +0000540 _PyImport_FindExtension().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000541
Martin v. Löwis1a214512008-06-11 05:26:20 +0000542 Modules which do support multiple multiple initialization set
543 their m_size field to a non-negative number (indicating the size
544 of the module-specific state). They are still recorded in the
545 extensions dictionary, to avoid loading shared libraries twice.
546*/
547
548int
549_PyImport_FixupExtension(PyObject *mod, char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000551 PyObject *modules, *dict;
552 struct PyModuleDef *def;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000553 if (extensions == NULL) {
554 extensions = PyDict_New();
555 if (extensions == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000556 return -1;
557 }
558 if (mod == NULL || !PyModule_Check(mod)) {
559 PyErr_BadInternalCall();
560 return -1;
561 }
562 def = PyModule_GetDef(mod);
563 if (!def) {
564 PyErr_BadInternalCall();
565 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000566 }
567 modules = PyImport_GetModuleDict();
Martin v. Löwis1a214512008-06-11 05:26:20 +0000568 if (PyDict_SetItemString(modules, name, mod) < 0)
569 return -1;
570 if (_PyState_AddModule(mod, def) < 0) {
571 PyDict_DelItemString(modules, name);
572 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000573 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000574 if (def->m_size == -1) {
575 if (def->m_base.m_copy) {
576 /* Somebody already imported the module,
577 likely under a different name.
578 XXX this should really not happen. */
579 Py_DECREF(def->m_base.m_copy);
580 def->m_base.m_copy = NULL;
581 }
582 dict = PyModule_GetDict(mod);
583 if (dict == NULL)
584 return -1;
585 def->m_base.m_copy = PyDict_Copy(dict);
586 if (def->m_base.m_copy == NULL)
587 return -1;
588 }
589 PyDict_SetItemString(extensions, filename, (PyObject*)def);
590 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000591}
592
593PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000594_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000595{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000596 PyObject *mod, *mdict;
597 PyModuleDef* def;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000598 if (extensions == NULL)
599 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000600 def = (PyModuleDef*)PyDict_GetItemString(extensions, filename);
601 if (def == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000602 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000603 if (def->m_size == -1) {
604 /* Module does not support repeated initialization */
605 if (def->m_base.m_copy == NULL)
606 return NULL;
607 mod = PyImport_AddModule(name);
608 if (mod == NULL)
609 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000610 mdict = PyModule_GetDict(mod);
611 if (mdict == NULL)
612 return NULL;
613 if (PyDict_Update(mdict, def->m_base.m_copy))
614 return NULL;
615 }
616 else {
617 if (def->m_base.m_init == NULL)
618 return NULL;
619 mod = def->m_base.m_init();
620 if (mod == NULL)
621 return NULL;
622 PyDict_SetItemString(PyImport_GetModuleDict(), name, mod);
Benjamin Petersonad956532008-09-04 02:28:15 +0000623 Py_DECREF(mod);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000624 }
625 if (_PyState_AddModule(mod, def) < 0) {
626 PyDict_DelItemString(PyImport_GetModuleDict(), name);
627 Py_DECREF(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000628 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000629 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000630 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000631 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Martin v. Löwis1a214512008-06-11 05:26:20 +0000632 name, filename);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000633 return mod;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000634
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000635}
636
637
638/* Get the module object corresponding to a module name.
639 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000640 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000641 Because the former action is most common, THIS DOES NOT RETURN A
642 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000643
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000645PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000646{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000647 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000648 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000649
Guido van Rossum25ce5661997-08-02 03:10:38 +0000650 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000651 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000652 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000653 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000654 if (m == NULL)
655 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000656 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000658 return NULL;
659 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000660 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000661
662 return m;
663}
664
Tim Peters1cd70172004-08-02 03:52:12 +0000665/* Remove name from sys.modules, if it's there. */
666static void
667_RemoveModule(const char *name)
668{
669 PyObject *modules = PyImport_GetModuleDict();
670 if (PyDict_GetItemString(modules, name) == NULL)
671 return;
672 if (PyDict_DelItemString(modules, name) < 0)
673 Py_FatalError("import: deleting existing key in"
674 "sys.modules failed");
675}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676
Christian Heimes3b06e532008-01-07 20:12:44 +0000677static PyObject * get_sourcefile(const char *file);
678
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000679/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000680 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
681 * removed from sys.modules, to avoid leaving damaged module objects
682 * in sys.modules. The caller may wish to restore the original
683 * module object (if any) in this case; PyImport_ReloadModule is an
684 * example.
685 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000687PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000688{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000689 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
690}
691
692PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000693PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000694{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000695 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000696 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000697
Guido van Rossum79f25d91997-04-29 20:08:16 +0000698 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000699 if (m == NULL)
700 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000701 /* If the module is being reloaded, we get the old module back
702 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000703 d = PyModule_GetDict(m);
704 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
705 if (PyDict_SetItemString(d, "__builtins__",
706 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000707 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000708 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000709 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000710 v = NULL;
711 if (pathname != NULL) {
Christian Heimes3b06e532008-01-07 20:12:44 +0000712 v = get_sourcefile(pathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000713 if (v == NULL)
714 PyErr_Clear();
715 }
716 if (v == NULL) {
717 v = ((PyCodeObject *)co)->co_filename;
718 Py_INCREF(v);
719 }
720 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000721 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000722 Py_DECREF(v);
723
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000724 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000725 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000726 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000727 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000728
Guido van Rossum25ce5661997-08-02 03:10:38 +0000729 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000730 PyErr_Format(PyExc_ImportError,
731 "Loaded module %.200s not found in sys.modules",
732 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000733 return NULL;
734 }
735
Guido van Rossum79f25d91997-04-29 20:08:16 +0000736 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000737
738 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000739
740 error:
741 _RemoveModule(name);
742 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000743}
744
745
746/* Given a pathname for a Python source file, fill a buffer with the
747 pathname for the corresponding compiled file. Return the pathname
748 for the compiled file, or NULL if there's no space in the buffer.
749 Doesn't set an exception. */
750
751static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000752make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000753{
Tim Petersc1731372001-08-04 08:12:36 +0000754 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000755 if (len+2 > buflen)
756 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000757
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000758#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000759 /* Treat .pyw as if it were .py. The case of ".pyw" must match
760 that used in _PyImport_StandardFiletab. */
761 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
762 --len; /* pretend 'w' isn't there */
763#endif
764 memcpy(buf, pathname, len);
765 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
766 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000767
768 return buf;
769}
770
771
772/* Given a pathname for a Python source file, its time of last
773 modification, and a pathname for a compiled file, check whether the
774 compiled file represents the same version of the source. If so,
775 return a FILE pointer for the compiled file, positioned just after
776 the header; if not, return NULL.
777 Doesn't set an exception. */
778
779static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000780check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000781{
782 FILE *fp;
783 long magic;
784 long pyc_mtime;
785
786 fp = fopen(cpathname, "rb");
787 if (fp == NULL)
788 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000789 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000790 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000791 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000792 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000793 fclose(fp);
794 return NULL;
795 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000796 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000797 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000798 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000799 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000800 fclose(fp);
801 return NULL;
802 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000803 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000804 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000805 return fp;
806}
807
808
809/* Read a code object from a file and check it for validity */
810
Guido van Rossum79f25d91997-04-29 20:08:16 +0000811static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000812read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000813{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000814 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000815
Tim Petersd9b9ac82001-01-28 00:27:39 +0000816 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000817 if (co == NULL)
818 return NULL;
819 if (!PyCode_Check(co)) {
820 PyErr_Format(PyExc_ImportError,
821 "Non-code object in %.200s", cpathname);
822 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000823 return NULL;
824 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000826}
827
828
829/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000830 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000831
Guido van Rossum79f25d91997-04-29 20:08:16 +0000832static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000833load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000834{
835 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000836 PyCodeObject *co;
837 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000838
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000840 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000841 PyErr_Format(PyExc_ImportError,
842 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000843 return NULL;
844 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000845 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000846 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000847 if (co == NULL)
848 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000849 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000850 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000851 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000852 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000853 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000854
855 return m;
856}
857
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000858/* Parse a source file and return the corresponding code object */
859
Guido van Rossum79f25d91997-04-29 20:08:16 +0000860static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000862{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 PyCodeObject *co = NULL;
864 mod_ty mod;
Christian Heimes4d6ec852008-03-26 22:34:47 +0000865 PyCompilerFlags flags;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000866 PyArena *arena = PyArena_New();
867 if (arena == NULL)
868 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000869
Christian Heimesb1b3efc2008-03-26 23:24:27 +0000870 flags.cf_flags = 0;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000871 mod = PyParser_ASTFromFile(fp, pathname, NULL,
Christian Heimes4d6ec852008-03-26 22:34:47 +0000872 Py_file_input, 0, 0, &flags,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000873 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000875 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000877 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000878 return co;
879}
880
881
Guido van Rossum55a83382000-09-20 20:31:38 +0000882/* Helper to open a bytecode file for writing in exclusive mode */
883
884static FILE *
Christian Heimes05e8be12008-02-23 18:30:17 +0000885open_exclusive(char *filename, mode_t mode)
Guido van Rossum55a83382000-09-20 20:31:38 +0000886{
887#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
888 /* Use O_EXCL to avoid a race condition when another process tries to
889 write the same file. When that happens, our open() call fails,
890 which is just fine (since it's only a cache).
891 XXX If the file exists and is writable but the directory is not
892 writable, the file will never be written. Oh well.
893 */
894 int fd;
895 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000896 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
897#ifdef O_BINARY
898 |O_BINARY /* necessary for Windows */
899#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000900#ifdef __VMS
Christian Heimes05e8be12008-02-23 18:30:17 +0000901 , mode, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000902#else
Christian Heimes05e8be12008-02-23 18:30:17 +0000903 , mode
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000904#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000905 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000906 if (fd < 0)
907 return NULL;
908 return fdopen(fd, "wb");
909#else
910 /* Best we can do -- on Windows this can't happen anyway */
911 return fopen(filename, "wb");
912#endif
913}
914
915
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000916/* Write a compiled module to a file, placing the time of last
917 modification of its source into the header.
918 Errors are ignored, if a write error occurs an attempt is made to
919 remove the file. */
920
921static void
Christian Heimes05e8be12008-02-23 18:30:17 +0000922write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000923{
924 FILE *fp;
Christian Heimes05e8be12008-02-23 18:30:17 +0000925 time_t mtime = srcstat->st_mtime;
R. David Murray07fc01f2009-07-19 01:59:05 +0000926#ifdef MS_WINDOWS /* since Windows uses different permissions */
927 mode_t mode = srcstat->st_mode & ~S_IEXEC;
928#else
929 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;
930#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000931
Christian Heimes05e8be12008-02-23 18:30:17 +0000932 fp = open_exclusive(cpathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000933 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000934 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000935 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000936 "# can't create %s\n", cpathname);
937 return;
938 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000939 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000940 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000941 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
942 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000943 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000944 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000945 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000946 /* Don't keep partial file */
947 fclose(fp);
948 (void) unlink(cpathname);
949 return;
950 }
951 /* Now write the true mtime */
952 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000953 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000954 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000955 fflush(fp);
956 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000957 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000958 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000959}
960
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000961static void
962update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
963{
964 PyObject *constants, *tmp;
965 Py_ssize_t i, n;
966
967 if (PyUnicode_Compare(co->co_filename, oldname))
968 return;
969
970 tmp = co->co_filename;
971 co->co_filename = newname;
972 Py_INCREF(co->co_filename);
973 Py_DECREF(tmp);
974
975 constants = co->co_consts;
976 n = PyTuple_GET_SIZE(constants);
977 for (i = 0; i < n; i++) {
978 tmp = PyTuple_GET_ITEM(constants, i);
979 if (PyCode_Check(tmp))
980 update_code_filenames((PyCodeObject *)tmp,
981 oldname, newname);
982 }
983}
984
985static int
986update_compiled_module(PyCodeObject *co, char *pathname)
987{
988 PyObject *oldname, *newname;
989
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000990 newname = PyUnicode_DecodeFSDefault(pathname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000991 if (newname == NULL)
992 return -1;
993
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000994 if (!PyUnicode_Compare(co->co_filename, newname)) {
995 Py_DECREF(newname);
996 return 0;
997 }
998
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000999 oldname = co->co_filename;
1000 Py_INCREF(oldname);
1001 update_code_filenames(co, oldname, newname);
1002 Py_DECREF(oldname);
1003 Py_DECREF(newname);
1004 return 1;
1005}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001006
1007/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001008 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
1009 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001010
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001012load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001013{
Christian Heimes05e8be12008-02-23 18:30:17 +00001014 struct stat st;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001015 FILE *fpc;
1016 char buf[MAXPATHLEN+1];
1017 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001018 PyCodeObject *co;
1019 PyObject *m;
Christian Heimes05e8be12008-02-23 18:30:17 +00001020
1021 if (fstat(fileno(fp), &st) != 0) {
Neal Norwitz708e51a2005-10-03 04:48:15 +00001022 PyErr_Format(PyExc_RuntimeError,
Christian Heimes05e8be12008-02-23 18:30:17 +00001023 "unable to get file status from '%s'",
Neal Norwitz708e51a2005-10-03 04:48:15 +00001024 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +00001025 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +00001026 }
Fred Drake4c82b232000-06-30 16:18:57 +00001027#if SIZEOF_TIME_T > 4
1028 /* Python's .pyc timestamp handling presumes that the timestamp fits
1029 in 4 bytes. This will be fine until sometime in the year 2038,
1030 when a 4-byte signed time_t will overflow.
1031 */
Christian Heimes05e8be12008-02-23 18:30:17 +00001032 if (st.st_mtime >> 32) {
Fred Drake4c82b232000-06-30 16:18:57 +00001033 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +00001034 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +00001035 return NULL;
1036 }
1037#endif
Tim Peters36515e22001-11-18 04:06:29 +00001038 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +00001039 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001040 if (cpathname != NULL &&
Christian Heimes05e8be12008-02-23 18:30:17 +00001041 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001042 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001043 fclose(fpc);
1044 if (co == NULL)
1045 return NULL;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001046 if (update_compiled_module(co, pathname) < 0)
1047 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001048 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001049 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001050 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001051 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001052 }
1053 else {
1054 co = parse_source_module(pathname, fp);
1055 if (co == NULL)
1056 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001057 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001058 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001059 name, pathname);
Christian Heimes790c8232008-01-07 21:14:23 +00001060 if (cpathname) {
1061 PyObject *ro = PySys_GetObject("dont_write_bytecode");
1062 if (ro == NULL || !PyObject_IsTrue(ro))
Christian Heimes05e8be12008-02-23 18:30:17 +00001063 write_compiled_module(co, cpathname, &st);
Christian Heimes790c8232008-01-07 21:14:23 +00001064 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001065 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001066 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001067 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001068
1069 return m;
1070}
1071
Christian Heimes3b06e532008-01-07 20:12:44 +00001072/* Get source file -> unicode or None
1073 * Returns the path to the py file if available, else the given path
1074 */
1075static PyObject *
1076get_sourcefile(const char *file)
1077{
1078 char py[MAXPATHLEN + 1];
1079 Py_ssize_t len;
1080 PyObject *u;
1081 struct stat statbuf;
1082
1083 if (!file || !*file) {
1084 Py_RETURN_NONE;
1085 }
1086
1087 len = strlen(file);
Christian Heimes3540b502008-01-11 07:03:05 +00001088 /* match '*.py?' */
1089 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {
Christian Heimes3b06e532008-01-07 20:12:44 +00001090 return PyUnicode_DecodeFSDefault(file);
1091 }
1092
1093 strncpy(py, file, len-1);
Martin v. Löwis5021ebc2008-03-22 22:07:13 +00001094 py[len-1] = '\0';
Christian Heimes3b06e532008-01-07 20:12:44 +00001095 if (stat(py, &statbuf) == 0 &&
1096 S_ISREG(statbuf.st_mode)) {
1097 u = PyUnicode_DecodeFSDefault(py);
1098 }
1099 else {
1100 u = PyUnicode_DecodeFSDefault(file);
1101 }
1102 return u;
1103}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001104
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001105/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001106static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
1107static struct filedescr *find_module(char *, char *, PyObject *,
1108 char *, size_t, FILE **, PyObject **);
Christian Heimes3b06e532008-01-07 20:12:44 +00001109static struct _frozen * find_frozen(char *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001110
1111/* Load a package and return its module object WITH INCREMENTED
1112 REFERENCE COUNT */
1113
1114static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001115load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001116{
Tim Peters1cd70172004-08-02 03:52:12 +00001117 PyObject *m, *d;
1118 PyObject *file = NULL;
1119 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001120 int err;
1121 char buf[MAXPATHLEN+1];
1122 FILE *fp = NULL;
1123 struct filedescr *fdp;
1124
1125 m = PyImport_AddModule(name);
1126 if (m == NULL)
1127 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001128 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001129 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001130 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001131 d = PyModule_GetDict(m);
Christian Heimes3b06e532008-01-07 20:12:44 +00001132 file = get_sourcefile(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001133 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +00001134 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001135 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +00001136 if (path == NULL)
1137 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001138 err = PyDict_SetItemString(d, "__file__", file);
1139 if (err == 0)
1140 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +00001141 if (err != 0)
1142 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001143 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001144 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001145 if (fdp == NULL) {
1146 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1147 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +00001148 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001149 }
1150 else
1151 m = NULL;
1152 goto cleanup;
1153 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001154 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001155 if (fp != NULL)
1156 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001157 goto cleanup;
1158
1159 error:
1160 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001161 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001162 Py_XDECREF(path);
1163 Py_XDECREF(file);
1164 return m;
1165}
1166
1167
1168/* Helper to test for built-in module */
1169
1170static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001171is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001172{
1173 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001174 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1175 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1176 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001177 return -1;
1178 else
1179 return 1;
1180 }
1181 }
1182 return 0;
1183}
1184
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001185
Just van Rossum52e14d62002-12-30 22:08:05 +00001186/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1187 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001188 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001189 that can handle the path item. Return None if no hook could;
1190 this tells our caller it should fall back to the builtin
1191 import mechanism. Cache the result in path_importer_cache.
1192 Returns a borrowed reference. */
1193
1194static PyObject *
1195get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1196 PyObject *p)
1197{
1198 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001199 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001200
1201 /* These conditions are the caller's responsibility: */
1202 assert(PyList_Check(path_hooks));
1203 assert(PyDict_Check(path_importer_cache));
1204
1205 nhooks = PyList_Size(path_hooks);
1206 if (nhooks < 0)
1207 return NULL; /* Shouldn't happen */
1208
1209 importer = PyDict_GetItem(path_importer_cache, p);
1210 if (importer != NULL)
1211 return importer;
1212
1213 /* set path_importer_cache[p] to None to avoid recursion */
1214 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1215 return NULL;
1216
1217 for (j = 0; j < nhooks; j++) {
1218 PyObject *hook = PyList_GetItem(path_hooks, j);
1219 if (hook == NULL)
1220 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001221 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001222 if (importer != NULL)
1223 break;
1224
1225 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1226 return NULL;
1227 }
1228 PyErr_Clear();
1229 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001230 if (importer == NULL) {
1231 importer = PyObject_CallFunctionObjArgs(
Christian Heimes9cd17752007-11-18 19:35:23 +00001232 (PyObject *)&PyNullImporter_Type, p, NULL
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001233 );
1234 if (importer == NULL) {
1235 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1236 PyErr_Clear();
1237 return Py_None;
1238 }
1239 }
1240 }
1241 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001242 int err = PyDict_SetItem(path_importer_cache, p, importer);
1243 Py_DECREF(importer);
1244 if (err != 0)
1245 return NULL;
1246 }
1247 return importer;
1248}
1249
Christian Heimes9cd17752007-11-18 19:35:23 +00001250PyAPI_FUNC(PyObject *)
1251PyImport_GetImporter(PyObject *path) {
1252 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
1253
1254 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1255 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1256 importer = get_path_importer(path_importer_cache,
1257 path_hooks, path);
1258 }
1259 }
1260 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1261 return importer;
1262}
1263
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001264/* Search the path (default sys.path) for a module. Return the
1265 corresponding filedescr struct, and (via return arguments) the
1266 pathname and an open file. Return NULL if the module is not found. */
1267
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001268#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001269extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001270 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001271#endif
1272
Martin v. Löwis18e16552006-02-15 17:27:45 +00001273static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001274static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001275static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001276
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001277static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001278find_module(char *fullname, char *subname, PyObject *path, char *buf,
1279 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001280{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001281 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001282 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001283 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001284 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001285 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001286 PyObject *path_hooks, *path_importer_cache;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001287 struct stat statbuf;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001288 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1289 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1290 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001291 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001292#if defined(PYOS_OS2)
1293 size_t saved_len;
1294 size_t saved_namelen;
1295 char *saved_buf = NULL;
1296#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001297 if (p_loader != NULL)
1298 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001299
Just van Rossum52e14d62002-12-30 22:08:05 +00001300 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001301 PyErr_SetString(PyExc_OverflowError,
1302 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001303 return NULL;
1304 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001305 strcpy(name, subname);
1306
1307 /* sys.meta_path import hook */
1308 if (p_loader != NULL) {
1309 PyObject *meta_path;
1310
1311 meta_path = PySys_GetObject("meta_path");
1312 if (meta_path == NULL || !PyList_Check(meta_path)) {
1313 PyErr_SetString(PyExc_ImportError,
1314 "sys.meta_path must be a list of "
1315 "import hooks");
1316 return NULL;
1317 }
1318 Py_INCREF(meta_path); /* zap guard */
1319 npath = PyList_Size(meta_path);
1320 for (i = 0; i < npath; i++) {
1321 PyObject *loader;
1322 PyObject *hook = PyList_GetItem(meta_path, i);
1323 loader = PyObject_CallMethod(hook, "find_module",
1324 "sO", fullname,
1325 path != NULL ?
1326 path : Py_None);
1327 if (loader == NULL) {
1328 Py_DECREF(meta_path);
1329 return NULL; /* true error */
1330 }
1331 if (loader != Py_None) {
1332 /* a loader was found */
1333 *p_loader = loader;
1334 Py_DECREF(meta_path);
1335 return &importhookdescr;
1336 }
1337 Py_DECREF(loader);
1338 }
1339 Py_DECREF(meta_path);
1340 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001341
Benjamin Petersond968e272008-11-05 22:48:33 +00001342 if (find_frozen(fullname) != NULL) {
1343 strcpy(buf, fullname);
1344 return &fd_frozen;
Guido van Rossum0506a431998-08-11 15:07:39 +00001345 }
Benjamin Petersond968e272008-11-05 22:48:33 +00001346
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001347 if (path == NULL) {
1348 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001349 strcpy(buf, name);
1350 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001351 }
Guido van Rossumac279101996-08-22 23:10:58 +00001352#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001353 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1354 if (fp != NULL) {
1355 *p_fp = fp;
1356 return fdp;
1357 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001358#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001359 path = PySys_GetObject("path");
1360 }
Benjamin Petersond968e272008-11-05 22:48:33 +00001361
Guido van Rossum79f25d91997-04-29 20:08:16 +00001362 if (path == NULL || !PyList_Check(path)) {
1363 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001364 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001365 return NULL;
1366 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001367
1368 path_hooks = PySys_GetObject("path_hooks");
1369 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1370 PyErr_SetString(PyExc_ImportError,
1371 "sys.path_hooks must be a list of "
1372 "import hooks");
1373 return NULL;
1374 }
1375 path_importer_cache = PySys_GetObject("path_importer_cache");
1376 if (path_importer_cache == NULL ||
1377 !PyDict_Check(path_importer_cache)) {
1378 PyErr_SetString(PyExc_ImportError,
1379 "sys.path_importer_cache must be a dict");
1380 return NULL;
1381 }
1382
Guido van Rossum79f25d91997-04-29 20:08:16 +00001383 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001384 namelen = strlen(name);
1385 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001386 PyObject *v = PyList_GetItem(path, i);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001387 PyObject *origv = v;
Guido van Rossum6262cc72007-05-09 23:29:27 +00001388 const char *base;
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001389 Py_ssize_t size;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001390 if (!v)
1391 return NULL;
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001392 if (PyUnicode_Check(v)) {
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001393 v = PyUnicode_AsEncodedString(v,
1394 Py_FileSystemDefaultEncoding, NULL);
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001395 if (v == NULL)
1396 return NULL;
1397 }
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001398 else if (!PyBytes_Check(v))
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001399 continue;
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001400 else
1401 Py_INCREF(v);
1402
Christian Heimes72b710a2008-05-26 13:28:38 +00001403 base = PyBytes_AS_STRING(v);
1404 size = PyBytes_GET_SIZE(v);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001405 len = size;
Walter Dörwald3430d702002-06-17 10:43:59 +00001406 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001407 Py_DECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001408 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001409 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001410 strcpy(buf, base);
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001411 Py_DECREF(v);
1412
Walter Dörwald3430d702002-06-17 10:43:59 +00001413 if (strlen(buf) != len) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001414 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001415 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001416
1417 /* sys.path_hooks import hook */
1418 if (p_loader != NULL) {
1419 PyObject *importer;
1420
1421 importer = get_path_importer(path_importer_cache,
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001422 path_hooks, origv);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001423 if (importer == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001424 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001425 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001426 /* Note: importer is a borrowed reference */
1427 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001428 PyObject *loader;
1429 loader = PyObject_CallMethod(importer,
1430 "find_module",
1431 "s", fullname);
1432 if (loader == NULL)
1433 return NULL; /* error */
1434 if (loader != Py_None) {
1435 /* a loader was found */
1436 *p_loader = loader;
1437 return &importhookdescr;
1438 }
1439 Py_DECREF(loader);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001440 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001441 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001442 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001443 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001444
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001445 if (len > 0 && buf[len-1] != SEP
1446#ifdef ALTSEP
1447 && buf[len-1] != ALTSEP
1448#endif
1449 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001450 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001451 strcpy(buf+len, name);
1452 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001453
1454 /* Check for package import (buf holds a directory name,
1455 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001456#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001457 if (stat(buf, &statbuf) == 0 && /* it exists */
1458 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001459 case_ok(buf, len, namelen, name)) { /* case matches */
1460 if (find_init_module(buf)) { /* and has __init__.py */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001461 return &fd_package;
1462 }
1463 else {
1464 char warnstr[MAXPATHLEN+80];
1465 sprintf(warnstr, "Not importing directory "
1466 "'%.*s': missing __init__.py",
1467 MAXPATHLEN, buf);
Skip Montanaro46fc3372007-08-12 11:44:53 +00001468 if (PyErr_WarnEx(PyExc_ImportWarning,
1469 warnstr, 1)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001470 return NULL;
1471 }
1472 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001473 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001474#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001475#if defined(PYOS_OS2)
1476 /* take a snapshot of the module spec for restoration
1477 * after the 8 character DLL hackery
1478 */
1479 saved_buf = strdup(buf);
1480 saved_len = len;
1481 saved_namelen = namelen;
1482#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001483 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum04110fb2007-08-24 16:32:05 +00001484#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001485 /* OS/2 limits DLLs to 8 character names (w/o
1486 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001487 * so if the name is longer than that and its a
1488 * dynamically loaded module we're going to try,
1489 * truncate the name before trying
1490 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001491 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001492 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001493 const struct filedescr *scan;
1494 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001495 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001496 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001497 break;
1498 else
1499 scan++;
1500 }
1501 if (scan->suffix != NULL) {
1502 /* yes, so truncate the name */
1503 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001504 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001505 buf[len] = '\0';
1506 }
1507 }
1508#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001509 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001510 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001511 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001512 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001513 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001514 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001515 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001516 if (fp != NULL) {
1517 if (case_ok(buf, len, namelen, name))
1518 break;
1519 else { /* continue search */
1520 fclose(fp);
1521 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001522 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001523 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001524#if defined(PYOS_OS2)
1525 /* restore the saved snapshot */
1526 strcpy(buf, saved_buf);
1527 len = saved_len;
1528 namelen = saved_namelen;
1529#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001530 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001531#if defined(PYOS_OS2)
1532 /* don't need/want the module name snapshot anymore */
1533 if (saved_buf)
1534 {
1535 free(saved_buf);
1536 saved_buf = NULL;
1537 }
1538#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001539 if (fp != NULL)
1540 break;
1541 }
1542 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001543 PyErr_Format(PyExc_ImportError,
1544 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001545 return NULL;
1546 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001547 *p_fp = fp;
1548 return fdp;
1549}
1550
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001551/* Helpers for main.c
1552 * Find the source file corresponding to a named module
1553 */
1554struct filedescr *
1555_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1556 size_t buflen, FILE **p_fp, PyObject **p_loader)
1557{
1558 return find_module((char *) name, (char *) name, path,
1559 buf, buflen, p_fp, p_loader);
1560}
1561
1562PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1563{
1564 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1565}
1566
Martin v. Löwis18e16552006-02-15 17:27:45 +00001567/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001568 * The arguments here are tricky, best shown by example:
1569 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1570 * ^ ^ ^ ^
1571 * |--------------------- buf ---------------------|
1572 * |------------------- len ------------------|
1573 * |------ name -------|
1574 * |----- namelen -----|
1575 * buf is the full path, but len only counts up to (& exclusive of) the
1576 * extension. name is the module name, also exclusive of extension.
1577 *
1578 * We've already done a successful stat() or fopen() on buf, so know that
1579 * there's some match, possibly case-insensitive.
1580 *
Tim Peters50d8d372001-02-28 05:34:27 +00001581 * case_ok() is to return 1 if there's a case-sensitive match for
1582 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1583 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001584 *
Tim Peters50d8d372001-02-28 05:34:27 +00001585 * case_ok() is used to implement case-sensitive import semantics even
1586 * on platforms with case-insensitive filesystems. It's trivial to implement
1587 * for case-sensitive filesystems. It's pretty much a cross-platform
1588 * nightmare for systems with case-insensitive filesystems.
1589 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001590
Tim Peters50d8d372001-02-28 05:34:27 +00001591/* First we may need a pile of platform-specific header files; the sequence
1592 * of #if's here should match the sequence in the body of case_ok().
1593 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001594#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001595#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001596
Tim Peters50d8d372001-02-28 05:34:27 +00001597#elif defined(DJGPP)
1598#include <dir.h>
1599
Jason Tishler7961aa62005-05-20 00:56:54 +00001600#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001601#include <sys/types.h>
1602#include <dirent.h>
1603
Andrew MacIntyred9400542002-02-26 11:41:34 +00001604#elif defined(PYOS_OS2)
1605#define INCL_DOS
1606#define INCL_DOSERRORS
1607#define INCL_NOPMAPI
1608#include <os2.h>
Tim Peters50d8d372001-02-28 05:34:27 +00001609#endif
1610
Guido van Rossum0980bd91998-02-13 17:18:36 +00001611static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001612case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001613{
Tim Peters50d8d372001-02-28 05:34:27 +00001614/* Pick a platform-specific implementation; the sequence of #if's here should
1615 * match the sequence just above.
1616 */
1617
Jason Tishler7961aa62005-05-20 00:56:54 +00001618/* MS_WINDOWS */
1619#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001620 WIN32_FIND_DATA data;
1621 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001622
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001623 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001624 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001625
Guido van Rossum0980bd91998-02-13 17:18:36 +00001626 h = FindFirstFile(buf, &data);
1627 if (h == INVALID_HANDLE_VALUE) {
1628 PyErr_Format(PyExc_NameError,
1629 "Can't find file for module %.100s\n(filename %.300s)",
1630 name, buf);
1631 return 0;
1632 }
1633 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001634 return strncmp(data.cFileName, name, namelen) == 0;
1635
1636/* DJGPP */
1637#elif defined(DJGPP)
1638 struct ffblk ffblk;
1639 int done;
1640
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001641 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001642 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001643
1644 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1645 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001646 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001647 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001648 name, buf);
1649 return 0;
1650 }
Tim Peters50d8d372001-02-28 05:34:27 +00001651 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001652
Jason Tishler7961aa62005-05-20 00:56:54 +00001653/* new-fangled macintosh (macosx) or Cygwin */
1654#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001655 DIR *dirp;
1656 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001657 char dirname[MAXPATHLEN + 1];
1658 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001659
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001660 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001661 return 1;
1662
Tim Petersd1e87a82001-03-01 18:12:00 +00001663 /* Copy the dir component into dirname; substitute "." if empty */
1664 if (dirlen <= 0) {
1665 dirname[0] = '.';
1666 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001667 }
1668 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001669 assert(dirlen <= MAXPATHLEN);
1670 memcpy(dirname, buf, dirlen);
1671 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001672 }
1673 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001674 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001675 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001676 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001677 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001678 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001679#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001680 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001681#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001682 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001683#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001684 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001685 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001686 (void)closedir(dirp);
1687 return 1; /* Found */
1688 }
1689 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001690 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001691 }
Tim Peters430f5d42001-03-01 01:30:56 +00001692 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001693
Andrew MacIntyred9400542002-02-26 11:41:34 +00001694/* OS/2 */
1695#elif defined(PYOS_OS2)
1696 HDIR hdir = 1;
1697 ULONG srchcnt = 1;
1698 FILEFINDBUF3 ffbuf;
1699 APIRET rc;
1700
Christian Heimes790c8232008-01-07 21:14:23 +00001701 if (Py_GETENV("PYTHONCASEOK") != NULL)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001702 return 1;
1703
1704 rc = DosFindFirst(buf,
1705 &hdir,
1706 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1707 &ffbuf, sizeof(ffbuf),
1708 &srchcnt,
1709 FIL_STANDARD);
1710 if (rc != NO_ERROR)
1711 return 0;
1712 return strncmp(ffbuf.achName, name, namelen) == 0;
1713
Tim Peters50d8d372001-02-28 05:34:27 +00001714/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1715#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001716 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001717
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001718#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001719}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001720
Guido van Rossum0980bd91998-02-13 17:18:36 +00001721
Guido van Rossum197346f1997-10-31 18:38:52 +00001722#ifdef HAVE_STAT
1723/* Helper to look for __init__.py or __init__.py[co] in potential package */
1724static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001725find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001726{
Tim Peters0f9431f2001-07-05 03:47:53 +00001727 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001728 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001729 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001730 struct stat statbuf;
1731
Tim Peters0f9431f2001-07-05 03:47:53 +00001732/* For calling case_ok(buf, len, namelen, name):
1733 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1734 * ^ ^ ^ ^
1735 * |--------------------- buf ---------------------|
1736 * |------------------- len ------------------|
1737 * |------ name -------|
1738 * |----- namelen -----|
1739 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001740 if (save_len + 13 >= MAXPATHLEN)
1741 return 0;
1742 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001743 pname = buf + i;
1744 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001745 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001746 if (case_ok(buf,
1747 save_len + 9, /* len("/__init__") */
1748 8, /* len("__init__") */
1749 pname)) {
1750 buf[save_len] = '\0';
1751 return 1;
1752 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001753 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001754 i += strlen(pname);
1755 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001756 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001757 if (case_ok(buf,
1758 save_len + 9, /* len("/__init__") */
1759 8, /* len("__init__") */
1760 pname)) {
1761 buf[save_len] = '\0';
1762 return 1;
1763 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001764 }
1765 buf[save_len] = '\0';
1766 return 0;
1767}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001768
Guido van Rossum197346f1997-10-31 18:38:52 +00001769#endif /* HAVE_STAT */
1770
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001771
Tim Petersdbd9ba62000-07-09 03:09:57 +00001772static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001773
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001774/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001775 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001776
Guido van Rossum79f25d91997-04-29 20:08:16 +00001777static PyObject *
Georg Brandl01a30522009-08-13 08:37:59 +00001778load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001779{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001780 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001781 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001782 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001783
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001784 /* First check that there's an open file (if we need one) */
1785 switch (type) {
1786 case PY_SOURCE:
1787 case PY_COMPILED:
1788 if (fp == NULL) {
1789 PyErr_Format(PyExc_ValueError,
1790 "file object required for import (type code %d)",
1791 type);
1792 return NULL;
1793 }
1794 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001795
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001796 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001797
1798 case PY_SOURCE:
Georg Brandl01a30522009-08-13 08:37:59 +00001799 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001800 break;
1801
1802 case PY_COMPILED:
Georg Brandl01a30522009-08-13 08:37:59 +00001803 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001804 break;
1805
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001806#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001807 case C_EXTENSION:
Georg Brandl01a30522009-08-13 08:37:59 +00001808 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001809 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001810#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001811
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001812 case PKG_DIRECTORY:
Georg Brandl01a30522009-08-13 08:37:59 +00001813 m = load_package(name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001814 break;
1815
1816 case C_BUILTIN:
1817 case PY_FROZEN:
Georg Brandl01a30522009-08-13 08:37:59 +00001818 if (pathname != NULL && pathname[0] != '\0')
1819 name = pathname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001820 if (type == C_BUILTIN)
1821 err = init_builtin(name);
1822 else
1823 err = PyImport_ImportFrozenModule(name);
1824 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001825 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001826 if (err == 0) {
1827 PyErr_Format(PyExc_ImportError,
1828 "Purported %s module %.200s not found",
1829 type == C_BUILTIN ?
1830 "builtin" : "frozen",
1831 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001832 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001833 }
1834 modules = PyImport_GetModuleDict();
1835 m = PyDict_GetItemString(modules, name);
1836 if (m == NULL) {
1837 PyErr_Format(
1838 PyExc_ImportError,
1839 "%s module %.200s not properly initialized",
1840 type == C_BUILTIN ?
1841 "builtin" : "frozen",
1842 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001843 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001844 }
1845 Py_INCREF(m);
1846 break;
1847
Just van Rossum52e14d62002-12-30 22:08:05 +00001848 case IMP_HOOK: {
1849 if (loader == NULL) {
1850 PyErr_SetString(PyExc_ImportError,
1851 "import hook without loader");
1852 return NULL;
1853 }
1854 m = PyObject_CallMethod(loader, "load_module", "s", name);
1855 break;
1856 }
1857
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001858 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001859 PyErr_Format(PyExc_ImportError,
1860 "Don't know how to import %.200s (type code %d)",
1861 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001862 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001863
1864 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001865
1866 return m;
1867}
1868
1869
1870/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001871 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001872 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001873
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001874static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001875init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001876{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001877 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001878
Greg Ward201baee2001-10-04 14:52:06 +00001879 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001880 return 1;
1881
Guido van Rossum771c6c81997-10-31 18:37:24 +00001882 for (p = PyImport_Inittab; p->name != NULL; p++) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00001883 PyObject *mod;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001884 if (strcmp(name, p->name) == 0) {
1885 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001886 PyErr_Format(PyExc_ImportError,
1887 "Cannot re-init internal module %.200s",
1888 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001889 return -1;
1890 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001891 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001892 PySys_WriteStderr("import %s # builtin\n", name);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001893 mod = (*p->initfunc)();
1894 if (mod == 0)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001895 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001896 if (_PyImport_FixupExtension(mod, name, name) < 0)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001897 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001898 /* FixupExtension has put the module into sys.modules,
1899 so we can release our own reference. */
1900 Py_DECREF(mod);
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001901 return 1;
1902 }
1903 }
1904 return 0;
1905}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001906
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001907
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001908/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001909
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001910static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001911find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001912{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001913 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001914
Benjamin Petersond968e272008-11-05 22:48:33 +00001915 if (!name)
1916 return NULL;
1917
Guido van Rossum79f25d91997-04-29 20:08:16 +00001918 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001919 if (p->name == NULL)
1920 return NULL;
1921 if (strcmp(p->name, name) == 0)
1922 break;
1923 }
1924 return p;
1925}
1926
Guido van Rossum79f25d91997-04-29 20:08:16 +00001927static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001928get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001929{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001930 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001931 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001932
1933 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001934 PyErr_Format(PyExc_ImportError,
1935 "No such frozen object named %.200s",
1936 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001937 return NULL;
1938 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001939 if (p->code == NULL) {
1940 PyErr_Format(PyExc_ImportError,
1941 "Excluded frozen object named %.200s",
1942 name);
1943 return NULL;
1944 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001945 size = p->size;
1946 if (size < 0)
1947 size = -size;
1948 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001949}
1950
Brett Cannon8d110132009-03-15 02:20:16 +00001951static PyObject *
1952is_frozen_package(char *name)
1953{
1954 struct _frozen *p = find_frozen(name);
1955 int size;
1956
1957 if (p == NULL) {
1958 PyErr_Format(PyExc_ImportError,
1959 "No such frozen object named %.200s",
1960 name);
1961 return NULL;
1962 }
1963
1964 size = p->size;
1965
1966 if (size < 0)
1967 Py_RETURN_TRUE;
1968 else
1969 Py_RETURN_FALSE;
1970}
1971
1972
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001973/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001974 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001975 an exception set if the initialization failed.
1976 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001977
1978int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001979PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001980{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001981 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001982 PyObject *co;
1983 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001984 int ispackage;
1985 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001986
1987 if (p == NULL)
1988 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001989 if (p->code == NULL) {
1990 PyErr_Format(PyExc_ImportError,
1991 "Excluded frozen object named %.200s",
1992 name);
1993 return -1;
1994 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001995 size = p->size;
1996 ispackage = (size < 0);
1997 if (ispackage)
1998 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001999 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00002000 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00002001 name, ispackage ? " package" : "");
2002 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002003 if (co == NULL)
2004 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002005 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002006 PyErr_Format(PyExc_TypeError,
2007 "frozen object %.200s is not a code object",
2008 name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002009 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002010 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00002011 if (ispackage) {
2012 /* Set __path__ to the package name */
Benjamin Petersond968e272008-11-05 22:48:33 +00002013 PyObject *d, *s, *l;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002014 int err;
2015 m = PyImport_AddModule(name);
2016 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002017 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002018 d = PyModule_GetDict(m);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002019 s = PyUnicode_InternFromString(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00002020 if (s == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002021 goto err_return;
Benjamin Petersond968e272008-11-05 22:48:33 +00002022 l = PyList_New(1);
2023 if (l == NULL) {
2024 Py_DECREF(s);
2025 goto err_return;
2026 }
2027 PyList_SET_ITEM(l, 0, s);
2028 err = PyDict_SetItemString(d, "__path__", l);
2029 Py_DECREF(l);
Guido van Rossuma5568d31998-03-05 03:45:08 +00002030 if (err != 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002031 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002032 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00002033 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002034 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002035 goto err_return;
2036 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002037 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002038 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002039err_return:
2040 Py_DECREF(co);
2041 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002042}
Guido van Rossum74e6a111994-08-29 12:54:38 +00002043
2044
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002045/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002046 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00002047
Guido van Rossum79f25d91997-04-29 20:08:16 +00002048PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002049PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00002050{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002051 PyObject *pname;
2052 PyObject *result;
2053
Martin v. Löwis5b222132007-06-10 09:51:05 +00002054 pname = PyUnicode_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00002055 if (pname == NULL)
2056 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002057 result = PyImport_Import(pname);
2058 Py_DECREF(pname);
2059 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002060}
2061
Christian Heimes072c0f12008-01-03 23:01:04 +00002062/* Import a module without blocking
2063 *
2064 * At first it tries to fetch the module from sys.modules. If the module was
2065 * never loaded before it loads it with PyImport_ImportModule() unless another
2066 * thread holds the import lock. In the latter case the function raises an
2067 * ImportError instead of blocking.
2068 *
2069 * Returns the module object with incremented ref count.
2070 */
2071PyObject *
2072PyImport_ImportModuleNoBlock(const char *name)
2073{
2074 PyObject *result;
2075 PyObject *modules;
2076 long me;
2077
2078 /* Try to get the module from sys.modules[name] */
2079 modules = PyImport_GetModuleDict();
2080 if (modules == NULL)
2081 return NULL;
2082
2083 result = PyDict_GetItemString(modules, name);
2084 if (result != NULL) {
2085 Py_INCREF(result);
2086 return result;
2087 }
2088 else {
2089 PyErr_Clear();
2090 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002091#ifdef WITH_THREAD
Christian Heimes072c0f12008-01-03 23:01:04 +00002092 /* check the import lock
2093 * me might be -1 but I ignore the error here, the lock function
2094 * takes care of the problem */
2095 me = PyThread_get_thread_ident();
2096 if (import_lock_thread == -1 || import_lock_thread == me) {
2097 /* no thread or me is holding the lock */
2098 return PyImport_ImportModule(name);
2099 }
2100 else {
2101 PyErr_Format(PyExc_ImportError,
2102 "Failed to import %.200s because the import lock"
2103 "is held by another thread.",
2104 name);
2105 return NULL;
2106 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002107#else
2108 return PyImport_ImportModule(name);
2109#endif
Christian Heimes072c0f12008-01-03 23:01:04 +00002110}
2111
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002112/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002113static PyObject *get_parent(PyObject *globals, char *buf,
2114 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002115static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002116 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002117static int mark_miss(char *name);
2118static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002119 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002120static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002121
2122/* The Magnum Opus of dotted-name import :-) */
2123
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002124static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002125import_module_level(char *name, PyObject *globals, PyObject *locals,
2126 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002127{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002128 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00002129 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002130 PyObject *parent, *head, *next, *tail;
2131
Christian Heimes454f37b2008-01-10 00:10:02 +00002132 if (strchr(name, '/') != NULL
2133#ifdef MS_WINDOWS
2134 || strchr(name, '\\') != NULL
2135#endif
2136 ) {
2137 PyErr_SetString(PyExc_ImportError,
2138 "Import by filename is not supported.");
2139 return NULL;
2140 }
2141
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002142 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002143 if (parent == NULL)
2144 return NULL;
2145
2146 head = load_next(parent, Py_None, &name, buf, &buflen);
2147 if (head == NULL)
2148 return NULL;
2149
2150 tail = head;
2151 Py_INCREF(tail);
2152 while (name) {
2153 next = load_next(tail, tail, &name, buf, &buflen);
2154 Py_DECREF(tail);
2155 if (next == NULL) {
2156 Py_DECREF(head);
2157 return NULL;
2158 }
2159 tail = next;
2160 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002161 if (tail == Py_None) {
2162 /* If tail is Py_None, both get_parent and load_next found
2163 an empty module name: someone called __import__("") or
2164 doctored faulty bytecode */
2165 Py_DECREF(tail);
2166 Py_DECREF(head);
2167 PyErr_SetString(PyExc_ValueError,
2168 "Empty module name");
2169 return NULL;
2170 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002171
2172 if (fromlist != NULL) {
2173 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2174 fromlist = NULL;
2175 }
2176
2177 if (fromlist == NULL) {
2178 Py_DECREF(tail);
2179 return head;
2180 }
2181
2182 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002183 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002184 Py_DECREF(tail);
2185 return NULL;
2186 }
2187
2188 return tail;
2189}
2190
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002191PyObject *
2192PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2193 PyObject *fromlist, int level)
2194{
2195 PyObject *result;
Benjamin Peterson51838562009-10-04 20:35:30 +00002196 _PyImport_AcquireLock();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002197 result = import_module_level(name, globals, locals, fromlist, level);
Benjamin Peterson51838562009-10-04 20:35:30 +00002198 if (_PyImport_ReleaseLock() < 0) {
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002199 Py_XDECREF(result);
2200 PyErr_SetString(PyExc_RuntimeError,
2201 "not holding the import lock");
2202 return NULL;
2203 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002204 return result;
2205}
2206
Fred Drake87590902004-05-28 20:21:36 +00002207/* Return the package that an import is being performed in. If globals comes
2208 from the module foo.bar.bat (not itself a package), this returns the
2209 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002210 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002211
2212 The *name* of the returned package is returned in buf, with the length of
2213 the name in *p_buflen.
2214
2215 If globals doesn't come from a package or a module in a package, or a
2216 corresponding entry is not found in sys.modules, Py_None is returned.
2217*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002218static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002219get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002220{
2221 static PyObject *namestr = NULL;
2222 static PyObject *pathstr = NULL;
Nick Coghlande10c852007-12-04 12:22:52 +00002223 static PyObject *pkgstr = NULL;
2224 PyObject *pkgname, *modname, *modpath, *modules, *parent;
Georg Brandl2ee470f2008-07-16 12:55:28 +00002225 int orig_level = level;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002226
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002227 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002228 return Py_None;
2229
2230 if (namestr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002231 namestr = PyUnicode_InternFromString("__name__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002232 if (namestr == NULL)
2233 return NULL;
2234 }
2235 if (pathstr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002236 pathstr = PyUnicode_InternFromString("__path__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002237 if (pathstr == NULL)
2238 return NULL;
2239 }
Nick Coghlande10c852007-12-04 12:22:52 +00002240 if (pkgstr == NULL) {
2241 pkgstr = PyUnicode_InternFromString("__package__");
2242 if (pkgstr == NULL)
2243 return NULL;
2244 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002245
2246 *buf = '\0';
2247 *p_buflen = 0;
Nick Coghlande10c852007-12-04 12:22:52 +00002248 pkgname = PyDict_GetItem(globals, pkgstr);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002249
Nick Coghlande10c852007-12-04 12:22:52 +00002250 if ((pkgname != NULL) && (pkgname != Py_None)) {
2251 /* __package__ is set, so use it */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002252 char *pkgname_str;
Nick Coghlande10c852007-12-04 12:22:52 +00002253 Py_ssize_t len;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002254
Nick Coghlande10c852007-12-04 12:22:52 +00002255 if (!PyUnicode_Check(pkgname)) {
2256 PyErr_SetString(PyExc_ValueError,
2257 "__package__ set to non-string");
2258 return NULL;
2259 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002260 pkgname_str = _PyUnicode_AsStringAndSize(pkgname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002261 if (len == 0) {
2262 if (level > 0) {
2263 PyErr_SetString(PyExc_ValueError,
2264 "Attempted relative import in non-package");
2265 return NULL;
2266 }
2267 return Py_None;
2268 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002269 if (len > MAXPATHLEN) {
2270 PyErr_SetString(PyExc_ValueError,
Nick Coghlande10c852007-12-04 12:22:52 +00002271 "Package name too long");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002272 return NULL;
2273 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002274 strcpy(buf, pkgname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002275 } else {
2276 /* __package__ not set, so figure it out and set it */
2277 modname = PyDict_GetItem(globals, namestr);
2278 if (modname == NULL || !PyUnicode_Check(modname))
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002279 return Py_None;
Nick Coghlande10c852007-12-04 12:22:52 +00002280
2281 modpath = PyDict_GetItem(globals, pathstr);
2282 if (modpath != NULL) {
2283 /* __path__ is set, so modname is already the package name */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002284 char *modname_str;
2285 Py_ssize_t len;
Nick Coghlande10c852007-12-04 12:22:52 +00002286 int error;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002287
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002288 modname_str = _PyUnicode_AsStringAndSize(modname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002289 if (len > MAXPATHLEN) {
2290 PyErr_SetString(PyExc_ValueError,
2291 "Module name too long");
2292 return NULL;
2293 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002294 strcpy(buf, modname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002295 error = PyDict_SetItem(globals, pkgstr, modname);
2296 if (error) {
2297 PyErr_SetString(PyExc_ValueError,
2298 "Could not set __package__");
2299 return NULL;
2300 }
2301 } else {
2302 /* Normal module, so work out the package name if any */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002303 char *start = _PyUnicode_AsString(modname);
Nick Coghlande10c852007-12-04 12:22:52 +00002304 char *lastdot = strrchr(start, '.');
2305 size_t len;
2306 int error;
2307 if (lastdot == NULL && level > 0) {
2308 PyErr_SetString(PyExc_ValueError,
2309 "Attempted relative import in non-package");
2310 return NULL;
2311 }
2312 if (lastdot == NULL) {
2313 error = PyDict_SetItem(globals, pkgstr, Py_None);
2314 if (error) {
2315 PyErr_SetString(PyExc_ValueError,
2316 "Could not set __package__");
2317 return NULL;
2318 }
2319 return Py_None;
2320 }
2321 len = lastdot - start;
2322 if (len >= MAXPATHLEN) {
2323 PyErr_SetString(PyExc_ValueError,
2324 "Module name too long");
2325 return NULL;
2326 }
2327 strncpy(buf, start, len);
2328 buf[len] = '\0';
2329 pkgname = PyUnicode_FromString(buf);
2330 if (pkgname == NULL) {
2331 return NULL;
2332 }
2333 error = PyDict_SetItem(globals, pkgstr, pkgname);
2334 Py_DECREF(pkgname);
2335 if (error) {
2336 PyErr_SetString(PyExc_ValueError,
2337 "Could not set __package__");
2338 return NULL;
2339 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002340 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002341 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002342 while (--level > 0) {
2343 char *dot = strrchr(buf, '.');
2344 if (dot == NULL) {
2345 PyErr_SetString(PyExc_ValueError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002346 "Attempted relative import beyond "
2347 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002348 return NULL;
2349 }
2350 *dot = '\0';
2351 }
2352 *p_buflen = strlen(buf);
2353
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002354 modules = PyImport_GetModuleDict();
2355 parent = PyDict_GetItemString(modules, buf);
Georg Brandl2ee470f2008-07-16 12:55:28 +00002356 if (parent == NULL) {
2357 if (orig_level < 1) {
2358 PyObject *err_msg = PyBytes_FromFormat(
2359 "Parent module '%.200s' not found "
2360 "while handling absolute import", buf);
2361 if (err_msg == NULL) {
2362 return NULL;
2363 }
2364 if (!PyErr_WarnEx(PyExc_RuntimeWarning,
2365 PyBytes_AsString(err_msg), 1)) {
2366 *buf = '\0';
2367 *p_buflen = 0;
2368 parent = Py_None;
2369 }
2370 Py_DECREF(err_msg);
2371 } else {
2372 PyErr_Format(PyExc_SystemError,
2373 "Parent module '%.200s' not loaded, "
2374 "cannot perform relative import", buf);
2375 }
2376 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002377 return parent;
2378 /* We expect, but can't guarantee, if parent != None, that:
2379 - parent.__name__ == buf
2380 - parent.__dict__ is globals
2381 If this is violated... Who cares? */
2382}
2383
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002384/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002385static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002386load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002387 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002388{
2389 char *name = *p_name;
2390 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002391 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002392 char *p;
2393 PyObject *result;
2394
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002395 if (strlen(name) == 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002396 /* completely empty module name should only happen in
2397 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002398 Py_INCREF(mod);
2399 *p_name = NULL;
2400 return mod;
2401 }
2402
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002403 if (dot == NULL) {
2404 *p_name = NULL;
2405 len = strlen(name);
2406 }
2407 else {
2408 *p_name = dot+1;
2409 len = dot-name;
2410 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002411 if (len == 0) {
2412 PyErr_SetString(PyExc_ValueError,
2413 "Empty module name");
2414 return NULL;
2415 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002416
2417 p = buf + *p_buflen;
2418 if (p != buf)
2419 *p++ = '.';
2420 if (p+len-buf >= MAXPATHLEN) {
2421 PyErr_SetString(PyExc_ValueError,
2422 "Module name too long");
2423 return NULL;
2424 }
2425 strncpy(p, name, len);
2426 p[len] = '\0';
2427 *p_buflen = p+len-buf;
2428
2429 result = import_submodule(mod, p, buf);
2430 if (result == Py_None && altmod != mod) {
2431 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002432 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002433 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002434 if (result != NULL && result != Py_None) {
2435 if (mark_miss(buf) != 0) {
2436 Py_DECREF(result);
2437 return NULL;
2438 }
2439 strncpy(buf, name, len);
2440 buf[len] = '\0';
2441 *p_buflen = len;
2442 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002443 }
2444 if (result == NULL)
2445 return NULL;
2446
2447 if (result == Py_None) {
2448 Py_DECREF(result);
2449 PyErr_Format(PyExc_ImportError,
2450 "No module named %.200s", name);
2451 return NULL;
2452 }
2453
2454 return result;
2455}
2456
2457static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002458mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002459{
2460 PyObject *modules = PyImport_GetModuleDict();
2461 return PyDict_SetItemString(modules, name, Py_None);
2462}
2463
2464static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002465ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002466 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002467{
2468 int i;
2469
2470 if (!PyObject_HasAttrString(mod, "__path__"))
2471 return 1;
2472
2473 for (i = 0; ; i++) {
2474 PyObject *item = PySequence_GetItem(fromlist, i);
2475 int hasit;
2476 if (item == NULL) {
2477 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2478 PyErr_Clear();
2479 return 1;
2480 }
2481 return 0;
2482 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002483 if (!PyUnicode_Check(item)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002484 PyErr_SetString(PyExc_TypeError,
Neal Norwitz80e7f272007-08-26 06:45:23 +00002485 "Item in ``from list'' not a string");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002486 Py_DECREF(item);
2487 return 0;
2488 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002489 if (PyUnicode_AS_UNICODE(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002490 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002491 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002492 /* See if the package defines __all__ */
2493 if (recursive)
2494 continue; /* Avoid endless recursion */
2495 all = PyObject_GetAttrString(mod, "__all__");
2496 if (all == NULL)
2497 PyErr_Clear();
2498 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002499 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002500 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002501 if (!ret)
2502 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002503 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002504 continue;
2505 }
2506 hasit = PyObject_HasAttr(mod, item);
2507 if (!hasit) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002508 PyObject *item8;
2509 char *subname;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002510 PyObject *submod;
2511 char *p;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002512 if (!Py_FileSystemDefaultEncoding) {
2513 item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item),
2514 PyUnicode_GetSize(item),
Martin v. Löwis427dbff2007-06-12 05:53:00 +00002515 NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002516 } else {
Guido van Rossum98297ee2007-11-06 21:34:58 +00002517 item8 = PyUnicode_AsEncodedString(item,
2518 Py_FileSystemDefaultEncoding, NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002519 }
2520 if (!item8) {
2521 PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
2522 return 0;
2523 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002524 subname = PyBytes_AS_STRING(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002525 if (buflen + strlen(subname) >= MAXPATHLEN) {
2526 PyErr_SetString(PyExc_ValueError,
2527 "Module name too long");
2528 Py_DECREF(item);
2529 return 0;
2530 }
2531 p = buf + buflen;
2532 *p++ = '.';
2533 strcpy(p, subname);
2534 submod = import_submodule(mod, subname, buf);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002535 Py_DECREF(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002536 Py_XDECREF(submod);
2537 if (submod == NULL) {
2538 Py_DECREF(item);
2539 return 0;
2540 }
2541 }
2542 Py_DECREF(item);
2543 }
2544
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002545 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002546}
2547
Neil Schemenauer00b09662003-06-16 21:03:07 +00002548static int
2549add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2550 PyObject *modules)
2551{
2552 if (mod == Py_None)
2553 return 1;
2554 /* Irrespective of the success of this load, make a
2555 reference to it in the parent package module. A copy gets
2556 saved in the modules dictionary under the full name, so get a
2557 reference from there, if need be. (The exception is when the
2558 load failed with a SyntaxError -- then there's no trace in
2559 sys.modules. In that case, of course, do nothing extra.) */
2560 if (submod == NULL) {
2561 submod = PyDict_GetItemString(modules, fullname);
2562 if (submod == NULL)
2563 return 1;
2564 }
2565 if (PyModule_Check(mod)) {
2566 /* We can't use setattr here since it can give a
2567 * spurious warning if the submodule name shadows a
2568 * builtin name */
2569 PyObject *dict = PyModule_GetDict(mod);
2570 if (!dict)
2571 return 0;
2572 if (PyDict_SetItemString(dict, subname, submod) < 0)
2573 return 0;
2574 }
2575 else {
2576 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2577 return 0;
2578 }
2579 return 1;
2580}
2581
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002582static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002583import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002584{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002585 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002586 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002587
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002588 /* Require:
2589 if mod == None: subname == fullname
2590 else: mod.__name__ + "." + subname == fullname
2591 */
2592
Tim Peters50d8d372001-02-28 05:34:27 +00002593 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002594 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002595 }
2596 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002597 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002598 char buf[MAXPATHLEN+1];
2599 struct filedescr *fdp;
2600 FILE *fp = NULL;
2601
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002602 if (mod == Py_None)
2603 path = NULL;
2604 else {
2605 path = PyObject_GetAttrString(mod, "__path__");
2606 if (path == NULL) {
2607 PyErr_Clear();
2608 Py_INCREF(Py_None);
2609 return Py_None;
2610 }
2611 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002612
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002613 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002614 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2615 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002616 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002617 if (fdp == NULL) {
2618 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2619 return NULL;
2620 PyErr_Clear();
2621 Py_INCREF(Py_None);
2622 return Py_None;
2623 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002624 m = load_module(fullname, fp, buf, fdp->type, loader);
2625 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002626 if (fp)
2627 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002628 if (!add_submodule(mod, m, fullname, subname, modules)) {
2629 Py_XDECREF(m);
2630 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002631 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002632 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002633
2634 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002635}
2636
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002637
2638/* Re-import a module of any kind and return its module object, WITH
2639 INCREMENTED REFERENCE COUNT */
2640
Guido van Rossum79f25d91997-04-29 20:08:16 +00002641PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002642PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002643{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002644 PyInterpreterState *interp = PyThreadState_Get()->interp;
2645 PyObject *modules_reloading = interp->modules_reloading;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002646 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossumd8faa362007-04-27 19:54:29 +00002647 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002648 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002649 char buf[MAXPATHLEN+1];
2650 struct filedescr *fdp;
2651 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002652 PyObject *newm;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002653
2654 if (modules_reloading == NULL) {
2655 Py_FatalError("PyImport_ReloadModule: "
Guido van Rossume7ba4952007-06-06 23:52:48 +00002656 "no modules_reloading dictionary!");
Guido van Rossumd8faa362007-04-27 19:54:29 +00002657 return NULL;
2658 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002659
Guido van Rossum79f25d91997-04-29 20:08:16 +00002660 if (m == NULL || !PyModule_Check(m)) {
2661 PyErr_SetString(PyExc_TypeError,
2662 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002663 return NULL;
2664 }
Neal Norwitz5616c6d2007-08-26 05:32:41 +00002665 name = (char*)PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002666 if (name == NULL)
2667 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002668 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002669 PyErr_Format(PyExc_ImportError,
2670 "reload(): module %.200s not in sys.modules",
2671 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002672 return NULL;
2673 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002674 existing_m = PyDict_GetItemString(modules_reloading, name);
2675 if (existing_m != NULL) {
2676 /* Due to a recursive reload, this module is already
2677 being reloaded. */
2678 Py_INCREF(existing_m);
2679 return existing_m;
2680 }
2681 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
2682 return NULL;
2683
Guido van Rossum222ef561997-09-06 19:41:09 +00002684 subname = strrchr(name, '.');
2685 if (subname == NULL)
2686 subname = name;
2687 else {
2688 PyObject *parentname, *parent;
Christian Heimesc4cb3b82007-11-04 12:10:01 +00002689 parentname = PyUnicode_FromStringAndSize(name, (subname-name));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002690 if (parentname == NULL) {
2691 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002692 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002693 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002694 parent = PyDict_GetItem(modules, parentname);
2695 if (parent == NULL) {
2696 PyErr_Format(PyExc_ImportError,
2697 "reload(): parent %.200s not in sys.modules",
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002698 _PyUnicode_AsString(parentname));
Georg Brandl0c55f292005-09-14 06:56:20 +00002699 Py_DECREF(parentname);
Guido van Rossume7ba4952007-06-06 23:52:48 +00002700 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002701 return NULL;
2702 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002703 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002704 subname++;
2705 path = PyObject_GetAttrString(parent, "__path__");
2706 if (path == NULL)
2707 PyErr_Clear();
2708 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002709 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002710 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002711 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002712
2713 if (fdp == NULL) {
2714 Py_XDECREF(loader);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002715 imp_modules_reloading_clear();
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002716 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002717 }
2718
2719 newm = load_module(name, fp, buf, fdp->type, loader);
2720 Py_XDECREF(loader);
2721
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002722 if (fp)
2723 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002724 if (newm == NULL) {
2725 /* load_module probably removed name from modules because of
2726 * the error. Put back the original module object. We're
2727 * going to return NULL in this case regardless of whether
2728 * replacing name succeeds, so the return value is ignored.
2729 */
2730 PyDict_SetItemString(modules, name, m);
2731 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002732 imp_modules_reloading_clear();
Tim Peters1cd70172004-08-02 03:52:12 +00002733 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002734}
2735
2736
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002737/* Higher-level import emulator which emulates the "import" statement
2738 more accurately -- it invokes the __import__() function from the
2739 builtins of the current globals. This means that the import is
2740 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002741 environment, e.g. by "rexec".
2742 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00002743 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00002744 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002745
2746PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002747PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002748{
2749 static PyObject *silly_list = NULL;
2750 static PyObject *builtins_str = NULL;
2751 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002752 PyObject *globals = NULL;
2753 PyObject *import = NULL;
2754 PyObject *builtins = NULL;
2755 PyObject *r = NULL;
2756
2757 /* Initialize constant string objects */
2758 if (silly_list == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002759 import_str = PyUnicode_InternFromString("__import__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002760 if (import_str == NULL)
2761 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002762 builtins_str = PyUnicode_InternFromString("__builtins__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002763 if (builtins_str == NULL)
2764 return NULL;
2765 silly_list = Py_BuildValue("[s]", "__doc__");
2766 if (silly_list == NULL)
2767 return NULL;
2768 }
2769
2770 /* Get the builtins from current globals */
2771 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002772 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002773 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002774 builtins = PyObject_GetItem(globals, builtins_str);
2775 if (builtins == NULL)
2776 goto err;
2777 }
2778 else {
2779 /* No globals -- use standard builtins, and fake globals */
2780 PyErr_Clear();
2781
Georg Brandl1a3284e2007-12-02 09:40:06 +00002782 builtins = PyImport_ImportModuleLevel("builtins",
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002783 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002784 if (builtins == NULL)
2785 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002786 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2787 if (globals == NULL)
2788 goto err;
2789 }
2790
2791 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002792 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002793 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002794 if (import == NULL)
2795 PyErr_SetObject(PyExc_KeyError, import_str);
2796 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002797 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002798 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002799 if (import == NULL)
2800 goto err;
2801
Christian Heimes072c0f12008-01-03 23:01:04 +00002802 /* Call the __import__ function with the proper argument list
2803 * Always use absolute import here. */
2804 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
2805 globals, silly_list, 0, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002806
2807 err:
2808 Py_XDECREF(globals);
2809 Py_XDECREF(builtins);
2810 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002811
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002812 return r;
2813}
2814
2815
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002816/* Module 'imp' provides Python access to the primitives used for
2817 importing modules.
2818*/
2819
Guido van Rossum79f25d91997-04-29 20:08:16 +00002820static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002821imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002822{
2823 char buf[4];
2824
Guido van Rossum96774c12000-05-01 20:19:08 +00002825 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2826 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2827 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2828 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002829
Christian Heimes72b710a2008-05-26 13:28:38 +00002830 return PyBytes_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002831}
2832
Guido van Rossum79f25d91997-04-29 20:08:16 +00002833static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002834imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002835{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002836 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002837 struct filedescr *fdp;
2838
Guido van Rossum79f25d91997-04-29 20:08:16 +00002839 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002840 if (list == NULL)
2841 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002842 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2843 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002844 fdp->suffix, fdp->mode, fdp->type);
2845 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002846 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002847 return NULL;
2848 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002849 if (PyList_Append(list, item) < 0) {
2850 Py_DECREF(list);
2851 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002852 return NULL;
2853 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002854 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002855 }
2856 return list;
2857}
2858
Guido van Rossum79f25d91997-04-29 20:08:16 +00002859static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002860call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002861{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002862 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002863 PyObject *fob, *ret;
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002864 PyObject *pathobj;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002865 struct filedescr *fdp;
2866 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002867 FILE *fp = NULL;
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002868 int fd = -1;
Brett Cannon3bb42d92007-10-20 03:43:15 +00002869 char *found_encoding = NULL;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002870 char *encoding = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002871
2872 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002873 if (path == Py_None)
2874 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002875 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002876 if (fdp == NULL)
2877 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002878 if (fp != NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002879 fd = fileno(fp);
2880 if (fd != -1)
2881 fd = dup(fd);
2882 fclose(fp);
2883 fp = NULL;
2884 }
2885 if (fd != -1) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002886 if (strchr(fdp->mode, 'b') == NULL) {
Brett Cannon3bb42d92007-10-20 03:43:15 +00002887 /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
2888 memory. */
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002889 found_encoding = PyTokenizer_FindEncoding(fd);
2890 lseek(fd, 0, 0); /* Reset position */
Amaury Forgeot d'Arc1b933ed2008-09-04 22:34:09 +00002891 if (found_encoding == NULL && PyErr_Occurred())
2892 return NULL;
Brett Cannon3bb42d92007-10-20 03:43:15 +00002893 encoding = (found_encoding != NULL) ? found_encoding :
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002894 (char*)PyUnicode_GetDefaultEncoding();
2895 }
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002896 fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002897 (char*)encoding, NULL, NULL, 1);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002898 if (fob == NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002899 close(fd);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002900 PyMem_FREE(found_encoding);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002901 return NULL;
2902 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002903 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002904 else {
2905 fob = Py_None;
2906 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002907 }
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002908 pathobj = PyUnicode_DecodeFSDefault(pathname);
2909 ret = Py_BuildValue("NN(ssi)",
2910 fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002911 PyMem_FREE(found_encoding);
2912
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002913 return ret;
2914}
2915
Guido van Rossum79f25d91997-04-29 20:08:16 +00002916static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002917imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002918{
2919 char *name;
Hirokazu Yamamoto90eaaf62009-01-30 03:15:05 +00002920 PyObject *ret, *path = NULL;
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002921 if (!PyArg_ParseTuple(args, "es|O:find_module",
2922 Py_FileSystemDefaultEncoding, &name,
2923 &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002924 return NULL;
Hirokazu Yamamoto90eaaf62009-01-30 03:15:05 +00002925 ret = call_find_module(name, path);
2926 PyMem_Free(name);
2927 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002928}
2929
2930static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002931imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002932{
2933 char *name;
2934 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002935 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002936 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002937 return NULL;
2938 ret = init_builtin(name);
2939 if (ret < 0)
2940 return NULL;
2941 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002942 Py_INCREF(Py_None);
2943 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002944 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002945 m = PyImport_AddModule(name);
2946 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002947 return m;
2948}
2949
Guido van Rossum79f25d91997-04-29 20:08:16 +00002950static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002951imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002952{
2953 char *name;
2954 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002955 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002956 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002957 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002958 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002959 if (ret < 0)
2960 return NULL;
2961 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002962 Py_INCREF(Py_None);
2963 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002964 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002965 m = PyImport_AddModule(name);
2966 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002967 return m;
2968}
2969
Guido van Rossum79f25d91997-04-29 20:08:16 +00002970static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002971imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002972{
2973 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002974
Guido van Rossum43713e52000-02-29 13:59:29 +00002975 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002976 return NULL;
2977 return get_frozen_object(name);
2978}
2979
Guido van Rossum79f25d91997-04-29 20:08:16 +00002980static PyObject *
Brett Cannon8d110132009-03-15 02:20:16 +00002981imp_is_frozen_package(PyObject *self, PyObject *args)
2982{
2983 char *name;
2984
2985 if (!PyArg_ParseTuple(args, "s:is_frozen_package", &name))
2986 return NULL;
2987 return is_frozen_package(name);
2988}
2989
2990static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002991imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002992{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002993 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002994 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002995 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00002996 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002997}
2998
Guido van Rossum79f25d91997-04-29 20:08:16 +00002999static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003000imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003001{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003002 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00003003 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00003004 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003005 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00003006 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00003007 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003008}
3009
3010static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003011get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003012{
3013 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003014 if (mode[0] == 'U')
3015 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003016 if (fob == NULL) {
3017 fp = fopen(pathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003018 }
3019 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003020 int fd = PyObject_AsFileDescriptor(fob);
3021 if (fd == -1)
3022 return NULL;
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003023 if (!_PyVerify_fd(fd))
3024 goto error;
3025 /* the FILE struct gets a new fd, so that it can be closed
3026 * independently of the file descriptor given
3027 */
3028 fd = dup(fd);
3029 if (fd == -1)
3030 goto error;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003031 fp = fdopen(fd, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003032 }
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003033 if (fp)
3034 return fp;
3035error:
3036 PyErr_SetFromErrno(PyExc_IOError);
3037 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003038}
3039
Guido van Rossum79f25d91997-04-29 20:08:16 +00003040static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003041imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003042{
3043 char *name;
3044 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003045 PyObject *fob = NULL;
3046 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003047 FILE *fp;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003048 if (!PyArg_ParseTuple(args, "ses|O:load_compiled",
3049 &name,
3050 Py_FileSystemDefaultEncoding, &pathname,
3051 &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003052 return NULL;
3053 fp = get_file(pathname, fob, "rb");
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003054 if (fp == NULL) {
3055 PyMem_Free(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003056 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003057 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003058 m = load_compiled_module(name, pathname, fp);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003059 fclose(fp);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003060 PyMem_Free(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003061 return m;
3062}
3063
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003064#ifdef HAVE_DYNAMIC_LOADING
3065
Guido van Rossum79f25d91997-04-29 20:08:16 +00003066static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003067imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003068{
3069 char *name;
3070 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003071 PyObject *fob = NULL;
3072 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00003073 FILE *fp = NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003074 if (!PyArg_ParseTuple(args, "ses|O:load_dynamic",
3075 &name,
3076 Py_FileSystemDefaultEncoding, &pathname,
3077 &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003078 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003079 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00003080 fp = get_file(pathname, fob, "r");
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003081 if (fp == NULL) {
3082 PyMem_Free(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003083 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003084 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003085 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003086 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003087 PyMem_Free(pathname);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003088 if (fp)
3089 fclose(fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00003090 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003091}
3092
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003093#endif /* HAVE_DYNAMIC_LOADING */
3094
Guido van Rossum79f25d91997-04-29 20:08:16 +00003095static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003096imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003097{
3098 char *name;
3099 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003100 PyObject *fob = NULL;
3101 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003102 FILE *fp;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003103 if (!PyArg_ParseTuple(args, "ses|O:load_source",
3104 &name,
3105 Py_FileSystemDefaultEncoding, &pathname,
3106 &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003107 return NULL;
3108 fp = get_file(pathname, fob, "r");
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003109 if (fp == NULL) {
3110 PyMem_Free(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003111 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003112 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003113 m = load_source_module(name, pathname, fp);
Kristján Valur Jónsson92af5d92009-03-31 17:47:50 +00003114 PyMem_Free(pathname);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003115 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003116 return m;
3117}
3118
Guido van Rossum79f25d91997-04-29 20:08:16 +00003119static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003120imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003121{
3122 char *name;
3123 PyObject *fob;
3124 char *pathname;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003125 PyObject * ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003126 char *suffix; /* Unused */
3127 char *mode;
3128 int type;
3129 FILE *fp;
3130
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003131 if (!PyArg_ParseTuple(args, "sOes(ssi):load_module",
3132 &name, &fob,
3133 Py_FileSystemDefaultEncoding, &pathname,
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003134 &suffix, &mode, &type))
3135 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003136 if (*mode) {
3137 /* Mode must start with 'r' or 'U' and must not contain '+'.
3138 Implicit in this test is the assumption that the mode
3139 may contain other modifiers like 'b' or 't'. */
3140
3141 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00003142 PyErr_Format(PyExc_ValueError,
3143 "invalid file open mode %.200s", mode);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003144 PyMem_Free(pathname);
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00003145 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003146 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003147 }
3148 if (fob == Py_None)
3149 fp = NULL;
3150 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003151 fp = get_file(NULL, fob, mode);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003152 if (fp == NULL) {
3153 PyMem_Free(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003154 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003155 }
3156 }
3157 ret = load_module(name, fp, pathname, type, NULL);
3158 PyMem_Free(pathname);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003159 if (fp)
3160 fclose(fp);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003161 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003162}
3163
3164static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003165imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003166{
3167 char *name;
3168 char *pathname;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003169 PyObject * ret;
3170 if (!PyArg_ParseTuple(args, "ses:load_package",
3171 &name, Py_FileSystemDefaultEncoding, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003172 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003173 ret = load_package(name, pathname);
3174 PyMem_Free(pathname);
3175 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003176}
3177
3178static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003179imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003180{
3181 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00003182 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003183 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003184 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003185}
3186
Christian Heimes13a7a212008-01-07 17:13:09 +00003187static PyObject *
3188imp_reload(PyObject *self, PyObject *v)
3189{
3190 return PyImport_ReloadModule(v);
3191}
3192
3193PyDoc_STRVAR(doc_reload,
3194"reload(module) -> module\n\
3195\n\
3196Reload the module. The module must have been successfully imported before.");
3197
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003198/* Doc strings */
3199
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003200PyDoc_STRVAR(doc_imp,
3201"This module provides the components needed to build your own\n\
3202__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003204PyDoc_STRVAR(doc_find_module,
3205"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003206Search for a module. If path is omitted or None, search for a\n\
3207built-in, frozen or special module and continue search in sys.path.\n\
3208The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003209package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003210
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003211PyDoc_STRVAR(doc_load_module,
3212"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003213Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003214The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003215
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003216PyDoc_STRVAR(doc_get_magic,
3217"get_magic() -> string\n\
3218Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003219
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003220PyDoc_STRVAR(doc_get_suffixes,
3221"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003222Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003223that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003224
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003225PyDoc_STRVAR(doc_new_module,
3226"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003227Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003228The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003229
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003230PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00003231"lock_held() -> boolean\n\
3232Return True if the import lock is currently held, else False.\n\
3233On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00003234
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003235PyDoc_STRVAR(doc_acquire_lock,
3236"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00003237Acquires the interpreter's import lock for the current thread.\n\
3238This lock should be used by import hooks to ensure thread-safety\n\
3239when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003240On platforms without threads, this function does nothing.");
3241
3242PyDoc_STRVAR(doc_release_lock,
3243"release_lock() -> None\n\
3244Release the interpreter's import lock.\n\
3245On platforms without threads, this function does nothing.");
3246
Guido van Rossum79f25d91997-04-29 20:08:16 +00003247static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00003248 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3249 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3250 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3251 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3252 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3253 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3254 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3255 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Christian Heimes13a7a212008-01-07 17:13:09 +00003256 {"reload", imp_reload, METH_O, doc_reload},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003257 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00003258 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
Brett Cannon8d110132009-03-15 02:20:16 +00003259 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00003260 {"init_builtin", imp_init_builtin, METH_VARARGS},
3261 {"init_frozen", imp_init_frozen, METH_VARARGS},
3262 {"is_builtin", imp_is_builtin, METH_VARARGS},
3263 {"is_frozen", imp_is_frozen, METH_VARARGS},
3264 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003265#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00003266 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003267#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00003268 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00003269 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003270 {NULL, NULL} /* sentinel */
3271};
3272
Guido van Rossum1a8791e1998-08-04 22:46:29 +00003273static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003274setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003275{
3276 PyObject *v;
3277 int err;
3278
Christian Heimes217cfd12007-12-02 14:31:20 +00003279 v = PyLong_FromLong((long)value);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003280 err = PyDict_SetItemString(d, name, v);
3281 Py_XDECREF(v);
3282 return err;
3283}
3284
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003285typedef struct {
3286 PyObject_HEAD
3287} NullImporter;
3288
3289static int
3290NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3291{
3292 char *path;
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003293 Py_ssize_t pathlen;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003294
3295 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3296 return -1;
3297
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00003298 if (!PyArg_ParseTuple(args, "es:NullImporter",
3299 Py_FileSystemDefaultEncoding, &path))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003300 return -1;
3301
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003302 pathlen = strlen(path);
3303 if (pathlen == 0) {
Georg Brandl8494d572008-07-19 10:13:15 +00003304 PyMem_Free(path);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003305 PyErr_SetString(PyExc_ImportError, "empty pathname");
3306 return -1;
3307 } else {
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003308#ifndef MS_WINDOWS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003309 struct stat statbuf;
3310 int rv;
3311
3312 rv = stat(path, &statbuf);
Georg Brandl8494d572008-07-19 10:13:15 +00003313 PyMem_Free(path);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003314 if (rv == 0) {
3315 /* it exists */
3316 if (S_ISDIR(statbuf.st_mode)) {
3317 /* it's a directory */
3318 PyErr_SetString(PyExc_ImportError,
3319 "existing directory");
3320 return -1;
3321 }
3322 }
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003323#else /* MS_WINDOWS */
3324 DWORD rv;
3325 /* see issue1293 and issue3677:
3326 * stat() on Windows doesn't recognise paths like
3327 * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
3328 */
3329 rv = GetFileAttributesA(path);
Kristján Valur Jónsson92cb4382009-01-24 10:33:25 +00003330 PyMem_Free(path);
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003331 if (rv != INVALID_FILE_ATTRIBUTES) {
3332 /* it exists */
3333 if (rv & FILE_ATTRIBUTE_DIRECTORY) {
3334 /* it's a directory */
3335 PyErr_SetString(PyExc_ImportError,
3336 "existing directory");
3337 return -1;
3338 }
3339 }
3340#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003341 }
3342 return 0;
3343}
3344
3345static PyObject *
3346NullImporter_find_module(NullImporter *self, PyObject *args)
3347{
3348 Py_RETURN_NONE;
3349}
3350
3351static PyMethodDef NullImporter_methods[] = {
3352 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3353 "Always return None"
3354 },
3355 {NULL} /* Sentinel */
3356};
3357
3358
Christian Heimes9cd17752007-11-18 19:35:23 +00003359PyTypeObject PyNullImporter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003360 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003361 "imp.NullImporter", /*tp_name*/
3362 sizeof(NullImporter), /*tp_basicsize*/
3363 0, /*tp_itemsize*/
3364 0, /*tp_dealloc*/
3365 0, /*tp_print*/
3366 0, /*tp_getattr*/
3367 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00003368 0, /*tp_reserved*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003369 0, /*tp_repr*/
3370 0, /*tp_as_number*/
3371 0, /*tp_as_sequence*/
3372 0, /*tp_as_mapping*/
3373 0, /*tp_hash */
3374 0, /*tp_call*/
3375 0, /*tp_str*/
3376 0, /*tp_getattro*/
3377 0, /*tp_setattro*/
3378 0, /*tp_as_buffer*/
3379 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3380 "Null importer object", /* tp_doc */
3381 0, /* tp_traverse */
3382 0, /* tp_clear */
3383 0, /* tp_richcompare */
3384 0, /* tp_weaklistoffset */
3385 0, /* tp_iter */
3386 0, /* tp_iternext */
3387 NullImporter_methods, /* tp_methods */
3388 0, /* tp_members */
3389 0, /* tp_getset */
3390 0, /* tp_base */
3391 0, /* tp_dict */
3392 0, /* tp_descr_get */
3393 0, /* tp_descr_set */
3394 0, /* tp_dictoffset */
3395 (initproc)NullImporter_init, /* tp_init */
3396 0, /* tp_alloc */
3397 PyType_GenericNew /* tp_new */
3398};
3399
Martin v. Löwis1a214512008-06-11 05:26:20 +00003400static struct PyModuleDef impmodule = {
3401 PyModuleDef_HEAD_INIT,
3402 "imp",
3403 doc_imp,
3404 0,
3405 imp_methods,
3406 NULL,
3407 NULL,
3408 NULL,
3409 NULL
3410};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003411
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003412PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003413PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003414{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003415 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003416
Christian Heimes9cd17752007-11-18 19:35:23 +00003417 if (PyType_Ready(&PyNullImporter_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003418 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003419
Martin v. Löwis1a214512008-06-11 05:26:20 +00003420 m = PyModule_Create(&impmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003421 if (m == NULL)
3422 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003423 d = PyModule_GetDict(m);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003424 if (d == NULL)
3425 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003426
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003427 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3428 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3429 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3430 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3431 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3432 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3433 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3434 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003435 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003436 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003437
Christian Heimes9cd17752007-11-18 19:35:23 +00003438 Py_INCREF(&PyNullImporter_Type);
3439 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
Martin v. Löwis1a214512008-06-11 05:26:20 +00003440 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003441 failure:
Martin v. Löwis1a214512008-06-11 05:26:20 +00003442 Py_XDECREF(m);
3443 return NULL;
3444
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003445}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003446
3447
Guido van Rossumb18618d2000-05-03 23:44:39 +00003448/* API for embedding applications that want to add their own entries
3449 to the table of built-in modules. This should normally be called
3450 *before* Py_Initialize(). When the table resize fails, -1 is
3451 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003452
3453 After a similar function by Just van Rossum. */
3454
3455int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003456PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003457{
3458 static struct _inittab *our_copy = NULL;
3459 struct _inittab *p;
3460 int i, n;
3461
3462 /* Count the number of entries in both tables */
3463 for (n = 0; newtab[n].name != NULL; n++)
3464 ;
3465 if (n == 0)
3466 return 0; /* Nothing to do */
3467 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3468 ;
3469
3470 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003471 p = our_copy;
3472 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003473 if (p == NULL)
3474 return -1;
3475
3476 /* Copy the tables into the new memory */
3477 if (our_copy != PyImport_Inittab)
3478 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3479 PyImport_Inittab = our_copy = p;
3480 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3481
3482 return 0;
3483}
3484
3485/* Shorthand to add a single entry given a name and a function */
3486
3487int
Brett Cannona826f322009-04-02 03:41:46 +00003488PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003489{
3490 struct _inittab newtab[2];
3491
3492 memset(newtab, '\0', sizeof newtab);
3493
Brett Cannona826f322009-04-02 03:41:46 +00003494 newtab[0].name = (char *)name;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003495 newtab[0].initfunc = initfunc;
3496
3497 return PyImport_ExtendInittab(newtab);
3498}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003499
3500#ifdef __cplusplus
3501}
3502#endif