blob: b046362818839510a8b3e1256af34b936c8deef0 [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
Brett Cannon9a5b25a2010-03-01 02:09:17 +000022extern "C" {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000023#endif
Guido van Rossum55a83382000-09-20 20:31:38 +000024
Christian Heimesd3eb5a152008-02-24 00:38:49 +000025#ifdef MS_WINDOWS
26/* for stat.st_mode */
27typedef unsigned short mode_t;
28#endif
29
Guido van Rossum21d335e1993-10-15 13:01:11 +000030
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000031/* Magic word to reject .pyc files generated by other Python versions.
32 It should change for each incompatible change to the bytecode.
33
34 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000035 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000036 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000037
Guido van Rossum45aecf42006-03-15 04:58:47 +000038 The magic numbers must be spaced apart at least 2 values, as the
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000039 -U interpeter flag will cause MAGIC+1 being used. They have been
40 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000041
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000042 There were a variety of old schemes for setting the magic number.
43 The current working scheme is to increment the previous value by
44 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000045
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000046 Known values:
47 Python 1.5: 20121
48 Python 1.5.1: 20121
49 Python 1.5.2: 20121
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000050 Python 1.6: 50428
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000051 Python 2.0: 50823
52 Python 2.0.1: 50823
53 Python 2.1: 60202
54 Python 2.1.1: 60202
55 Python 2.1.2: 60202
56 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000057 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000058 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000059 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000060 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000061 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000062 Python 2.4b1: 62061
Michael W. Hudsondf888462005-06-03 14:41:55 +000063 Python 2.5a0: 62071
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000064 Python 2.5a0: 62081 (ast-branch)
Guido van Rossumc2e20742006-02-27 22:32:47 +000065 Python 2.5a0: 62091 (with)
Guido van Rossumf6694362006-03-10 02:28:35 +000066 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
68 Python 2.5b3: 62111 (fix wrong code: x += yield)
69 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
70 storing constants that should have been removed)
Thomas Wouters89f507f2006-12-13 04:49:30 +000071 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
Christian Heimes99170a52007-12-19 02:07:34 +000072 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000073 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
Guido van Rossum45aecf42006-03-15 04:58:47 +000074 Python 3000: 3000
Brett Cannone2e23ef2006-08-25 05:05:30 +000075 3010 (removed UNARY_CONVERT)
Guido van Rossum86e58e22006-08-28 15:27:34 +000076 3020 (added BUILD_SET)
Guido van Rossum4f72a782006-10-27 23:31:49 +000077 3030 (added keyword-only parameters)
Guido van Rossum3203bdc2006-12-28 21:03:31 +000078 3040 (added signature annotations)
Guido van Rossum452bf512007-02-09 05:32:43 +000079 3050 (print becomes a function)
Guido van Rossum52cc1d82007-03-18 15:41:51 +000080 3060 (PEP 3115 metaclass syntax)
Benjamin Peterson0f6cae02009-12-10 02:09:08 +000081 3061 (string literals become unicode)
82 3071 (PEP 3109 raise changes)
83 3081 (PEP 3137 make __file__ and __name__ unicode)
84 3091 (kill str8 interning)
85 3101 (merge from 2.6a0, see 62151)
86 3103 (__file__ points to source file)
87 Python 3.0a4: 3111 (WITH_CLEANUP optimization).
88 Python 3.0a5: 3131 (lexical exception stacking, including POP_EXCEPT)
89 Python 3.1a0: 3141 (optimize list, set and dict comprehensions:
Antoine Pitrouf289ae62008-12-18 11:06:25 +000090 change LIST_APPEND and SET_ADD, add MAP_ADD)
Benjamin Peterson0f6cae02009-12-10 02:09:08 +000091 Python 3.1a0: 3151 (optimize conditional branches:
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +000092 introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
Benjamin Peterson876b2f22009-06-28 03:18:59 +000093 Python 3.2a0: 3160 (add SETUP_WITH)
Tim Peters36515e22001-11-18 04:06:29 +000094*/
Guido van Rossum3ddee711991-12-16 13:06:34 +000095
Benjamin Peterson876b2f22009-06-28 03:18:59 +000096#define MAGIC (3160 | ((long)'\r'<<16) | ((long)'\n'<<24))
Benjamin Peterson0f6cae02009-12-10 02:09:08 +000097/* Magic word as global */
Guido van Rossum96774c12000-05-01 20:19:08 +000098static long pyc_magic = MAGIC;
99
Guido van Rossum25ce5661997-08-02 03:10:38 +0000100/* See _PyImport_FixupExtension() below */
101static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102
Guido van Rossum771c6c81997-10-31 18:37:24 +0000103/* This table is defined in config.c: */
104extern struct _inittab _PyImport_Inittab[];
105
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000106/* Method from Parser/tokenizer.c */
Guido van Rossum40d20bc2007-10-22 00:09:51 +0000107extern char * PyTokenizer_FindEncoding(int);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000108
Guido van Rossum771c6c81997-10-31 18:37:24 +0000109struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +0000110
Guido van Rossumed1170e1999-12-20 21:23:41 +0000111/* these tables define the module suffixes that Python recognizes */
112struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000113
Guido van Rossumed1170e1999-12-20 21:23:41 +0000114static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +0000115 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000116#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +0000117 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000118#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000119 {".pyc", "rb", PY_COMPILED},
120 {0, 0}
121};
122
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000123
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000124/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125
126void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000127_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000129 const struct filedescr *scan;
130 struct filedescr *filetab;
131 int countD = 0;
132 int countS = 0;
133
134 /* prepare _PyImport_Filetab: copy entries from
135 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
136 */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000137#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000138 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
139 ++countD;
Guido van Rossum04110fb2007-08-24 16:32:05 +0000140#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000141 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
142 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000143 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000144 if (filetab == NULL)
145 Py_FatalError("Can't initialize import file table.");
Guido van Rossum04110fb2007-08-24 16:32:05 +0000146#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000147 memcpy(filetab, _PyImport_DynLoadFiletab,
148 countD * sizeof(struct filedescr));
Guido van Rossum04110fb2007-08-24 16:32:05 +0000149#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000150 memcpy(filetab + countD, _PyImport_StandardFiletab,
151 countS * sizeof(struct filedescr));
152 filetab[countD + countS].suffix = NULL;
153
154 _PyImport_Filetab = filetab;
155
Guido van Rossum0824f631997-03-11 18:37:35 +0000156 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000157 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
158 for (; filetab->suffix != NULL; filetab++) {
159 if (strcmp(filetab->suffix, ".pyc") == 0)
160 filetab->suffix = ".pyo";
Guido van Rossum0824f631997-03-11 18:37:35 +0000161 }
162 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163}
164
Guido van Rossum25ce5661997-08-02 03:10:38 +0000165void
Just van Rossum52e14d62002-12-30 22:08:05 +0000166_PyImportHooks_Init(void)
167{
168 PyObject *v, *path_hooks = NULL, *zimpimport;
169 int err = 0;
170
171 /* adding sys.path_hooks and sys.path_importer_cache, setting up
172 zipimport */
Christian Heimes9cd17752007-11-18 19:35:23 +0000173 if (PyType_Ready(&PyNullImporter_Type) < 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000174 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000175
176 if (Py_VerboseFlag)
177 PySys_WriteStderr("# installing zipimport hook\n");
178
179 v = PyList_New(0);
180 if (v == NULL)
181 goto error;
182 err = PySys_SetObject("meta_path", v);
183 Py_DECREF(v);
184 if (err)
185 goto error;
186 v = PyDict_New();
187 if (v == NULL)
188 goto error;
189 err = PySys_SetObject("path_importer_cache", v);
190 Py_DECREF(v);
191 if (err)
192 goto error;
193 path_hooks = PyList_New(0);
194 if (path_hooks == NULL)
195 goto error;
196 err = PySys_SetObject("path_hooks", path_hooks);
197 if (err) {
198 error:
199 PyErr_Print();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000200 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
201 "path_importer_cache, or NullImporter failed"
202 );
Just van Rossum52e14d62002-12-30 22:08:05 +0000203 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000204
Just van Rossum52e14d62002-12-30 22:08:05 +0000205 zimpimport = PyImport_ImportModule("zipimport");
206 if (zimpimport == NULL) {
207 PyErr_Clear(); /* No zip import module -- okay */
208 if (Py_VerboseFlag)
209 PySys_WriteStderr("# can't import zipimport\n");
210 }
211 else {
212 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
213 "zipimporter");
214 Py_DECREF(zimpimport);
215 if (zipimporter == NULL) {
216 PyErr_Clear(); /* No zipimporter object -- okay */
217 if (Py_VerboseFlag)
218 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000219 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000220 }
221 else {
222 /* sys.path_hooks.append(zipimporter) */
223 err = PyList_Append(path_hooks, zipimporter);
224 Py_DECREF(zipimporter);
225 if (err)
226 goto error;
227 if (Py_VerboseFlag)
228 PySys_WriteStderr(
229 "# installed zipimport hook\n");
230 }
231 }
232 Py_DECREF(path_hooks);
233}
234
235void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000236_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000237{
238 Py_XDECREF(extensions);
239 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000240 PyMem_DEL(_PyImport_Filetab);
241 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242}
243
244
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000245/* Locking primitives to prevent parallel imports of the same module
246 in different threads to return with a partially loaded module.
247 These calls are serialized by the global interpreter lock. */
248
249#ifdef WITH_THREAD
250
Guido van Rossum49b56061998-10-01 20:42:43 +0000251#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000252
Guido van Rossum65d5b571998-12-21 19:32:43 +0000253static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000254static long import_lock_thread = -1;
255static int import_lock_level = 0;
256
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000257void
258_PyImport_AcquireLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000259{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000260 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000261 if (me == -1)
262 return; /* Too bad */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000263 if (import_lock == NULL) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000264 import_lock = PyThread_allocate_lock();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000265 if (import_lock == NULL)
266 return; /* Nothing much we can do. */
267 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000268 if (import_lock_thread == me) {
269 import_lock_level++;
270 return;
271 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000272 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
273 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000274 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000275 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000276 PyEval_RestoreThread(tstate);
277 }
278 import_lock_thread = me;
279 import_lock_level = 1;
280}
281
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000282int
283_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000284{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000285 long me = PyThread_get_thread_ident();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000286 if (me == -1 || import_lock == NULL)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000287 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000288 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000289 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000290 import_lock_level--;
291 if (import_lock_level == 0) {
292 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000293 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000294 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000295 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000296}
297
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000298/* This function is called from PyOS_AfterFork to ensure that newly
299 created child processes do not share locks with the parent.
300 We now acquire the import lock around fork() calls but on some platforms
301 (Solaris 9 and earlier? see isue7242) that still left us with problems. */
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000302
303void
304_PyImport_ReInitLock(void)
305{
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000306 if (import_lock != NULL)
307 import_lock = PyThread_allocate_lock();
308 import_lock_thread = -1;
309 import_lock_level = 0;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000310}
311
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000312#endif
313
Tim Peters69232342001-08-30 05:16:13 +0000314static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000315imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000316{
Tim Peters69232342001-08-30 05:16:13 +0000317#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000318 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000319#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000320 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000321#endif
322}
323
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000324static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000325imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000326{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000327#ifdef WITH_THREAD
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000328 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000329#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000330 Py_INCREF(Py_None);
331 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000332}
333
334static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000335imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000336{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000337#ifdef WITH_THREAD
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000338 if (_PyImport_ReleaseLock() < 0) {
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000339 PyErr_SetString(PyExc_RuntimeError,
340 "not holding the import lock");
341 return NULL;
342 }
343#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000344 Py_INCREF(Py_None);
345 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000346}
347
Guido van Rossumd8faa362007-04-27 19:54:29 +0000348static void
349imp_modules_reloading_clear(void)
350{
351 PyInterpreterState *interp = PyThreadState_Get()->interp;
352 if (interp->modules_reloading != NULL)
353 PyDict_Clear(interp->modules_reloading);
354}
355
Guido van Rossum25ce5661997-08-02 03:10:38 +0000356/* Helper for sys */
357
358PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000359PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000360{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000361 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000362 if (interp->modules == NULL)
363 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
364 return interp->modules;
365}
366
Guido van Rossum3f5da241990-12-20 15:06:42 +0000367
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000368/* List of names to clear in sys */
369static char* sys_deletes[] = {
Collin Winter670e6922007-03-21 02:57:17 +0000370 "path", "argv", "ps1", "ps2",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000371 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000372 "path_hooks", "path_importer_cache", "meta_path",
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000373 /* misc stuff */
374 "flags", "float_info",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000375 NULL
376};
377
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000378static char* sys_files[] = {
379 "stdin", "__stdin__",
380 "stdout", "__stdout__",
381 "stderr", "__stderr__",
382 NULL
383};
384
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000385
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000386/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000387
Guido van Rossum3f5da241990-12-20 15:06:42 +0000388void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000389PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000390{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000391 Py_ssize_t pos, ndone;
Guido van Rossum758eec01998-01-19 21:58:26 +0000392 char *name;
393 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000394 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000395 PyObject *modules = interp->modules;
396
397 if (modules == NULL)
398 return; /* Already done */
399
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000400 /* Delete some special variables first. These are common
401 places where user values hide and people complain when their
402 destructors fail. Since the modules containing them are
403 deleted *last* of all, they would come too late in the normal
404 destruction order. Sigh. */
405
Georg Brandl1a3284e2007-12-02 09:40:06 +0000406 value = PyDict_GetItemString(modules, "builtins");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000407 if (value != NULL && PyModule_Check(value)) {
408 dict = PyModule_GetDict(value);
409 if (Py_VerboseFlag)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000410 PySys_WriteStderr("# clear builtins._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000411 PyDict_SetItemString(dict, "_", Py_None);
412 }
413 value = PyDict_GetItemString(modules, "sys");
414 if (value != NULL && PyModule_Check(value)) {
415 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000416 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000417 dict = PyModule_GetDict(value);
418 for (p = sys_deletes; *p != NULL; p++) {
419 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000420 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000421 PyDict_SetItemString(dict, *p, Py_None);
422 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000423 for (p = sys_files; *p != NULL; p+=2) {
424 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000425 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000426 v = PyDict_GetItemString(dict, *(p+1));
427 if (v == NULL)
428 v = Py_None;
429 PyDict_SetItemString(dict, *p, v);
430 }
431 }
432
433 /* First, delete __main__ */
434 value = PyDict_GetItemString(modules, "__main__");
435 if (value != NULL && PyModule_Check(value)) {
436 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000437 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000438 _PyModule_Clear(value);
439 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000440 }
441
Georg Brandl1a3284e2007-12-02 09:40:06 +0000442 /* The special treatment of "builtins" here is because even
Guido van Rossum758eec01998-01-19 21:58:26 +0000443 when it's not referenced as a module, its dictionary is
444 referenced by almost every module's __builtins__. Since
445 deleting a module clears its dictionary (even if there are
Georg Brandl1a3284e2007-12-02 09:40:06 +0000446 references left to it), we need to delete the "builtins"
Guido van Rossum758eec01998-01-19 21:58:26 +0000447 module last. Likewise, we don't delete sys until the very
448 end because it is implicitly referenced (e.g. by print).
449
450 Also note that we 'delete' modules by replacing their entry
451 in the modules dict with None, rather than really deleting
452 them; this avoids a rehash of the modules dictionary and
453 also marks them as "non existent" so they won't be
454 re-imported. */
455
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000456 /* Next, repeatedly delete modules with a reference count of
Georg Brandl1a3284e2007-12-02 09:40:06 +0000457 one (skipping builtins and sys) and delete them */
Guido van Rossum758eec01998-01-19 21:58:26 +0000458 do {
459 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000460 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000461 while (PyDict_Next(modules, &pos, &key, &value)) {
462 if (value->ob_refcnt != 1)
463 continue;
Neal Norwitz80e7f272007-08-26 06:45:23 +0000464 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000465 name = _PyUnicode_AsString(key);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000466 if (strcmp(name, "builtins") == 0)
Guido van Rossum758eec01998-01-19 21:58:26 +0000467 continue;
468 if (strcmp(name, "sys") == 0)
469 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000470 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000471 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000472 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000473 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000474 PyDict_SetItem(modules, key, Py_None);
475 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000476 }
477 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000478 } while (ndone > 0);
479
Georg Brandl1a3284e2007-12-02 09:40:06 +0000480 /* Next, delete all modules (still skipping builtins and sys) */
Guido van Rossum758eec01998-01-19 21:58:26 +0000481 pos = 0;
482 while (PyDict_Next(modules, &pos, &key, &value)) {
Neal Norwitz80e7f272007-08-26 06:45:23 +0000483 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000484 name = _PyUnicode_AsString(key);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000485 if (strcmp(name, "builtins") == 0)
Guido van Rossum758eec01998-01-19 21:58:26 +0000486 continue;
487 if (strcmp(name, "sys") == 0)
488 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000489 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000490 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000491 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000492 PyDict_SetItem(modules, key, Py_None);
493 }
494 }
495
Georg Brandl1a3284e2007-12-02 09:40:06 +0000496 /* Next, delete sys and builtins (in that order) */
Guido van Rossum758eec01998-01-19 21:58:26 +0000497 value = PyDict_GetItemString(modules, "sys");
498 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000499 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000500 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000501 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000502 PyDict_SetItemString(modules, "sys", Py_None);
503 }
Georg Brandl1a3284e2007-12-02 09:40:06 +0000504 value = PyDict_GetItemString(modules, "builtins");
Guido van Rossum758eec01998-01-19 21:58:26 +0000505 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000506 if (Py_VerboseFlag)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000507 PySys_WriteStderr("# cleanup builtins\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000508 _PyModule_Clear(value);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000509 PyDict_SetItemString(modules, "builtins", Py_None);
Guido van Rossum758eec01998-01-19 21:58:26 +0000510 }
511
512 /* Finally, clear and delete the modules directory */
513 PyDict_Clear(modules);
514 interp->modules = NULL;
515 Py_DECREF(modules);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000516 Py_CLEAR(interp->modules_reloading);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000517}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000518
519
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000520/* Helper for pythonrun.c -- return magic number */
521
522long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000523PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000524{
Guido van Rossum96774c12000-05-01 20:19:08 +0000525 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000526}
527
528
Guido van Rossum25ce5661997-08-02 03:10:38 +0000529/* Magic for extension modules (built-in as well as dynamically
530 loaded). To prevent initializing an extension module more than
531 once, we keep a static dictionary 'extensions' keyed by module name
532 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000533 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000534 dictionary is stored by calling _PyImport_FixupExtension()
535 immediately after the module initialization function succeeds. A
536 copy can be retrieved from there by calling
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000537 _PyImport_FindExtension().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000538
Martin v. Löwis1a214512008-06-11 05:26:20 +0000539 Modules which do support multiple multiple initialization set
540 their m_size field to a non-negative number (indicating the size
541 of the module-specific state). They are still recorded in the
542 extensions dictionary, to avoid loading shared libraries twice.
543*/
544
545int
546_PyImport_FixupExtension(PyObject *mod, char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000547{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000548 PyObject *modules, *dict;
549 struct PyModuleDef *def;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000550 if (extensions == NULL) {
551 extensions = PyDict_New();
552 if (extensions == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000553 return -1;
554 }
555 if (mod == NULL || !PyModule_Check(mod)) {
556 PyErr_BadInternalCall();
557 return -1;
558 }
559 def = PyModule_GetDef(mod);
560 if (!def) {
561 PyErr_BadInternalCall();
562 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000563 }
564 modules = PyImport_GetModuleDict();
Martin v. Löwis1a214512008-06-11 05:26:20 +0000565 if (PyDict_SetItemString(modules, name, mod) < 0)
566 return -1;
567 if (_PyState_AddModule(mod, def) < 0) {
568 PyDict_DelItemString(modules, name);
569 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000570 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000571 if (def->m_size == -1) {
572 if (def->m_base.m_copy) {
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000573 /* Somebody already imported the module,
Martin v. Löwis1a214512008-06-11 05:26:20 +0000574 likely under a different name.
575 XXX this should really not happen. */
576 Py_DECREF(def->m_base.m_copy);
577 def->m_base.m_copy = NULL;
578 }
579 dict = PyModule_GetDict(mod);
580 if (dict == NULL)
581 return -1;
582 def->m_base.m_copy = PyDict_Copy(dict);
583 if (def->m_base.m_copy == NULL)
584 return -1;
585 }
586 PyDict_SetItemString(extensions, filename, (PyObject*)def);
587 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000588}
589
590PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000591_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000592{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000593 PyObject *mod, *mdict;
594 PyModuleDef* def;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000595 if (extensions == NULL)
596 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000597 def = (PyModuleDef*)PyDict_GetItemString(extensions, filename);
598 if (def == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000599 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000600 if (def->m_size == -1) {
601 /* Module does not support repeated initialization */
602 if (def->m_base.m_copy == NULL)
603 return NULL;
604 mod = PyImport_AddModule(name);
605 if (mod == NULL)
606 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000607 mdict = PyModule_GetDict(mod);
608 if (mdict == NULL)
609 return NULL;
610 if (PyDict_Update(mdict, def->m_base.m_copy))
611 return NULL;
612 }
613 else {
614 if (def->m_base.m_init == NULL)
615 return NULL;
616 mod = def->m_base.m_init();
617 if (mod == NULL)
618 return NULL;
619 PyDict_SetItemString(PyImport_GetModuleDict(), name, mod);
Benjamin Petersonad956532008-09-04 02:28:15 +0000620 Py_DECREF(mod);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000621 }
622 if (_PyState_AddModule(mod, def) < 0) {
623 PyDict_DelItemString(PyImport_GetModuleDict(), name);
624 Py_DECREF(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000625 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000626 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000627 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000628 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Martin v. Löwis1a214512008-06-11 05:26:20 +0000629 name, filename);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000630 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000631
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000632}
633
634
635/* Get the module object corresponding to a module name.
636 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000637 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000638 Because the former action is most common, THIS DOES NOT RETURN A
639 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000640
Guido van Rossum79f25d91997-04-29 20:08:16 +0000641PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000642PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000643{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000644 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000645 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000646
Guido van Rossum25ce5661997-08-02 03:10:38 +0000647 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000648 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000649 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000650 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651 if (m == NULL)
652 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000653 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655 return NULL;
656 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000658
659 return m;
660}
661
Tim Peters1cd70172004-08-02 03:52:12 +0000662/* Remove name from sys.modules, if it's there. */
663static void
Benjamin Petersonfa0aeba2010-03-25 23:30:20 +0000664remove_module(const char *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000665{
666 PyObject *modules = PyImport_GetModuleDict();
667 if (PyDict_GetItemString(modules, name) == NULL)
668 return;
669 if (PyDict_DelItemString(modules, name) < 0)
670 Py_FatalError("import: deleting existing key in"
671 "sys.modules failed");
672}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000673
Christian Heimes3b06e532008-01-07 20:12:44 +0000674static PyObject * get_sourcefile(const char *file);
675
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000676/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000677 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
678 * removed from sys.modules, to avoid leaving damaged module objects
679 * in sys.modules. The caller may wish to restore the original
680 * module object (if any) in this case; PyImport_ReloadModule is an
681 * example.
682 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000683PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000684PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000685{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000686 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
687}
688
689PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000690PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000691{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000692 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000693 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000694
Guido van Rossum79f25d91997-04-29 20:08:16 +0000695 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000696 if (m == NULL)
697 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000698 /* If the module is being reloaded, we get the old module back
699 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000700 d = PyModule_GetDict(m);
701 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
702 if (PyDict_SetItemString(d, "__builtins__",
703 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000704 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000705 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000706 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000707 v = NULL;
708 if (pathname != NULL) {
Christian Heimes3b06e532008-01-07 20:12:44 +0000709 v = get_sourcefile(pathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000710 if (v == NULL)
711 PyErr_Clear();
712 }
713 if (v == NULL) {
714 v = ((PyCodeObject *)co)->co_filename;
715 Py_INCREF(v);
716 }
717 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000718 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000719 Py_DECREF(v);
720
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000721 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000722 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000723 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000724 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000725
Guido van Rossum25ce5661997-08-02 03:10:38 +0000726 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000727 PyErr_Format(PyExc_ImportError,
728 "Loaded module %.200s not found in sys.modules",
729 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000730 return NULL;
731 }
732
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000734
735 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000736
737 error:
Benjamin Petersonfa0aeba2010-03-25 23:30:20 +0000738 remove_module(name);
Tim Peters1cd70172004-08-02 03:52:12 +0000739 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000740}
741
742
743/* Given a pathname for a Python source file, fill a buffer with the
744 pathname for the corresponding compiled file. Return the pathname
745 for the compiled file, or NULL if there's no space in the buffer.
746 Doesn't set an exception. */
747
748static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000749make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000750{
Tim Petersc1731372001-08-04 08:12:36 +0000751 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000752 if (len+2 > buflen)
753 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000754
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000755#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000756 /* Treat .pyw as if it were .py. The case of ".pyw" must match
757 that used in _PyImport_StandardFiletab. */
758 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
759 --len; /* pretend 'w' isn't there */
760#endif
761 memcpy(buf, pathname, len);
762 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
763 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000764
765 return buf;
766}
767
768
769/* Given a pathname for a Python source file, its time of last
770 modification, and a pathname for a compiled file, check whether the
771 compiled file represents the same version of the source. If so,
772 return a FILE pointer for the compiled file, positioned just after
773 the header; if not, return NULL.
774 Doesn't set an exception. */
775
776static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000777check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000778{
779 FILE *fp;
780 long magic;
781 long pyc_mtime;
782
783 fp = fopen(cpathname, "rb");
784 if (fp == NULL)
785 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000786 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000787 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000788 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000789 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000790 fclose(fp);
791 return NULL;
792 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000793 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000794 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000795 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000796 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000797 fclose(fp);
798 return NULL;
799 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000800 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000801 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000802 return fp;
803}
804
805
806/* Read a code object from a file and check it for validity */
807
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000809read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000810{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000811 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000812
Tim Petersd9b9ac82001-01-28 00:27:39 +0000813 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000814 if (co == NULL)
815 return NULL;
816 if (!PyCode_Check(co)) {
817 PyErr_Format(PyExc_ImportError,
818 "Non-code object in %.200s", cpathname);
819 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000820 return NULL;
821 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000822 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000823}
824
825
826/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000827 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000828
Guido van Rossum79f25d91997-04-29 20:08:16 +0000829static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000830load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000831{
832 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000833 PyCodeObject *co;
834 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000835
Guido van Rossum79f25d91997-04-29 20:08:16 +0000836 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000837 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000838 PyErr_Format(PyExc_ImportError,
839 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000840 return NULL;
841 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000843 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000844 if (co == NULL)
845 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000847 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000848 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000849 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000850 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000851
852 return m;
853}
854
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000855/* Parse a source file and return the corresponding code object */
856
Guido van Rossum79f25d91997-04-29 20:08:16 +0000857static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000859{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 PyCodeObject *co = NULL;
861 mod_ty mod;
Christian Heimes4d6ec852008-03-26 22:34:47 +0000862 PyCompilerFlags flags;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000863 PyArena *arena = PyArena_New();
864 if (arena == NULL)
865 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000866
Christian Heimesb1b3efc2008-03-26 23:24:27 +0000867 flags.cf_flags = 0;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000868 mod = PyParser_ASTFromFile(fp, pathname, NULL,
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000869 Py_file_input, 0, 0, &flags,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000870 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000872 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000874 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000875 return co;
876}
877
878
Guido van Rossum55a83382000-09-20 20:31:38 +0000879/* Helper to open a bytecode file for writing in exclusive mode */
880
881static FILE *
Christian Heimes05e8be12008-02-23 18:30:17 +0000882open_exclusive(char *filename, mode_t mode)
Guido van Rossum55a83382000-09-20 20:31:38 +0000883{
884#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
885 /* Use O_EXCL to avoid a race condition when another process tries to
886 write the same file. When that happens, our open() call fails,
887 which is just fine (since it's only a cache).
888 XXX If the file exists and is writable but the directory is not
889 writable, the file will never be written. Oh well.
890 */
891 int fd;
892 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000893 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
894#ifdef O_BINARY
895 |O_BINARY /* necessary for Windows */
896#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000897#ifdef __VMS
Christian Heimes05e8be12008-02-23 18:30:17 +0000898 , mode, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000899#else
Christian Heimes05e8be12008-02-23 18:30:17 +0000900 , mode
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000901#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000902 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000903 if (fd < 0)
904 return NULL;
905 return fdopen(fd, "wb");
906#else
907 /* Best we can do -- on Windows this can't happen anyway */
908 return fopen(filename, "wb");
909#endif
910}
911
912
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000913/* Write a compiled module to a file, placing the time of last
914 modification of its source into the header.
915 Errors are ignored, if a write error occurs an attempt is made to
916 remove the file. */
917
918static void
Christian Heimes05e8be12008-02-23 18:30:17 +0000919write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000920{
921 FILE *fp;
Christian Heimes05e8be12008-02-23 18:30:17 +0000922 time_t mtime = srcstat->st_mtime;
Alexandre Vassalotti9d58e3e2009-07-17 10:55:50 +0000923#ifdef MS_WINDOWS /* since Windows uses different permissions */
924 mode_t mode = srcstat->st_mode & ~S_IEXEC;
925#else
926 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000927#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000928
Christian Heimes05e8be12008-02-23 18:30:17 +0000929 fp = open_exclusive(cpathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000930 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000931 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000932 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000933 "# can't create %s\n", cpathname);
934 return;
935 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000936 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000937 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000938 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
939 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000940 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000941 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000942 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000943 /* Don't keep partial file */
944 fclose(fp);
945 (void) unlink(cpathname);
946 return;
947 }
948 /* Now write the true mtime */
949 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000950 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000951 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000952 fflush(fp);
953 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000954 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000955 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000956}
957
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000958static void
959update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
960{
961 PyObject *constants, *tmp;
962 Py_ssize_t i, n;
963
964 if (PyUnicode_Compare(co->co_filename, oldname))
965 return;
966
967 tmp = co->co_filename;
968 co->co_filename = newname;
969 Py_INCREF(co->co_filename);
970 Py_DECREF(tmp);
971
972 constants = co->co_consts;
973 n = PyTuple_GET_SIZE(constants);
974 for (i = 0; i < n; i++) {
975 tmp = PyTuple_GET_ITEM(constants, i);
976 if (PyCode_Check(tmp))
977 update_code_filenames((PyCodeObject *)tmp,
978 oldname, newname);
979 }
980}
981
982static int
983update_compiled_module(PyCodeObject *co, char *pathname)
984{
985 PyObject *oldname, *newname;
986
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000987 newname = PyUnicode_DecodeFSDefault(pathname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000988 if (newname == NULL)
989 return -1;
990
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000991 if (!PyUnicode_Compare(co->co_filename, newname)) {
992 Py_DECREF(newname);
993 return 0;
994 }
995
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000996 oldname = co->co_filename;
997 Py_INCREF(oldname);
998 update_code_filenames(co, oldname, newname);
999 Py_DECREF(oldname);
1000 Py_DECREF(newname);
1001 return 1;
1002}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001003
1004/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001005 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
1006 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001007
Guido van Rossum79f25d91997-04-29 20:08:16 +00001008static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001009load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001010{
Christian Heimes05e8be12008-02-23 18:30:17 +00001011 struct stat st;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001012 FILE *fpc;
1013 char buf[MAXPATHLEN+1];
1014 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001015 PyCodeObject *co;
1016 PyObject *m;
Brett Cannon9a5b25a2010-03-01 02:09:17 +00001017
Christian Heimes05e8be12008-02-23 18:30:17 +00001018 if (fstat(fileno(fp), &st) != 0) {
Neal Norwitz708e51a2005-10-03 04:48:15 +00001019 PyErr_Format(PyExc_RuntimeError,
Christian Heimes05e8be12008-02-23 18:30:17 +00001020 "unable to get file status from '%s'",
Neal Norwitz708e51a2005-10-03 04:48:15 +00001021 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +00001022 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +00001023 }
Fred Drake4c82b232000-06-30 16:18:57 +00001024#if SIZEOF_TIME_T > 4
1025 /* Python's .pyc timestamp handling presumes that the timestamp fits
1026 in 4 bytes. This will be fine until sometime in the year 2038,
1027 when a 4-byte signed time_t will overflow.
1028 */
Christian Heimes05e8be12008-02-23 18:30:17 +00001029 if (st.st_mtime >> 32) {
Fred Drake4c82b232000-06-30 16:18:57 +00001030 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +00001031 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +00001032 return NULL;
1033 }
1034#endif
Tim Peters36515e22001-11-18 04:06:29 +00001035 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +00001036 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001037 if (cpathname != NULL &&
Christian Heimes05e8be12008-02-23 18:30:17 +00001038 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001039 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001040 fclose(fpc);
1041 if (co == NULL)
1042 return NULL;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001043 if (update_compiled_module(co, pathname) < 0)
1044 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001045 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001046 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001047 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001048 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001049 }
1050 else {
1051 co = parse_source_module(pathname, fp);
1052 if (co == NULL)
1053 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001054 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001055 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001056 name, pathname);
Christian Heimes790c8232008-01-07 21:14:23 +00001057 if (cpathname) {
1058 PyObject *ro = PySys_GetObject("dont_write_bytecode");
1059 if (ro == NULL || !PyObject_IsTrue(ro))
Christian Heimes05e8be12008-02-23 18:30:17 +00001060 write_compiled_module(co, cpathname, &st);
Christian Heimes790c8232008-01-07 21:14:23 +00001061 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001062 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001063 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001064 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001065
1066 return m;
1067}
1068
Christian Heimes3b06e532008-01-07 20:12:44 +00001069/* Get source file -> unicode or None
1070 * Returns the path to the py file if available, else the given path
1071 */
1072static PyObject *
1073get_sourcefile(const char *file)
1074{
1075 char py[MAXPATHLEN + 1];
1076 Py_ssize_t len;
1077 PyObject *u;
1078 struct stat statbuf;
1079
1080 if (!file || !*file) {
1081 Py_RETURN_NONE;
1082 }
1083
1084 len = strlen(file);
Christian Heimes3540b502008-01-11 07:03:05 +00001085 /* match '*.py?' */
1086 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {
Christian Heimes3b06e532008-01-07 20:12:44 +00001087 return PyUnicode_DecodeFSDefault(file);
1088 }
1089
1090 strncpy(py, file, len-1);
Martin v. Löwis5021ebc2008-03-22 22:07:13 +00001091 py[len-1] = '\0';
Christian Heimes3b06e532008-01-07 20:12:44 +00001092 if (stat(py, &statbuf) == 0 &&
1093 S_ISREG(statbuf.st_mode)) {
1094 u = PyUnicode_DecodeFSDefault(py);
1095 }
1096 else {
1097 u = PyUnicode_DecodeFSDefault(file);
1098 }
1099 return u;
1100}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001101
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001102/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001103static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
1104static struct filedescr *find_module(char *, char *, PyObject *,
1105 char *, size_t, FILE **, PyObject **);
Christian Heimes3b06e532008-01-07 20:12:44 +00001106static struct _frozen * find_frozen(char *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001107
1108/* Load a package and return its module object WITH INCREMENTED
1109 REFERENCE COUNT */
1110
1111static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001112load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001113{
Tim Peters1cd70172004-08-02 03:52:12 +00001114 PyObject *m, *d;
1115 PyObject *file = NULL;
1116 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001117 int err;
1118 char buf[MAXPATHLEN+1];
1119 FILE *fp = NULL;
1120 struct filedescr *fdp;
1121
1122 m = PyImport_AddModule(name);
1123 if (m == NULL)
1124 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001125 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001126 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001127 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001128 d = PyModule_GetDict(m);
Christian Heimes3b06e532008-01-07 20:12:44 +00001129 file = get_sourcefile(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001130 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +00001131 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001132 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +00001133 if (path == NULL)
1134 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001135 err = PyDict_SetItemString(d, "__file__", file);
1136 if (err == 0)
1137 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +00001138 if (err != 0)
1139 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001140 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001141 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001142 if (fdp == NULL) {
1143 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1144 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +00001145 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001146 }
1147 else
1148 m = NULL;
1149 goto cleanup;
1150 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001151 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001152 if (fp != NULL)
1153 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001154 goto cleanup;
1155
1156 error:
1157 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001158 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001159 Py_XDECREF(path);
1160 Py_XDECREF(file);
1161 return m;
1162}
1163
1164
1165/* Helper to test for built-in module */
1166
1167static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001168is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001169{
1170 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001171 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1172 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1173 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001174 return -1;
1175 else
1176 return 1;
1177 }
1178 }
1179 return 0;
1180}
1181
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001182
Just van Rossum52e14d62002-12-30 22:08:05 +00001183/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1184 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001185 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001186 that can handle the path item. Return None if no hook could;
1187 this tells our caller it should fall back to the builtin
1188 import mechanism. Cache the result in path_importer_cache.
1189 Returns a borrowed reference. */
1190
1191static PyObject *
1192get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1193 PyObject *p)
1194{
1195 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001196 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001197
1198 /* These conditions are the caller's responsibility: */
1199 assert(PyList_Check(path_hooks));
1200 assert(PyDict_Check(path_importer_cache));
1201
1202 nhooks = PyList_Size(path_hooks);
1203 if (nhooks < 0)
1204 return NULL; /* Shouldn't happen */
1205
1206 importer = PyDict_GetItem(path_importer_cache, p);
1207 if (importer != NULL)
1208 return importer;
1209
1210 /* set path_importer_cache[p] to None to avoid recursion */
1211 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1212 return NULL;
1213
1214 for (j = 0; j < nhooks; j++) {
1215 PyObject *hook = PyList_GetItem(path_hooks, j);
1216 if (hook == NULL)
1217 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001218 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001219 if (importer != NULL)
1220 break;
1221
1222 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1223 return NULL;
1224 }
1225 PyErr_Clear();
1226 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001227 if (importer == NULL) {
1228 importer = PyObject_CallFunctionObjArgs(
Christian Heimes9cd17752007-11-18 19:35:23 +00001229 (PyObject *)&PyNullImporter_Type, p, NULL
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001230 );
1231 if (importer == NULL) {
1232 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1233 PyErr_Clear();
1234 return Py_None;
1235 }
1236 }
1237 }
1238 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001239 int err = PyDict_SetItem(path_importer_cache, p, importer);
1240 Py_DECREF(importer);
1241 if (err != 0)
1242 return NULL;
1243 }
1244 return importer;
1245}
1246
Christian Heimes9cd17752007-11-18 19:35:23 +00001247PyAPI_FUNC(PyObject *)
1248PyImport_GetImporter(PyObject *path) {
1249 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
1250
1251 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1252 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1253 importer = get_path_importer(path_importer_cache,
1254 path_hooks, path);
1255 }
1256 }
1257 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1258 return importer;
1259}
1260
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001261/* Search the path (default sys.path) for a module. Return the
1262 corresponding filedescr struct, and (via return arguments) the
1263 pathname and an open file. Return NULL if the module is not found. */
1264
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001265#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001266extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001267 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001268#endif
1269
Martin v. Löwis18e16552006-02-15 17:27:45 +00001270static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001271static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001272static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001273
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001274static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001275find_module(char *fullname, char *subname, PyObject *path, char *buf,
1276 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001277{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001278 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001279 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001280 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001281 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001282 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001283 PyObject *path_hooks, *path_importer_cache;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001284 struct stat statbuf;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001285 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1286 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1287 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001288 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001289#if defined(PYOS_OS2)
1290 size_t saved_len;
1291 size_t saved_namelen;
1292 char *saved_buf = NULL;
1293#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001294 if (p_loader != NULL)
1295 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001296
Just van Rossum52e14d62002-12-30 22:08:05 +00001297 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001298 PyErr_SetString(PyExc_OverflowError,
1299 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001300 return NULL;
1301 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001302 strcpy(name, subname);
1303
1304 /* sys.meta_path import hook */
1305 if (p_loader != NULL) {
1306 PyObject *meta_path;
1307
1308 meta_path = PySys_GetObject("meta_path");
1309 if (meta_path == NULL || !PyList_Check(meta_path)) {
1310 PyErr_SetString(PyExc_ImportError,
1311 "sys.meta_path must be a list of "
1312 "import hooks");
1313 return NULL;
1314 }
1315 Py_INCREF(meta_path); /* zap guard */
1316 npath = PyList_Size(meta_path);
1317 for (i = 0; i < npath; i++) {
1318 PyObject *loader;
1319 PyObject *hook = PyList_GetItem(meta_path, i);
1320 loader = PyObject_CallMethod(hook, "find_module",
1321 "sO", fullname,
1322 path != NULL ?
1323 path : Py_None);
1324 if (loader == NULL) {
1325 Py_DECREF(meta_path);
1326 return NULL; /* true error */
1327 }
1328 if (loader != Py_None) {
1329 /* a loader was found */
1330 *p_loader = loader;
1331 Py_DECREF(meta_path);
1332 return &importhookdescr;
1333 }
1334 Py_DECREF(loader);
1335 }
1336 Py_DECREF(meta_path);
1337 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001338
Benjamin Petersond968e272008-11-05 22:48:33 +00001339 if (find_frozen(fullname) != NULL) {
1340 strcpy(buf, fullname);
1341 return &fd_frozen;
Guido van Rossum0506a431998-08-11 15:07:39 +00001342 }
Benjamin Petersond968e272008-11-05 22:48:33 +00001343
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001344 if (path == NULL) {
1345 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001346 strcpy(buf, name);
1347 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001348 }
Guido van Rossumac279101996-08-22 23:10:58 +00001349#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001350 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1351 if (fp != NULL) {
1352 *p_fp = fp;
1353 return fdp;
1354 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001355#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001356 path = PySys_GetObject("path");
1357 }
Benjamin Petersond968e272008-11-05 22:48:33 +00001358
Guido van Rossum79f25d91997-04-29 20:08:16 +00001359 if (path == NULL || !PyList_Check(path)) {
1360 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001361 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001362 return NULL;
1363 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001364
1365 path_hooks = PySys_GetObject("path_hooks");
1366 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1367 PyErr_SetString(PyExc_ImportError,
1368 "sys.path_hooks must be a list of "
1369 "import hooks");
1370 return NULL;
1371 }
1372 path_importer_cache = PySys_GetObject("path_importer_cache");
1373 if (path_importer_cache == NULL ||
1374 !PyDict_Check(path_importer_cache)) {
1375 PyErr_SetString(PyExc_ImportError,
1376 "sys.path_importer_cache must be a dict");
1377 return NULL;
1378 }
1379
Guido van Rossum79f25d91997-04-29 20:08:16 +00001380 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001381 namelen = strlen(name);
1382 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001383 PyObject *v = PyList_GetItem(path, i);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001384 PyObject *origv = v;
Guido van Rossum6262cc72007-05-09 23:29:27 +00001385 const char *base;
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001386 Py_ssize_t size;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001387 if (!v)
1388 return NULL;
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001389 if (PyUnicode_Check(v)) {
Brett Cannon9a5b25a2010-03-01 02:09:17 +00001390 v = PyUnicode_AsEncodedString(v,
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001391 Py_FileSystemDefaultEncoding, NULL);
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001392 if (v == NULL)
1393 return NULL;
1394 }
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001395 else if (!PyBytes_Check(v))
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001396 continue;
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001397 else
1398 Py_INCREF(v);
1399
Christian Heimes72b710a2008-05-26 13:28:38 +00001400 base = PyBytes_AS_STRING(v);
1401 size = PyBytes_GET_SIZE(v);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001402 len = size;
Walter Dörwald3430d702002-06-17 10:43:59 +00001403 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001404 Py_DECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001405 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001406 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001407 strcpy(buf, base);
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001408 Py_DECREF(v);
1409
Walter Dörwald3430d702002-06-17 10:43:59 +00001410 if (strlen(buf) != len) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001411 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001412 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001413
1414 /* sys.path_hooks import hook */
1415 if (p_loader != NULL) {
1416 PyObject *importer;
1417
1418 importer = get_path_importer(path_importer_cache,
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001419 path_hooks, origv);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001420 if (importer == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001421 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001422 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001423 /* Note: importer is a borrowed reference */
1424 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001425 PyObject *loader;
1426 loader = PyObject_CallMethod(importer,
1427 "find_module",
1428 "s", fullname);
1429 if (loader == NULL)
1430 return NULL; /* error */
1431 if (loader != Py_None) {
1432 /* a loader was found */
1433 *p_loader = loader;
1434 return &importhookdescr;
1435 }
1436 Py_DECREF(loader);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001437 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001438 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001439 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001440 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001441
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001442 if (len > 0 && buf[len-1] != SEP
1443#ifdef ALTSEP
1444 && buf[len-1] != ALTSEP
1445#endif
1446 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001447 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001448 strcpy(buf+len, name);
1449 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001450
1451 /* Check for package import (buf holds a directory name,
1452 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001453#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001454 if (stat(buf, &statbuf) == 0 && /* it exists */
1455 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001456 case_ok(buf, len, namelen, name)) { /* case matches */
1457 if (find_init_module(buf)) { /* and has __init__.py */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001458 return &fd_package;
1459 }
1460 else {
1461 char warnstr[MAXPATHLEN+80];
1462 sprintf(warnstr, "Not importing directory "
Brett Cannon9a5b25a2010-03-01 02:09:17 +00001463 "'%.*s': missing __init__.py",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001464 MAXPATHLEN, buf);
Skip Montanaro46fc3372007-08-12 11:44:53 +00001465 if (PyErr_WarnEx(PyExc_ImportWarning,
1466 warnstr, 1)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001467 return NULL;
1468 }
1469 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001470 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001471#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001472#if defined(PYOS_OS2)
1473 /* take a snapshot of the module spec for restoration
1474 * after the 8 character DLL hackery
1475 */
1476 saved_buf = strdup(buf);
1477 saved_len = len;
1478 saved_namelen = namelen;
1479#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001480 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum04110fb2007-08-24 16:32:05 +00001481#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001482 /* OS/2 limits DLLs to 8 character names (w/o
1483 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001484 * so if the name is longer than that and its a
1485 * dynamically loaded module we're going to try,
1486 * truncate the name before trying
1487 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001488 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001489 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001490 const struct filedescr *scan;
1491 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001492 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001493 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001494 break;
1495 else
1496 scan++;
1497 }
1498 if (scan->suffix != NULL) {
1499 /* yes, so truncate the name */
1500 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001501 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001502 buf[len] = '\0';
1503 }
1504 }
1505#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001506 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001507 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001508 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001509 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001510 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001511 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001512 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001513 if (fp != NULL) {
1514 if (case_ok(buf, len, namelen, name))
1515 break;
1516 else { /* continue search */
1517 fclose(fp);
1518 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001519 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001520 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001521#if defined(PYOS_OS2)
1522 /* restore the saved snapshot */
1523 strcpy(buf, saved_buf);
1524 len = saved_len;
1525 namelen = saved_namelen;
1526#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001527 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001528#if defined(PYOS_OS2)
1529 /* don't need/want the module name snapshot anymore */
1530 if (saved_buf)
1531 {
1532 free(saved_buf);
1533 saved_buf = NULL;
1534 }
1535#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001536 if (fp != NULL)
1537 break;
1538 }
1539 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001540 PyErr_Format(PyExc_ImportError,
1541 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001542 return NULL;
1543 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001544 *p_fp = fp;
1545 return fdp;
1546}
1547
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001548/* Helpers for main.c
1549 * Find the source file corresponding to a named module
1550 */
1551struct filedescr *
1552_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1553 size_t buflen, FILE **p_fp, PyObject **p_loader)
1554{
1555 return find_module((char *) name, (char *) name, path,
1556 buf, buflen, p_fp, p_loader);
1557}
1558
1559PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1560{
1561 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1562}
1563
Martin v. Löwis18e16552006-02-15 17:27:45 +00001564/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001565 * The arguments here are tricky, best shown by example:
1566 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1567 * ^ ^ ^ ^
1568 * |--------------------- buf ---------------------|
1569 * |------------------- len ------------------|
1570 * |------ name -------|
1571 * |----- namelen -----|
1572 * buf is the full path, but len only counts up to (& exclusive of) the
1573 * extension. name is the module name, also exclusive of extension.
1574 *
1575 * We've already done a successful stat() or fopen() on buf, so know that
1576 * there's some match, possibly case-insensitive.
1577 *
Tim Peters50d8d372001-02-28 05:34:27 +00001578 * case_ok() is to return 1 if there's a case-sensitive match for
1579 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1580 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001581 *
Tim Peters50d8d372001-02-28 05:34:27 +00001582 * case_ok() is used to implement case-sensitive import semantics even
1583 * on platforms with case-insensitive filesystems. It's trivial to implement
1584 * for case-sensitive filesystems. It's pretty much a cross-platform
1585 * nightmare for systems with case-insensitive filesystems.
1586 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001587
Tim Peters50d8d372001-02-28 05:34:27 +00001588/* First we may need a pile of platform-specific header files; the sequence
1589 * of #if's here should match the sequence in the body of case_ok().
1590 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001591#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001592#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001593
Tim Peters50d8d372001-02-28 05:34:27 +00001594#elif defined(DJGPP)
1595#include <dir.h>
1596
Jason Tishler7961aa62005-05-20 00:56:54 +00001597#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001598#include <sys/types.h>
1599#include <dirent.h>
1600
Andrew MacIntyred9400542002-02-26 11:41:34 +00001601#elif defined(PYOS_OS2)
1602#define INCL_DOS
1603#define INCL_DOSERRORS
1604#define INCL_NOPMAPI
1605#include <os2.h>
Tim Peters50d8d372001-02-28 05:34:27 +00001606#endif
1607
Guido van Rossum0980bd91998-02-13 17:18:36 +00001608static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001609case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001610{
Tim Peters50d8d372001-02-28 05:34:27 +00001611/* Pick a platform-specific implementation; the sequence of #if's here should
1612 * match the sequence just above.
1613 */
1614
Jason Tishler7961aa62005-05-20 00:56:54 +00001615/* MS_WINDOWS */
1616#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001617 WIN32_FIND_DATA data;
1618 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001619
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001620 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001621 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001622
Guido van Rossum0980bd91998-02-13 17:18:36 +00001623 h = FindFirstFile(buf, &data);
1624 if (h == INVALID_HANDLE_VALUE) {
1625 PyErr_Format(PyExc_NameError,
1626 "Can't find file for module %.100s\n(filename %.300s)",
1627 name, buf);
1628 return 0;
1629 }
1630 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001631 return strncmp(data.cFileName, name, namelen) == 0;
1632
1633/* DJGPP */
1634#elif defined(DJGPP)
1635 struct ffblk ffblk;
1636 int done;
1637
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001638 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001639 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001640
1641 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1642 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001643 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001644 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001645 name, buf);
1646 return 0;
1647 }
Tim Peters50d8d372001-02-28 05:34:27 +00001648 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001649
Jason Tishler7961aa62005-05-20 00:56:54 +00001650/* new-fangled macintosh (macosx) or Cygwin */
1651#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001652 DIR *dirp;
1653 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001654 char dirname[MAXPATHLEN + 1];
1655 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001656
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001657 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001658 return 1;
1659
Tim Petersd1e87a82001-03-01 18:12:00 +00001660 /* Copy the dir component into dirname; substitute "." if empty */
1661 if (dirlen <= 0) {
1662 dirname[0] = '.';
1663 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001664 }
1665 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001666 assert(dirlen <= MAXPATHLEN);
1667 memcpy(dirname, buf, dirlen);
1668 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001669 }
1670 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001671 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001672 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001673 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001674 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001675 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001676#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001677 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001678#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001679 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001680#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001681 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001682 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001683 (void)closedir(dirp);
1684 return 1; /* Found */
1685 }
1686 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001687 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001688 }
Tim Peters430f5d42001-03-01 01:30:56 +00001689 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001690
Andrew MacIntyred9400542002-02-26 11:41:34 +00001691/* OS/2 */
1692#elif defined(PYOS_OS2)
1693 HDIR hdir = 1;
1694 ULONG srchcnt = 1;
1695 FILEFINDBUF3 ffbuf;
1696 APIRET rc;
1697
Christian Heimes790c8232008-01-07 21:14:23 +00001698 if (Py_GETENV("PYTHONCASEOK") != NULL)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001699 return 1;
1700
1701 rc = DosFindFirst(buf,
1702 &hdir,
1703 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1704 &ffbuf, sizeof(ffbuf),
1705 &srchcnt,
1706 FIL_STANDARD);
1707 if (rc != NO_ERROR)
1708 return 0;
1709 return strncmp(ffbuf.achName, name, namelen) == 0;
1710
Tim Peters50d8d372001-02-28 05:34:27 +00001711/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1712#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001713 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001714
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001715#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001716}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001717
Guido van Rossum0980bd91998-02-13 17:18:36 +00001718
Guido van Rossum197346f1997-10-31 18:38:52 +00001719#ifdef HAVE_STAT
1720/* Helper to look for __init__.py or __init__.py[co] in potential package */
1721static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001722find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001723{
Tim Peters0f9431f2001-07-05 03:47:53 +00001724 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001725 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001726 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001727 struct stat statbuf;
1728
Tim Peters0f9431f2001-07-05 03:47:53 +00001729/* For calling case_ok(buf, len, namelen, name):
1730 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1731 * ^ ^ ^ ^
1732 * |--------------------- buf ---------------------|
1733 * |------------------- len ------------------|
1734 * |------ name -------|
1735 * |----- namelen -----|
1736 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001737 if (save_len + 13 >= MAXPATHLEN)
1738 return 0;
1739 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001740 pname = buf + i;
1741 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001742 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001743 if (case_ok(buf,
1744 save_len + 9, /* len("/__init__") */
1745 8, /* len("__init__") */
1746 pname)) {
1747 buf[save_len] = '\0';
1748 return 1;
1749 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001750 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001751 i += strlen(pname);
1752 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001753 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001754 if (case_ok(buf,
1755 save_len + 9, /* len("/__init__") */
1756 8, /* len("__init__") */
1757 pname)) {
1758 buf[save_len] = '\0';
1759 return 1;
1760 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001761 }
1762 buf[save_len] = '\0';
1763 return 0;
1764}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001765
Guido van Rossum197346f1997-10-31 18:38:52 +00001766#endif /* HAVE_STAT */
1767
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001768
Tim Petersdbd9ba62000-07-09 03:09:57 +00001769static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001770
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001771/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001772 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001773
Guido van Rossum79f25d91997-04-29 20:08:16 +00001774static PyObject *
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001775load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001776{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001777 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001778 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001779 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001780
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001781 /* First check that there's an open file (if we need one) */
1782 switch (type) {
1783 case PY_SOURCE:
1784 case PY_COMPILED:
1785 if (fp == NULL) {
1786 PyErr_Format(PyExc_ValueError,
1787 "file object required for import (type code %d)",
1788 type);
1789 return NULL;
1790 }
1791 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001792
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001793 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001794
1795 case PY_SOURCE:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001796 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001797 break;
1798
1799 case PY_COMPILED:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001800 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001801 break;
1802
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001803#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001804 case C_EXTENSION:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001805 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001806 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001807#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001808
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001809 case PKG_DIRECTORY:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001810 m = load_package(name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001811 break;
1812
1813 case C_BUILTIN:
1814 case PY_FROZEN:
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00001815 if (pathname != NULL && pathname[0] != '\0')
1816 name = pathname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001817 if (type == C_BUILTIN)
1818 err = init_builtin(name);
1819 else
1820 err = PyImport_ImportFrozenModule(name);
1821 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001822 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001823 if (err == 0) {
1824 PyErr_Format(PyExc_ImportError,
1825 "Purported %s module %.200s not found",
1826 type == C_BUILTIN ?
1827 "builtin" : "frozen",
1828 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001829 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001830 }
1831 modules = PyImport_GetModuleDict();
1832 m = PyDict_GetItemString(modules, name);
1833 if (m == NULL) {
1834 PyErr_Format(
1835 PyExc_ImportError,
1836 "%s module %.200s not properly initialized",
1837 type == C_BUILTIN ?
1838 "builtin" : "frozen",
1839 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001840 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001841 }
1842 Py_INCREF(m);
1843 break;
1844
Just van Rossum52e14d62002-12-30 22:08:05 +00001845 case IMP_HOOK: {
1846 if (loader == NULL) {
1847 PyErr_SetString(PyExc_ImportError,
1848 "import hook without loader");
1849 return NULL;
1850 }
1851 m = PyObject_CallMethod(loader, "load_module", "s", name);
1852 break;
1853 }
1854
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001855 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001856 PyErr_Format(PyExc_ImportError,
1857 "Don't know how to import %.200s (type code %d)",
1858 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001859 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001860
1861 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001862
1863 return m;
1864}
1865
1866
1867/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001868 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001869 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001870
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001871static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001872init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001873{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001874 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001875
Greg Ward201baee2001-10-04 14:52:06 +00001876 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001877 return 1;
1878
Guido van Rossum771c6c81997-10-31 18:37:24 +00001879 for (p = PyImport_Inittab; p->name != NULL; p++) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00001880 PyObject *mod;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001881 if (strcmp(name, p->name) == 0) {
1882 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001883 PyErr_Format(PyExc_ImportError,
1884 "Cannot re-init internal module %.200s",
1885 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001886 return -1;
1887 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001888 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001889 PySys_WriteStderr("import %s # builtin\n", name);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001890 mod = (*p->initfunc)();
1891 if (mod == 0)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001892 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001893 if (_PyImport_FixupExtension(mod, name, name) < 0)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001894 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001895 /* FixupExtension has put the module into sys.modules,
1896 so we can release our own reference. */
1897 Py_DECREF(mod);
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001898 return 1;
1899 }
1900 }
1901 return 0;
1902}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001903
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001904
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001905/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001906
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001907static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001908find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001909{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001910 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001911
Benjamin Petersond968e272008-11-05 22:48:33 +00001912 if (!name)
1913 return NULL;
1914
Guido van Rossum79f25d91997-04-29 20:08:16 +00001915 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001916 if (p->name == NULL)
1917 return NULL;
1918 if (strcmp(p->name, name) == 0)
1919 break;
1920 }
1921 return p;
1922}
1923
Guido van Rossum79f25d91997-04-29 20:08:16 +00001924static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001925get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001926{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001927 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001928 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001929
1930 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001931 PyErr_Format(PyExc_ImportError,
1932 "No such frozen object named %.200s",
1933 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001934 return NULL;
1935 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001936 if (p->code == NULL) {
1937 PyErr_Format(PyExc_ImportError,
1938 "Excluded frozen object named %.200s",
1939 name);
1940 return NULL;
1941 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001942 size = p->size;
1943 if (size < 0)
1944 size = -size;
1945 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001946}
1947
Brett Cannon8d110132009-03-15 02:20:16 +00001948static PyObject *
1949is_frozen_package(char *name)
1950{
1951 struct _frozen *p = find_frozen(name);
1952 int size;
1953
1954 if (p == NULL) {
1955 PyErr_Format(PyExc_ImportError,
1956 "No such frozen object named %.200s",
1957 name);
1958 return NULL;
1959 }
1960
1961 size = p->size;
1962
1963 if (size < 0)
1964 Py_RETURN_TRUE;
1965 else
1966 Py_RETURN_FALSE;
1967}
1968
1969
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001970/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001971 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001972 an exception set if the initialization failed.
1973 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001974
1975int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001976PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001977{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001978 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001979 PyObject *co;
1980 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001981 int ispackage;
1982 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001983
1984 if (p == NULL)
1985 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001986 if (p->code == NULL) {
1987 PyErr_Format(PyExc_ImportError,
1988 "Excluded frozen object named %.200s",
1989 name);
1990 return -1;
1991 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001992 size = p->size;
1993 ispackage = (size < 0);
1994 if (ispackage)
1995 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001996 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001997 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001998 name, ispackage ? " package" : "");
1999 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002000 if (co == NULL)
2001 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002002 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002003 PyErr_Format(PyExc_TypeError,
2004 "frozen object %.200s is not a code object",
2005 name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002006 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002007 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00002008 if (ispackage) {
2009 /* Set __path__ to the package name */
Benjamin Petersond968e272008-11-05 22:48:33 +00002010 PyObject *d, *s, *l;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002011 int err;
2012 m = PyImport_AddModule(name);
2013 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002014 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002015 d = PyModule_GetDict(m);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002016 s = PyUnicode_InternFromString(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00002017 if (s == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002018 goto err_return;
Benjamin Petersond968e272008-11-05 22:48:33 +00002019 l = PyList_New(1);
2020 if (l == NULL) {
2021 Py_DECREF(s);
2022 goto err_return;
2023 }
2024 PyList_SET_ITEM(l, 0, s);
2025 err = PyDict_SetItemString(d, "__path__", l);
2026 Py_DECREF(l);
Guido van Rossuma5568d31998-03-05 03:45:08 +00002027 if (err != 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002028 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002029 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00002030 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002031 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002032 goto err_return;
2033 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002034 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002035 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002036err_return:
2037 Py_DECREF(co);
2038 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002039}
Guido van Rossum74e6a111994-08-29 12:54:38 +00002040
2041
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002042/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002043 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00002044
Guido van Rossum79f25d91997-04-29 20:08:16 +00002045PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002046PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00002047{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002048 PyObject *pname;
2049 PyObject *result;
2050
Martin v. Löwis5b222132007-06-10 09:51:05 +00002051 pname = PyUnicode_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00002052 if (pname == NULL)
2053 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002054 result = PyImport_Import(pname);
2055 Py_DECREF(pname);
2056 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002057}
2058
Christian Heimes072c0f12008-01-03 23:01:04 +00002059/* Import a module without blocking
2060 *
2061 * At first it tries to fetch the module from sys.modules. If the module was
2062 * never loaded before it loads it with PyImport_ImportModule() unless another
2063 * thread holds the import lock. In the latter case the function raises an
2064 * ImportError instead of blocking.
2065 *
2066 * Returns the module object with incremented ref count.
2067 */
2068PyObject *
2069PyImport_ImportModuleNoBlock(const char *name)
2070{
2071 PyObject *result;
2072 PyObject *modules;
2073 long me;
2074
2075 /* Try to get the module from sys.modules[name] */
2076 modules = PyImport_GetModuleDict();
2077 if (modules == NULL)
2078 return NULL;
2079
2080 result = PyDict_GetItemString(modules, name);
2081 if (result != NULL) {
2082 Py_INCREF(result);
2083 return result;
2084 }
2085 else {
2086 PyErr_Clear();
2087 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002088#ifdef WITH_THREAD
Christian Heimes072c0f12008-01-03 23:01:04 +00002089 /* check the import lock
2090 * me might be -1 but I ignore the error here, the lock function
2091 * takes care of the problem */
2092 me = PyThread_get_thread_ident();
2093 if (import_lock_thread == -1 || import_lock_thread == me) {
2094 /* no thread or me is holding the lock */
2095 return PyImport_ImportModule(name);
2096 }
2097 else {
2098 PyErr_Format(PyExc_ImportError,
2099 "Failed to import %.200s because the import lock"
2100 "is held by another thread.",
2101 name);
2102 return NULL;
2103 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002104#else
2105 return PyImport_ImportModule(name);
2106#endif
Christian Heimes072c0f12008-01-03 23:01:04 +00002107}
2108
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002109/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002110static PyObject *get_parent(PyObject *globals, char *buf,
2111 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002112static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002113 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002114static int mark_miss(char *name);
2115static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002116 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002117static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002118
2119/* The Magnum Opus of dotted-name import :-) */
2120
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002121static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002122import_module_level(char *name, PyObject *globals, PyObject *locals,
2123 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002124{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002125 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00002126 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002127 PyObject *parent, *head, *next, *tail;
2128
Christian Heimes454f37b2008-01-10 00:10:02 +00002129 if (strchr(name, '/') != NULL
2130#ifdef MS_WINDOWS
2131 || strchr(name, '\\') != NULL
2132#endif
2133 ) {
2134 PyErr_SetString(PyExc_ImportError,
2135 "Import by filename is not supported.");
2136 return NULL;
2137 }
2138
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002139 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002140 if (parent == NULL)
2141 return NULL;
2142
2143 head = load_next(parent, Py_None, &name, buf, &buflen);
2144 if (head == NULL)
2145 return NULL;
2146
2147 tail = head;
2148 Py_INCREF(tail);
2149 while (name) {
2150 next = load_next(tail, tail, &name, buf, &buflen);
2151 Py_DECREF(tail);
2152 if (next == NULL) {
2153 Py_DECREF(head);
2154 return NULL;
2155 }
2156 tail = next;
2157 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002158 if (tail == Py_None) {
2159 /* If tail is Py_None, both get_parent and load_next found
2160 an empty module name: someone called __import__("") or
2161 doctored faulty bytecode */
2162 Py_DECREF(tail);
2163 Py_DECREF(head);
2164 PyErr_SetString(PyExc_ValueError,
2165 "Empty module name");
2166 return NULL;
2167 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002168
2169 if (fromlist != NULL) {
2170 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2171 fromlist = NULL;
2172 }
2173
2174 if (fromlist == NULL) {
2175 Py_DECREF(tail);
2176 return head;
2177 }
2178
2179 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002180 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002181 Py_DECREF(tail);
2182 return NULL;
2183 }
2184
2185 return tail;
2186}
2187
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002188PyObject *
2189PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2190 PyObject *fromlist, int level)
2191{
2192 PyObject *result;
Benjamin Peterson0df35a92009-10-04 20:32:25 +00002193 _PyImport_AcquireLock();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002194 result = import_module_level(name, globals, locals, fromlist, level);
Benjamin Peterson0df35a92009-10-04 20:32:25 +00002195 if (_PyImport_ReleaseLock() < 0) {
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002196 Py_XDECREF(result);
2197 PyErr_SetString(PyExc_RuntimeError,
2198 "not holding the import lock");
2199 return NULL;
2200 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002201 return result;
2202}
2203
Fred Drake87590902004-05-28 20:21:36 +00002204/* Return the package that an import is being performed in. If globals comes
2205 from the module foo.bar.bat (not itself a package), this returns the
2206 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002207 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002208
2209 The *name* of the returned package is returned in buf, with the length of
2210 the name in *p_buflen.
2211
2212 If globals doesn't come from a package or a module in a package, or a
2213 corresponding entry is not found in sys.modules, Py_None is returned.
2214*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002215static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002216get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002217{
2218 static PyObject *namestr = NULL;
2219 static PyObject *pathstr = NULL;
Nick Coghlande10c852007-12-04 12:22:52 +00002220 static PyObject *pkgstr = NULL;
2221 PyObject *pkgname, *modname, *modpath, *modules, *parent;
Georg Brandl2ee470f2008-07-16 12:55:28 +00002222 int orig_level = level;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002223
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002224 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002225 return Py_None;
2226
2227 if (namestr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002228 namestr = PyUnicode_InternFromString("__name__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002229 if (namestr == NULL)
2230 return NULL;
2231 }
2232 if (pathstr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002233 pathstr = PyUnicode_InternFromString("__path__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002234 if (pathstr == NULL)
2235 return NULL;
2236 }
Nick Coghlande10c852007-12-04 12:22:52 +00002237 if (pkgstr == NULL) {
2238 pkgstr = PyUnicode_InternFromString("__package__");
2239 if (pkgstr == NULL)
2240 return NULL;
2241 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002242
2243 *buf = '\0';
2244 *p_buflen = 0;
Nick Coghlande10c852007-12-04 12:22:52 +00002245 pkgname = PyDict_GetItem(globals, pkgstr);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002246
Nick Coghlande10c852007-12-04 12:22:52 +00002247 if ((pkgname != NULL) && (pkgname != Py_None)) {
2248 /* __package__ is set, so use it */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002249 char *pkgname_str;
Nick Coghlande10c852007-12-04 12:22:52 +00002250 Py_ssize_t len;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002251
Nick Coghlande10c852007-12-04 12:22:52 +00002252 if (!PyUnicode_Check(pkgname)) {
2253 PyErr_SetString(PyExc_ValueError,
2254 "__package__ set to non-string");
2255 return NULL;
2256 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002257 pkgname_str = _PyUnicode_AsStringAndSize(pkgname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002258 if (len == 0) {
2259 if (level > 0) {
2260 PyErr_SetString(PyExc_ValueError,
2261 "Attempted relative import in non-package");
2262 return NULL;
2263 }
2264 return Py_None;
2265 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002266 if (len > MAXPATHLEN) {
2267 PyErr_SetString(PyExc_ValueError,
Nick Coghlande10c852007-12-04 12:22:52 +00002268 "Package name too long");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002269 return NULL;
2270 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002271 strcpy(buf, pkgname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002272 } else {
2273 /* __package__ not set, so figure it out and set it */
2274 modname = PyDict_GetItem(globals, namestr);
2275 if (modname == NULL || !PyUnicode_Check(modname))
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002276 return Py_None;
Brett Cannon9a5b25a2010-03-01 02:09:17 +00002277
Nick Coghlande10c852007-12-04 12:22:52 +00002278 modpath = PyDict_GetItem(globals, pathstr);
2279 if (modpath != NULL) {
2280 /* __path__ is set, so modname is already the package name */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002281 char *modname_str;
2282 Py_ssize_t len;
Nick Coghlande10c852007-12-04 12:22:52 +00002283 int error;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002284
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002285 modname_str = _PyUnicode_AsStringAndSize(modname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002286 if (len > MAXPATHLEN) {
2287 PyErr_SetString(PyExc_ValueError,
2288 "Module name too long");
2289 return NULL;
2290 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002291 strcpy(buf, modname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002292 error = PyDict_SetItem(globals, pkgstr, modname);
2293 if (error) {
2294 PyErr_SetString(PyExc_ValueError,
2295 "Could not set __package__");
2296 return NULL;
2297 }
2298 } else {
2299 /* Normal module, so work out the package name if any */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002300 char *start = _PyUnicode_AsString(modname);
Nick Coghlande10c852007-12-04 12:22:52 +00002301 char *lastdot = strrchr(start, '.');
2302 size_t len;
2303 int error;
2304 if (lastdot == NULL && level > 0) {
2305 PyErr_SetString(PyExc_ValueError,
2306 "Attempted relative import in non-package");
2307 return NULL;
2308 }
2309 if (lastdot == NULL) {
2310 error = PyDict_SetItem(globals, pkgstr, Py_None);
2311 if (error) {
2312 PyErr_SetString(PyExc_ValueError,
2313 "Could not set __package__");
2314 return NULL;
2315 }
2316 return Py_None;
2317 }
2318 len = lastdot - start;
2319 if (len >= MAXPATHLEN) {
2320 PyErr_SetString(PyExc_ValueError,
2321 "Module name too long");
2322 return NULL;
2323 }
2324 strncpy(buf, start, len);
2325 buf[len] = '\0';
2326 pkgname = PyUnicode_FromString(buf);
2327 if (pkgname == NULL) {
2328 return NULL;
2329 }
2330 error = PyDict_SetItem(globals, pkgstr, pkgname);
2331 Py_DECREF(pkgname);
2332 if (error) {
2333 PyErr_SetString(PyExc_ValueError,
2334 "Could not set __package__");
2335 return NULL;
2336 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002337 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002338 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002339 while (--level > 0) {
2340 char *dot = strrchr(buf, '.');
2341 if (dot == NULL) {
2342 PyErr_SetString(PyExc_ValueError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002343 "Attempted relative import beyond "
2344 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002345 return NULL;
2346 }
2347 *dot = '\0';
2348 }
2349 *p_buflen = strlen(buf);
2350
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002351 modules = PyImport_GetModuleDict();
2352 parent = PyDict_GetItemString(modules, buf);
Georg Brandl2ee470f2008-07-16 12:55:28 +00002353 if (parent == NULL) {
2354 if (orig_level < 1) {
2355 PyObject *err_msg = PyBytes_FromFormat(
2356 "Parent module '%.200s' not found "
2357 "while handling absolute import", buf);
2358 if (err_msg == NULL) {
2359 return NULL;
2360 }
2361 if (!PyErr_WarnEx(PyExc_RuntimeWarning,
2362 PyBytes_AsString(err_msg), 1)) {
2363 *buf = '\0';
2364 *p_buflen = 0;
2365 parent = Py_None;
2366 }
2367 Py_DECREF(err_msg);
2368 } else {
2369 PyErr_Format(PyExc_SystemError,
2370 "Parent module '%.200s' not loaded, "
2371 "cannot perform relative import", buf);
2372 }
2373 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002374 return parent;
2375 /* We expect, but can't guarantee, if parent != None, that:
2376 - parent.__name__ == buf
2377 - parent.__dict__ is globals
2378 If this is violated... Who cares? */
2379}
2380
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002381/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002382static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002383load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002384 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002385{
2386 char *name = *p_name;
2387 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002388 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002389 char *p;
2390 PyObject *result;
2391
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002392 if (strlen(name) == 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002393 /* completely empty module name should only happen in
2394 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002395 Py_INCREF(mod);
2396 *p_name = NULL;
2397 return mod;
2398 }
2399
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002400 if (dot == NULL) {
2401 *p_name = NULL;
2402 len = strlen(name);
2403 }
2404 else {
2405 *p_name = dot+1;
2406 len = dot-name;
2407 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002408 if (len == 0) {
2409 PyErr_SetString(PyExc_ValueError,
2410 "Empty module name");
2411 return NULL;
2412 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002413
2414 p = buf + *p_buflen;
2415 if (p != buf)
2416 *p++ = '.';
2417 if (p+len-buf >= MAXPATHLEN) {
2418 PyErr_SetString(PyExc_ValueError,
2419 "Module name too long");
2420 return NULL;
2421 }
2422 strncpy(p, name, len);
2423 p[len] = '\0';
2424 *p_buflen = p+len-buf;
2425
2426 result = import_submodule(mod, p, buf);
2427 if (result == Py_None && altmod != mod) {
2428 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002429 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002430 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002431 if (result != NULL && result != Py_None) {
2432 if (mark_miss(buf) != 0) {
2433 Py_DECREF(result);
2434 return NULL;
2435 }
2436 strncpy(buf, name, len);
2437 buf[len] = '\0';
2438 *p_buflen = len;
2439 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002440 }
2441 if (result == NULL)
2442 return NULL;
2443
2444 if (result == Py_None) {
2445 Py_DECREF(result);
2446 PyErr_Format(PyExc_ImportError,
2447 "No module named %.200s", name);
2448 return NULL;
2449 }
2450
2451 return result;
2452}
2453
2454static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002455mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002456{
2457 PyObject *modules = PyImport_GetModuleDict();
2458 return PyDict_SetItemString(modules, name, Py_None);
2459}
2460
2461static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002462ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002463 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002464{
2465 int i;
2466
2467 if (!PyObject_HasAttrString(mod, "__path__"))
2468 return 1;
2469
2470 for (i = 0; ; i++) {
2471 PyObject *item = PySequence_GetItem(fromlist, i);
2472 int hasit;
2473 if (item == NULL) {
2474 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2475 PyErr_Clear();
2476 return 1;
2477 }
2478 return 0;
2479 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002480 if (!PyUnicode_Check(item)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002481 PyErr_SetString(PyExc_TypeError,
Neal Norwitz80e7f272007-08-26 06:45:23 +00002482 "Item in ``from list'' not a string");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002483 Py_DECREF(item);
2484 return 0;
2485 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002486 if (PyUnicode_AS_UNICODE(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002487 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002488 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002489 /* See if the package defines __all__ */
2490 if (recursive)
2491 continue; /* Avoid endless recursion */
2492 all = PyObject_GetAttrString(mod, "__all__");
2493 if (all == NULL)
2494 PyErr_Clear();
2495 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002496 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002497 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002498 if (!ret)
2499 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002500 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002501 continue;
2502 }
2503 hasit = PyObject_HasAttr(mod, item);
2504 if (!hasit) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002505 PyObject *item8;
2506 char *subname;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002507 PyObject *submod;
2508 char *p;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002509 if (!Py_FileSystemDefaultEncoding) {
2510 item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item),
2511 PyUnicode_GetSize(item),
Martin v. Löwis427dbff2007-06-12 05:53:00 +00002512 NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002513 } else {
Guido van Rossum98297ee2007-11-06 21:34:58 +00002514 item8 = PyUnicode_AsEncodedString(item,
2515 Py_FileSystemDefaultEncoding, NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002516 }
2517 if (!item8) {
2518 PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
2519 return 0;
2520 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002521 subname = PyBytes_AS_STRING(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002522 if (buflen + strlen(subname) >= MAXPATHLEN) {
2523 PyErr_SetString(PyExc_ValueError,
2524 "Module name too long");
2525 Py_DECREF(item);
2526 return 0;
2527 }
2528 p = buf + buflen;
2529 *p++ = '.';
2530 strcpy(p, subname);
2531 submod = import_submodule(mod, subname, buf);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002532 Py_DECREF(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002533 Py_XDECREF(submod);
2534 if (submod == NULL) {
2535 Py_DECREF(item);
2536 return 0;
2537 }
2538 }
2539 Py_DECREF(item);
2540 }
2541
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002542 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002543}
2544
Neil Schemenauer00b09662003-06-16 21:03:07 +00002545static int
2546add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2547 PyObject *modules)
2548{
2549 if (mod == Py_None)
2550 return 1;
2551 /* Irrespective of the success of this load, make a
2552 reference to it in the parent package module. A copy gets
2553 saved in the modules dictionary under the full name, so get a
2554 reference from there, if need be. (The exception is when the
2555 load failed with a SyntaxError -- then there's no trace in
2556 sys.modules. In that case, of course, do nothing extra.) */
2557 if (submod == NULL) {
2558 submod = PyDict_GetItemString(modules, fullname);
2559 if (submod == NULL)
2560 return 1;
2561 }
2562 if (PyModule_Check(mod)) {
2563 /* We can't use setattr here since it can give a
2564 * spurious warning if the submodule name shadows a
2565 * builtin name */
2566 PyObject *dict = PyModule_GetDict(mod);
2567 if (!dict)
2568 return 0;
2569 if (PyDict_SetItemString(dict, subname, submod) < 0)
2570 return 0;
2571 }
2572 else {
2573 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2574 return 0;
2575 }
2576 return 1;
2577}
2578
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002579static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002580import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002581{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002582 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002583 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002584
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002585 /* Require:
2586 if mod == None: subname == fullname
2587 else: mod.__name__ + "." + subname == fullname
2588 */
2589
Tim Peters50d8d372001-02-28 05:34:27 +00002590 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002591 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002592 }
2593 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002594 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002595 char buf[MAXPATHLEN+1];
2596 struct filedescr *fdp;
2597 FILE *fp = NULL;
2598
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002599 if (mod == Py_None)
2600 path = NULL;
2601 else {
2602 path = PyObject_GetAttrString(mod, "__path__");
2603 if (path == NULL) {
2604 PyErr_Clear();
2605 Py_INCREF(Py_None);
2606 return Py_None;
2607 }
2608 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002609
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002610 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002611 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2612 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002613 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002614 if (fdp == NULL) {
2615 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2616 return NULL;
2617 PyErr_Clear();
2618 Py_INCREF(Py_None);
2619 return Py_None;
2620 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002621 m = load_module(fullname, fp, buf, fdp->type, loader);
2622 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002623 if (fp)
2624 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002625 if (!add_submodule(mod, m, fullname, subname, modules)) {
2626 Py_XDECREF(m);
2627 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002628 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002629 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002630
2631 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002632}
2633
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002634
2635/* Re-import a module of any kind and return its module object, WITH
2636 INCREMENTED REFERENCE COUNT */
2637
Guido van Rossum79f25d91997-04-29 20:08:16 +00002638PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002639PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002640{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002641 PyInterpreterState *interp = PyThreadState_Get()->interp;
2642 PyObject *modules_reloading = interp->modules_reloading;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002643 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossumd8faa362007-04-27 19:54:29 +00002644 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002645 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002646 char buf[MAXPATHLEN+1];
2647 struct filedescr *fdp;
2648 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002649 PyObject *newm;
Brett Cannon9a5b25a2010-03-01 02:09:17 +00002650
Guido van Rossumd8faa362007-04-27 19:54:29 +00002651 if (modules_reloading == NULL) {
2652 Py_FatalError("PyImport_ReloadModule: "
Guido van Rossume7ba4952007-06-06 23:52:48 +00002653 "no modules_reloading dictionary!");
Guido van Rossumd8faa362007-04-27 19:54:29 +00002654 return NULL;
2655 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002656
Guido van Rossum79f25d91997-04-29 20:08:16 +00002657 if (m == NULL || !PyModule_Check(m)) {
2658 PyErr_SetString(PyExc_TypeError,
2659 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002660 return NULL;
2661 }
Neal Norwitz5616c6d2007-08-26 05:32:41 +00002662 name = (char*)PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002663 if (name == NULL)
2664 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002665 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002666 PyErr_Format(PyExc_ImportError,
2667 "reload(): module %.200s not in sys.modules",
2668 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002669 return NULL;
2670 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002671 existing_m = PyDict_GetItemString(modules_reloading, name);
2672 if (existing_m != NULL) {
2673 /* Due to a recursive reload, this module is already
2674 being reloaded. */
2675 Py_INCREF(existing_m);
2676 return existing_m;
2677 }
2678 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
2679 return NULL;
2680
Guido van Rossum222ef561997-09-06 19:41:09 +00002681 subname = strrchr(name, '.');
2682 if (subname == NULL)
2683 subname = name;
2684 else {
2685 PyObject *parentname, *parent;
Christian Heimesc4cb3b82007-11-04 12:10:01 +00002686 parentname = PyUnicode_FromStringAndSize(name, (subname-name));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002687 if (parentname == NULL) {
2688 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002689 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002690 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002691 parent = PyDict_GetItem(modules, parentname);
2692 if (parent == NULL) {
2693 PyErr_Format(PyExc_ImportError,
Victor Stinner3f1af5c2010-03-12 17:00:41 +00002694 "reload(): parent %U not in sys.modules",
2695 parentname);
Georg Brandl0c55f292005-09-14 06:56:20 +00002696 Py_DECREF(parentname);
Guido van Rossume7ba4952007-06-06 23:52:48 +00002697 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002698 return NULL;
2699 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002700 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002701 subname++;
2702 path = PyObject_GetAttrString(parent, "__path__");
2703 if (path == NULL)
2704 PyErr_Clear();
2705 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002706 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002707 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002708 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002709
2710 if (fdp == NULL) {
2711 Py_XDECREF(loader);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002712 imp_modules_reloading_clear();
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002713 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002714 }
2715
2716 newm = load_module(name, fp, buf, fdp->type, loader);
2717 Py_XDECREF(loader);
2718
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002719 if (fp)
2720 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002721 if (newm == NULL) {
2722 /* load_module probably removed name from modules because of
2723 * the error. Put back the original module object. We're
2724 * going to return NULL in this case regardless of whether
2725 * replacing name succeeds, so the return value is ignored.
2726 */
2727 PyDict_SetItemString(modules, name, m);
2728 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002729 imp_modules_reloading_clear();
Tim Peters1cd70172004-08-02 03:52:12 +00002730 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002731}
2732
2733
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002734/* Higher-level import emulator which emulates the "import" statement
2735 more accurately -- it invokes the __import__() function from the
2736 builtins of the current globals. This means that the import is
2737 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002738 environment, e.g. by "rexec".
2739 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00002740 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00002741 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002742
2743PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002744PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002745{
2746 static PyObject *silly_list = NULL;
2747 static PyObject *builtins_str = NULL;
2748 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002749 PyObject *globals = NULL;
2750 PyObject *import = NULL;
2751 PyObject *builtins = NULL;
2752 PyObject *r = NULL;
2753
2754 /* Initialize constant string objects */
2755 if (silly_list == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002756 import_str = PyUnicode_InternFromString("__import__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002757 if (import_str == NULL)
2758 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002759 builtins_str = PyUnicode_InternFromString("__builtins__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002760 if (builtins_str == NULL)
2761 return NULL;
2762 silly_list = Py_BuildValue("[s]", "__doc__");
2763 if (silly_list == NULL)
2764 return NULL;
2765 }
2766
2767 /* Get the builtins from current globals */
2768 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002769 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002770 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002771 builtins = PyObject_GetItem(globals, builtins_str);
2772 if (builtins == NULL)
2773 goto err;
2774 }
2775 else {
2776 /* No globals -- use standard builtins, and fake globals */
Georg Brandl1a3284e2007-12-02 09:40:06 +00002777 builtins = PyImport_ImportModuleLevel("builtins",
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002778 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002779 if (builtins == NULL)
2780 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002781 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2782 if (globals == NULL)
2783 goto err;
2784 }
2785
2786 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002787 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002788 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002789 if (import == NULL)
2790 PyErr_SetObject(PyExc_KeyError, import_str);
2791 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002792 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002793 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002794 if (import == NULL)
2795 goto err;
2796
Christian Heimes072c0f12008-01-03 23:01:04 +00002797 /* Call the __import__ function with the proper argument list
2798 * Always use absolute import here. */
2799 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
2800 globals, silly_list, 0, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002801
2802 err:
2803 Py_XDECREF(globals);
2804 Py_XDECREF(builtins);
2805 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002806
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002807 return r;
2808}
2809
2810
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002811/* Module 'imp' provides Python access to the primitives used for
2812 importing modules.
2813*/
2814
Guido van Rossum79f25d91997-04-29 20:08:16 +00002815static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002816imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002817{
2818 char buf[4];
2819
Guido van Rossum96774c12000-05-01 20:19:08 +00002820 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2821 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2822 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2823 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002824
Christian Heimes72b710a2008-05-26 13:28:38 +00002825 return PyBytes_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002826}
2827
Guido van Rossum79f25d91997-04-29 20:08:16 +00002828static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002829imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002830{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002831 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002832 struct filedescr *fdp;
2833
Guido van Rossum79f25d91997-04-29 20:08:16 +00002834 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002835 if (list == NULL)
2836 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002837 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2838 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002839 fdp->suffix, fdp->mode, fdp->type);
2840 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002841 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002842 return NULL;
2843 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002844 if (PyList_Append(list, item) < 0) {
2845 Py_DECREF(list);
2846 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002847 return NULL;
2848 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002849 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002850 }
2851 return list;
2852}
2853
Guido van Rossum79f25d91997-04-29 20:08:16 +00002854static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002855call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002856{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002857 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002858 PyObject *fob, *ret;
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002859 PyObject *pathobj;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002860 struct filedescr *fdp;
2861 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002862 FILE *fp = NULL;
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002863 int fd = -1;
Brett Cannon3bb42d92007-10-20 03:43:15 +00002864 char *found_encoding = NULL;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002865 char *encoding = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002866
2867 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002868 if (path == Py_None)
2869 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002870 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002871 if (fdp == NULL)
2872 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002873 if (fp != NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002874 fd = fileno(fp);
2875 if (fd != -1)
2876 fd = dup(fd);
2877 fclose(fp);
2878 fp = NULL;
2879 }
2880 if (fd != -1) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002881 if (strchr(fdp->mode, 'b') == NULL) {
Brett Cannon3bb42d92007-10-20 03:43:15 +00002882 /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
2883 memory. */
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002884 found_encoding = PyTokenizer_FindEncoding(fd);
2885 lseek(fd, 0, 0); /* Reset position */
Amaury Forgeot d'Arc1b933ed2008-09-04 22:34:09 +00002886 if (found_encoding == NULL && PyErr_Occurred())
2887 return NULL;
Brett Cannon3bb42d92007-10-20 03:43:15 +00002888 encoding = (found_encoding != NULL) ? found_encoding :
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002889 (char*)PyUnicode_GetDefaultEncoding();
2890 }
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002891 fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002892 (char*)encoding, NULL, NULL, 1);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002893 if (fob == NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002894 close(fd);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002895 PyMem_FREE(found_encoding);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002896 return NULL;
2897 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002898 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002899 else {
2900 fob = Py_None;
2901 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002902 }
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002903 pathobj = PyUnicode_DecodeFSDefault(pathname);
2904 ret = Py_BuildValue("NN(ssi)",
2905 fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002906 PyMem_FREE(found_encoding);
2907
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002908 return ret;
2909}
2910
Guido van Rossum79f25d91997-04-29 20:08:16 +00002911static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002912imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002913{
2914 char *name;
Hirokazu Yamamoto90eaaf62009-01-30 03:15:05 +00002915 PyObject *ret, *path = NULL;
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002916 if (!PyArg_ParseTuple(args, "es|O:find_module",
2917 Py_FileSystemDefaultEncoding, &name,
2918 &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002919 return NULL;
Hirokazu Yamamoto90eaaf62009-01-30 03:15:05 +00002920 ret = call_find_module(name, path);
2921 PyMem_Free(name);
2922 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002923}
2924
2925static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002926imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002927{
2928 char *name;
2929 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002930 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002931 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002932 return NULL;
2933 ret = init_builtin(name);
2934 if (ret < 0)
2935 return NULL;
2936 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002937 Py_INCREF(Py_None);
2938 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002939 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002940 m = PyImport_AddModule(name);
2941 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002942 return m;
2943}
2944
Guido van Rossum79f25d91997-04-29 20:08:16 +00002945static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002946imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002947{
2948 char *name;
2949 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002950 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002951 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002952 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002953 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002954 if (ret < 0)
2955 return NULL;
2956 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002957 Py_INCREF(Py_None);
2958 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002959 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002960 m = PyImport_AddModule(name);
2961 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002962 return m;
2963}
2964
Guido van Rossum79f25d91997-04-29 20:08:16 +00002965static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002966imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002967{
2968 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002969
Guido van Rossum43713e52000-02-29 13:59:29 +00002970 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002971 return NULL;
2972 return get_frozen_object(name);
2973}
2974
Guido van Rossum79f25d91997-04-29 20:08:16 +00002975static PyObject *
Brett Cannon8d110132009-03-15 02:20:16 +00002976imp_is_frozen_package(PyObject *self, PyObject *args)
2977{
2978 char *name;
2979
2980 if (!PyArg_ParseTuple(args, "s:is_frozen_package", &name))
2981 return NULL;
2982 return is_frozen_package(name);
2983}
2984
2985static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002986imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002987{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002988 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002989 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002990 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00002991 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002992}
2993
Guido van Rossum79f25d91997-04-29 20:08:16 +00002994static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002995imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002996{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002997 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002998 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002999 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003000 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00003001 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00003002 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003003}
3004
3005static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003006get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003007{
3008 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003009 if (mode[0] == 'U')
3010 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003011 if (fob == NULL) {
3012 fp = fopen(pathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003013 }
3014 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003015 int fd = PyObject_AsFileDescriptor(fob);
3016 if (fd == -1)
3017 return NULL;
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003018 if (!_PyVerify_fd(fd))
3019 goto error;
3020 /* the FILE struct gets a new fd, so that it can be closed
3021 * independently of the file descriptor given
3022 */
3023 fd = dup(fd);
3024 if (fd == -1)
3025 goto error;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003026 fp = fdopen(fd, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003027 }
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003028 if (fp)
3029 return fp;
3030error:
3031 PyErr_SetFromErrno(PyExc_IOError);
3032 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003033}
3034
Guido van Rossum79f25d91997-04-29 20:08:16 +00003035static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003036imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003037{
3038 char *name;
3039 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003040 PyObject *fob = NULL;
3041 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003042 FILE *fp;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003043 if (!PyArg_ParseTuple(args, "ses|O:load_compiled",
Brett Cannon9a5b25a2010-03-01 02:09:17 +00003044 &name,
3045 Py_FileSystemDefaultEncoding, &pathname,
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003046 &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003047 return NULL;
3048 fp = get_file(pathname, fob, "rb");
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003049 if (fp == NULL) {
3050 PyMem_Free(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003051 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003052 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003053 m = load_compiled_module(name, pathname, fp);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003054 fclose(fp);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003055 PyMem_Free(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003056 return m;
3057}
3058
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003059#ifdef HAVE_DYNAMIC_LOADING
3060
Guido van Rossum79f25d91997-04-29 20:08:16 +00003061static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003062imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003063{
3064 char *name;
3065 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003066 PyObject *fob = NULL;
3067 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00003068 FILE *fp = NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003069 if (!PyArg_ParseTuple(args, "ses|O:load_dynamic",
Brett Cannon9a5b25a2010-03-01 02:09:17 +00003070 &name,
3071 Py_FileSystemDefaultEncoding, &pathname,
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003072 &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003073 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003074 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00003075 fp = get_file(pathname, fob, "r");
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003076 if (fp == NULL) {
3077 PyMem_Free(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003078 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003079 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003080 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003081 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003082 PyMem_Free(pathname);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003083 if (fp)
3084 fclose(fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00003085 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003086}
3087
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003088#endif /* HAVE_DYNAMIC_LOADING */
3089
Guido van Rossum79f25d91997-04-29 20:08:16 +00003090static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003091imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003092{
3093 char *name;
3094 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003095 PyObject *fob = NULL;
3096 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003097 FILE *fp;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003098 if (!PyArg_ParseTuple(args, "ses|O:load_source",
Brett Cannon9a5b25a2010-03-01 02:09:17 +00003099 &name,
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003100 Py_FileSystemDefaultEncoding, &pathname,
3101 &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003102 return NULL;
3103 fp = get_file(pathname, fob, "r");
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003104 if (fp == NULL) {
3105 PyMem_Free(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003106 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003107 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003108 m = load_source_module(name, pathname, fp);
Kristján Valur Jónsson92af5d92009-03-31 17:47:50 +00003109 PyMem_Free(pathname);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003110 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003111 return m;
3112}
3113
Guido van Rossum79f25d91997-04-29 20:08:16 +00003114static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003115imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003116{
3117 char *name;
3118 PyObject *fob;
3119 char *pathname;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003120 PyObject * ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003121 char *suffix; /* Unused */
3122 char *mode;
3123 int type;
3124 FILE *fp;
3125
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003126 if (!PyArg_ParseTuple(args, "sOes(ssi):load_module",
Brett Cannon9a5b25a2010-03-01 02:09:17 +00003127 &name, &fob,
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003128 Py_FileSystemDefaultEncoding, &pathname,
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003129 &suffix, &mode, &type))
3130 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003131 if (*mode) {
3132 /* Mode must start with 'r' or 'U' and must not contain '+'.
3133 Implicit in this test is the assumption that the mode
3134 may contain other modifiers like 'b' or 't'. */
3135
3136 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00003137 PyErr_Format(PyExc_ValueError,
3138 "invalid file open mode %.200s", mode);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003139 PyMem_Free(pathname);
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00003140 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003141 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003142 }
3143 if (fob == Py_None)
3144 fp = NULL;
3145 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003146 fp = get_file(NULL, fob, mode);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003147 if (fp == NULL) {
3148 PyMem_Free(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003149 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003150 }
Brett Cannon9a5b25a2010-03-01 02:09:17 +00003151 }
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003152 ret = load_module(name, fp, pathname, type, NULL);
3153 PyMem_Free(pathname);
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003154 if (fp)
3155 fclose(fp);
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003156 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003157}
3158
3159static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003160imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003161{
3162 char *name;
3163 char *pathname;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003164 PyObject * ret;
Brett Cannon9a5b25a2010-03-01 02:09:17 +00003165 if (!PyArg_ParseTuple(args, "ses:load_package",
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003166 &name, Py_FileSystemDefaultEncoding, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003167 return NULL;
Guido van Rossum0ad59d42009-03-30 22:01:35 +00003168 ret = load_package(name, pathname);
3169 PyMem_Free(pathname);
3170 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003171}
3172
3173static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003174imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003175{
3176 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00003177 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003178 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003179 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003180}
3181
Christian Heimes13a7a212008-01-07 17:13:09 +00003182static PyObject *
3183imp_reload(PyObject *self, PyObject *v)
3184{
3185 return PyImport_ReloadModule(v);
3186}
3187
3188PyDoc_STRVAR(doc_reload,
3189"reload(module) -> module\n\
3190\n\
3191Reload the module. The module must have been successfully imported before.");
3192
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003193/* Doc strings */
3194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003195PyDoc_STRVAR(doc_imp,
3196"This module provides the components needed to build your own\n\
3197__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003198
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003199PyDoc_STRVAR(doc_find_module,
3200"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003201Search for a module. If path is omitted or None, search for a\n\
3202built-in, frozen or special module and continue search in sys.path.\n\
3203The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003204package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003205
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003206PyDoc_STRVAR(doc_load_module,
3207"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003208Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003209The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003210
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003211PyDoc_STRVAR(doc_get_magic,
3212"get_magic() -> string\n\
3213Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003214
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003215PyDoc_STRVAR(doc_get_suffixes,
3216"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003217Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003218that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003219
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003220PyDoc_STRVAR(doc_new_module,
3221"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003222Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003223The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003224
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003225PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00003226"lock_held() -> boolean\n\
3227Return True if the import lock is currently held, else False.\n\
3228On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00003229
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003230PyDoc_STRVAR(doc_acquire_lock,
3231"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00003232Acquires the interpreter's import lock for the current thread.\n\
3233This lock should be used by import hooks to ensure thread-safety\n\
3234when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003235On platforms without threads, this function does nothing.");
3236
3237PyDoc_STRVAR(doc_release_lock,
3238"release_lock() -> None\n\
3239Release the interpreter's import lock.\n\
3240On platforms without threads, this function does nothing.");
3241
Guido van Rossum79f25d91997-04-29 20:08:16 +00003242static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00003243 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3244 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3245 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3246 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3247 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3248 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3249 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3250 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Christian Heimes13a7a212008-01-07 17:13:09 +00003251 {"reload", imp_reload, METH_O, doc_reload},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003252 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00003253 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
Brett Cannon8d110132009-03-15 02:20:16 +00003254 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00003255 {"init_builtin", imp_init_builtin, METH_VARARGS},
3256 {"init_frozen", imp_init_frozen, METH_VARARGS},
3257 {"is_builtin", imp_is_builtin, METH_VARARGS},
3258 {"is_frozen", imp_is_frozen, METH_VARARGS},
3259 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003260#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00003261 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003262#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00003263 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00003264 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003265 {NULL, NULL} /* sentinel */
3266};
3267
Guido van Rossum1a8791e1998-08-04 22:46:29 +00003268static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003269setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003270{
3271 PyObject *v;
3272 int err;
3273
Christian Heimes217cfd12007-12-02 14:31:20 +00003274 v = PyLong_FromLong((long)value);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003275 err = PyDict_SetItemString(d, name, v);
3276 Py_XDECREF(v);
3277 return err;
3278}
3279
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003280typedef struct {
3281 PyObject_HEAD
3282} NullImporter;
3283
3284static int
3285NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3286{
3287 char *path;
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003288 Py_ssize_t pathlen;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003289
3290 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3291 return -1;
3292
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00003293 if (!PyArg_ParseTuple(args, "es:NullImporter",
3294 Py_FileSystemDefaultEncoding, &path))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003295 return -1;
3296
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003297 pathlen = strlen(path);
3298 if (pathlen == 0) {
Georg Brandl8494d572008-07-19 10:13:15 +00003299 PyMem_Free(path);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003300 PyErr_SetString(PyExc_ImportError, "empty pathname");
3301 return -1;
3302 } else {
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003303#ifndef MS_WINDOWS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003304 struct stat statbuf;
3305 int rv;
3306
3307 rv = stat(path, &statbuf);
Georg Brandl8494d572008-07-19 10:13:15 +00003308 PyMem_Free(path);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003309 if (rv == 0) {
3310 /* it exists */
3311 if (S_ISDIR(statbuf.st_mode)) {
3312 /* it's a directory */
3313 PyErr_SetString(PyExc_ImportError,
3314 "existing directory");
3315 return -1;
3316 }
3317 }
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003318#else /* MS_WINDOWS */
3319 DWORD rv;
3320 /* see issue1293 and issue3677:
3321 * stat() on Windows doesn't recognise paths like
3322 * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
3323 */
3324 rv = GetFileAttributesA(path);
Kristján Valur Jónsson92cb4382009-01-24 10:33:25 +00003325 PyMem_Free(path);
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003326 if (rv != INVALID_FILE_ATTRIBUTES) {
3327 /* it exists */
3328 if (rv & FILE_ATTRIBUTE_DIRECTORY) {
3329 /* it's a directory */
3330 PyErr_SetString(PyExc_ImportError,
3331 "existing directory");
3332 return -1;
3333 }
3334 }
3335#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003336 }
3337 return 0;
3338}
3339
3340static PyObject *
3341NullImporter_find_module(NullImporter *self, PyObject *args)
3342{
3343 Py_RETURN_NONE;
3344}
3345
3346static PyMethodDef NullImporter_methods[] = {
3347 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3348 "Always return None"
3349 },
3350 {NULL} /* Sentinel */
3351};
3352
3353
Christian Heimes9cd17752007-11-18 19:35:23 +00003354PyTypeObject PyNullImporter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003355 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003356 "imp.NullImporter", /*tp_name*/
3357 sizeof(NullImporter), /*tp_basicsize*/
3358 0, /*tp_itemsize*/
3359 0, /*tp_dealloc*/
3360 0, /*tp_print*/
3361 0, /*tp_getattr*/
3362 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00003363 0, /*tp_reserved*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003364 0, /*tp_repr*/
3365 0, /*tp_as_number*/
3366 0, /*tp_as_sequence*/
3367 0, /*tp_as_mapping*/
3368 0, /*tp_hash */
3369 0, /*tp_call*/
3370 0, /*tp_str*/
3371 0, /*tp_getattro*/
3372 0, /*tp_setattro*/
3373 0, /*tp_as_buffer*/
3374 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3375 "Null importer object", /* tp_doc */
3376 0, /* tp_traverse */
3377 0, /* tp_clear */
3378 0, /* tp_richcompare */
3379 0, /* tp_weaklistoffset */
3380 0, /* tp_iter */
3381 0, /* tp_iternext */
3382 NullImporter_methods, /* tp_methods */
3383 0, /* tp_members */
3384 0, /* tp_getset */
3385 0, /* tp_base */
3386 0, /* tp_dict */
3387 0, /* tp_descr_get */
3388 0, /* tp_descr_set */
3389 0, /* tp_dictoffset */
3390 (initproc)NullImporter_init, /* tp_init */
3391 0, /* tp_alloc */
3392 PyType_GenericNew /* tp_new */
3393};
3394
Martin v. Löwis1a214512008-06-11 05:26:20 +00003395static struct PyModuleDef impmodule = {
3396 PyModuleDef_HEAD_INIT,
3397 "imp",
3398 doc_imp,
3399 0,
3400 imp_methods,
3401 NULL,
3402 NULL,
3403 NULL,
3404 NULL
3405};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003406
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003407PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003408PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003409{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003410 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003411
Christian Heimes9cd17752007-11-18 19:35:23 +00003412 if (PyType_Ready(&PyNullImporter_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003413 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003414
Martin v. Löwis1a214512008-06-11 05:26:20 +00003415 m = PyModule_Create(&impmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003416 if (m == NULL)
3417 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003418 d = PyModule_GetDict(m);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003419 if (d == NULL)
3420 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003421
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003422 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3423 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3424 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3425 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3426 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3427 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3428 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3429 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003430 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003431 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003432
Christian Heimes9cd17752007-11-18 19:35:23 +00003433 Py_INCREF(&PyNullImporter_Type);
3434 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
Martin v. Löwis1a214512008-06-11 05:26:20 +00003435 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003436 failure:
Martin v. Löwis1a214512008-06-11 05:26:20 +00003437 Py_XDECREF(m);
3438 return NULL;
3439
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003440}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003441
3442
Guido van Rossumb18618d2000-05-03 23:44:39 +00003443/* API for embedding applications that want to add their own entries
3444 to the table of built-in modules. This should normally be called
3445 *before* Py_Initialize(). When the table resize fails, -1 is
3446 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003447
3448 After a similar function by Just van Rossum. */
3449
3450int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003451PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003452{
3453 static struct _inittab *our_copy = NULL;
3454 struct _inittab *p;
3455 int i, n;
3456
3457 /* Count the number of entries in both tables */
3458 for (n = 0; newtab[n].name != NULL; n++)
3459 ;
3460 if (n == 0)
3461 return 0; /* Nothing to do */
3462 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3463 ;
3464
3465 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003466 p = our_copy;
3467 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003468 if (p == NULL)
3469 return -1;
3470
3471 /* Copy the tables into the new memory */
3472 if (our_copy != PyImport_Inittab)
3473 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3474 PyImport_Inittab = our_copy = p;
3475 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3476
3477 return 0;
3478}
3479
3480/* Shorthand to add a single entry given a name and a function */
3481
3482int
Brett Cannona826f322009-04-02 03:41:46 +00003483PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003484{
3485 struct _inittab newtab[2];
3486
3487 memset(newtab, '\0', sizeof newtab);
3488
Brett Cannona826f322009-04-02 03:41:46 +00003489 newtab[0].name = (char *)name;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003490 newtab[0].initfunc = initfunc;
3491
3492 return PyImport_ExtendInittab(newtab);
3493}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003494
3495#ifdef __cplusplus
3496}
3497#endif