blob: 5a2c3cb038651a3a66192926e7112a217881ea4d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module definition and import implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Guido van Rossumd8faa362007-04-27 19:54:29 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Neal Norwitzadb69fc2005-12-17 20:54:49 +00008#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00009#include "pythonrun.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000011#include "marshal.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012#include "code.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000013#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000014#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000015#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000016#include "importdl.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000017
Guido van Rossum55a83382000-09-20 20:31:38 +000018#ifdef HAVE_FCNTL_H
19#include <fcntl.h>
20#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000021#ifdef __cplusplus
22extern "C" {
23#endif
Guido van Rossum55a83382000-09-20 20:31:38 +000024
Christian Heimesd3eb5a152008-02-24 00:38:49 +000025#ifdef MS_WINDOWS
26/* for stat.st_mode */
27typedef unsigned short mode_t;
28#endif
29
Guido van Rossum21d335e1993-10-15 13:01:11 +000030
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000031/* Magic word to reject .pyc files generated by other Python versions.
32 It should change for each incompatible change to the bytecode.
33
34 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000035 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000036 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000037
Guido van Rossum45aecf42006-03-15 04:58:47 +000038 The magic numbers must be spaced apart at least 2 values, as the
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000039 -U interpeter flag will cause MAGIC+1 being used. They have been
40 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000041
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000042 There were a variety of old schemes for setting the magic number.
43 The current working scheme is to increment the previous value by
44 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000045
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000046 Known values:
47 Python 1.5: 20121
48 Python 1.5.1: 20121
49 Python 1.5.2: 20121
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000050 Python 1.6: 50428
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000051 Python 2.0: 50823
52 Python 2.0.1: 50823
53 Python 2.1: 60202
54 Python 2.1.1: 60202
55 Python 2.1.2: 60202
56 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000057 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000058 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000059 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000060 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000061 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000062 Python 2.4b1: 62061
Michael W. Hudsondf888462005-06-03 14:41:55 +000063 Python 2.5a0: 62071
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000064 Python 2.5a0: 62081 (ast-branch)
Guido van Rossumc2e20742006-02-27 22:32:47 +000065 Python 2.5a0: 62091 (with)
Guido van Rossumf6694362006-03-10 02:28:35 +000066 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
68 Python 2.5b3: 62111 (fix wrong code: x += yield)
69 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
70 storing constants that should have been removed)
Thomas Wouters89f507f2006-12-13 04:49:30 +000071 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
Christian Heimes99170a52007-12-19 02:07:34 +000072 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000073 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
Guido van Rossum45aecf42006-03-15 04:58:47 +000074 Python 3000: 3000
Brett Cannone2e23ef2006-08-25 05:05:30 +000075 3010 (removed UNARY_CONVERT)
Guido van Rossum86e58e22006-08-28 15:27:34 +000076 3020 (added BUILD_SET)
Guido van Rossum4f72a782006-10-27 23:31:49 +000077 3030 (added keyword-only parameters)
Guido van Rossum3203bdc2006-12-28 21:03:31 +000078 3040 (added signature annotations)
Guido van Rossum452bf512007-02-09 05:32:43 +000079 3050 (print becomes a function)
Guido van Rossum52cc1d82007-03-18 15:41:51 +000080 3060 (PEP 3115 metaclass syntax)
Guido van Rossum00bc0e02007-10-15 02:52:41 +000081 3070 (PEP 3109 raise changes)
82 3080 (PEP 3137 make __file__ and __name__ unicode)
Guido van Rossum98297ee2007-11-06 21:34:58 +000083 3090 (kill str8 interning)
Christian Heimes99170a52007-12-19 02:07:34 +000084 3100 (merge from 2.6a0, see 62151)
Christian Heimes3b06e532008-01-07 20:12:44 +000085 3102 (__file__ points to source file)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000086 Python 3.0a4: 3110 (WITH_CLEANUP optimization).
Benjamin Petersoneec3d712008-06-11 15:59:43 +000087 Python 3.0a5: 3130 (lexical exception stacking, including POP_EXCEPT)
Antoine Pitrouf289ae62008-12-18 11:06:25 +000088 Python 3.1a0: 3140 (optimize list, set and dict comprehensions:
89 change LIST_APPEND and SET_ADD, add MAP_ADD)
Tim Peters36515e22001-11-18 04:06:29 +000090*/
Antoine Pitrouf289ae62008-12-18 11:06:25 +000091#define MAGIC (3140 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000092
Guido van Rossum96774c12000-05-01 20:19:08 +000093/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000094 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000095 compiler works which are enabled by command line switches. */
96static long pyc_magic = MAGIC;
97
Guido van Rossum25ce5661997-08-02 03:10:38 +000098/* See _PyImport_FixupExtension() below */
99static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000100
Guido van Rossum771c6c81997-10-31 18:37:24 +0000101/* This table is defined in config.c: */
102extern struct _inittab _PyImport_Inittab[];
103
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000104/* Method from Parser/tokenizer.c */
Guido van Rossum40d20bc2007-10-22 00:09:51 +0000105extern char * PyTokenizer_FindEncoding(int);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000106
Guido van Rossum771c6c81997-10-31 18:37:24 +0000107struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +0000108
Guido van Rossumed1170e1999-12-20 21:23:41 +0000109/* these tables define the module suffixes that Python recognizes */
110struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000111
Guido van Rossumed1170e1999-12-20 21:23:41 +0000112static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +0000113 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000114#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +0000115 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000116#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000117 {".pyc", "rb", PY_COMPILED},
118 {0, 0}
119};
120
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000121
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000122/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123
124void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000125_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000127 const struct filedescr *scan;
128 struct filedescr *filetab;
129 int countD = 0;
130 int countS = 0;
131
132 /* prepare _PyImport_Filetab: copy entries from
133 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
134 */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000135#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000136 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
137 ++countD;
Guido van Rossum04110fb2007-08-24 16:32:05 +0000138#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000139 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
140 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000141 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000142 if (filetab == NULL)
143 Py_FatalError("Can't initialize import file table.");
Guido van Rossum04110fb2007-08-24 16:32:05 +0000144#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000145 memcpy(filetab, _PyImport_DynLoadFiletab,
146 countD * sizeof(struct filedescr));
Guido van Rossum04110fb2007-08-24 16:32:05 +0000147#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000148 memcpy(filetab + countD, _PyImport_StandardFiletab,
149 countS * sizeof(struct filedescr));
150 filetab[countD + countS].suffix = NULL;
151
152 _PyImport_Filetab = filetab;
153
Guido van Rossum0824f631997-03-11 18:37:35 +0000154 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000155 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
156 for (; filetab->suffix != NULL; filetab++) {
157 if (strcmp(filetab->suffix, ".pyc") == 0)
158 filetab->suffix = ".pyo";
Guido van Rossum0824f631997-03-11 18:37:35 +0000159 }
160 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000161
Guido van Rossum572dbf82007-04-27 23:53:51 +0000162 {
Guido van Rossum96774c12000-05-01 20:19:08 +0000163 /* Fix the pyc_magic so that byte compiled code created
164 using the all-Unicode method doesn't interfere with
165 code created in normal operation mode. */
166 pyc_magic = MAGIC + 1;
167 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168}
169
Guido van Rossum25ce5661997-08-02 03:10:38 +0000170void
Just van Rossum52e14d62002-12-30 22:08:05 +0000171_PyImportHooks_Init(void)
172{
173 PyObject *v, *path_hooks = NULL, *zimpimport;
174 int err = 0;
175
176 /* adding sys.path_hooks and sys.path_importer_cache, setting up
177 zipimport */
Christian Heimes9cd17752007-11-18 19:35:23 +0000178 if (PyType_Ready(&PyNullImporter_Type) < 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000179 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000180
181 if (Py_VerboseFlag)
182 PySys_WriteStderr("# installing zipimport hook\n");
183
184 v = PyList_New(0);
185 if (v == NULL)
186 goto error;
187 err = PySys_SetObject("meta_path", v);
188 Py_DECREF(v);
189 if (err)
190 goto error;
191 v = PyDict_New();
192 if (v == NULL)
193 goto error;
194 err = PySys_SetObject("path_importer_cache", v);
195 Py_DECREF(v);
196 if (err)
197 goto error;
198 path_hooks = PyList_New(0);
199 if (path_hooks == NULL)
200 goto error;
201 err = PySys_SetObject("path_hooks", path_hooks);
202 if (err) {
203 error:
204 PyErr_Print();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000205 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
206 "path_importer_cache, or NullImporter failed"
207 );
Just van Rossum52e14d62002-12-30 22:08:05 +0000208 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000209
Just van Rossum52e14d62002-12-30 22:08:05 +0000210 zimpimport = PyImport_ImportModule("zipimport");
211 if (zimpimport == NULL) {
212 PyErr_Clear(); /* No zip import module -- okay */
213 if (Py_VerboseFlag)
214 PySys_WriteStderr("# can't import zipimport\n");
215 }
216 else {
217 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
218 "zipimporter");
219 Py_DECREF(zimpimport);
220 if (zipimporter == NULL) {
221 PyErr_Clear(); /* No zipimporter object -- okay */
222 if (Py_VerboseFlag)
223 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000224 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000225 }
226 else {
227 /* sys.path_hooks.append(zipimporter) */
228 err = PyList_Append(path_hooks, zipimporter);
229 Py_DECREF(zipimporter);
230 if (err)
231 goto error;
232 if (Py_VerboseFlag)
233 PySys_WriteStderr(
234 "# installed zipimport hook\n");
235 }
236 }
237 Py_DECREF(path_hooks);
238}
239
240void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242{
243 Py_XDECREF(extensions);
244 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000245 PyMem_DEL(_PyImport_Filetab);
246 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000247}
248
249
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000250/* Locking primitives to prevent parallel imports of the same module
251 in different threads to return with a partially loaded module.
252 These calls are serialized by the global interpreter lock. */
253
254#ifdef WITH_THREAD
255
Guido van Rossum49b56061998-10-01 20:42:43 +0000256#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000257
Guido van Rossum65d5b571998-12-21 19:32:43 +0000258static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000259static long import_lock_thread = -1;
260static int import_lock_level = 0;
261
262static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000264{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000265 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000266 if (me == -1)
267 return; /* Too bad */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000268 if (import_lock == NULL) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000269 import_lock = PyThread_allocate_lock();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000270 if (import_lock == NULL)
271 return; /* Nothing much we can do. */
272 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000273 if (import_lock_thread == me) {
274 import_lock_level++;
275 return;
276 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000277 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
278 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000279 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000280 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000281 PyEval_RestoreThread(tstate);
282 }
283 import_lock_thread = me;
284 import_lock_level = 1;
285}
286
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000287static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000288unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000289{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000290 long me = PyThread_get_thread_ident();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000291 if (me == -1 || import_lock == NULL)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000292 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000293 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000294 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000295 import_lock_level--;
296 if (import_lock_level == 0) {
297 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000298 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000299 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000300 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000301}
302
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000303/* This function is called from PyOS_AfterFork to ensure that newly
304 created child processes do not share locks with the parent. */
305
306void
307_PyImport_ReInitLock(void)
308{
309#ifdef _AIX
310 if (import_lock != NULL)
311 import_lock = PyThread_allocate_lock();
312#endif
313}
314
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000315#else
316
317#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000318#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000319
320#endif
321
Tim Peters69232342001-08-30 05:16:13 +0000322static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000323imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000324{
Tim Peters69232342001-08-30 05:16:13 +0000325#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000326 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000327#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000328 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000329#endif
330}
331
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000332static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000333imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000334{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000335#ifdef WITH_THREAD
336 lock_import();
337#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000338 Py_INCREF(Py_None);
339 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000340}
341
342static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000343imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000344{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000345#ifdef WITH_THREAD
346 if (unlock_import() < 0) {
347 PyErr_SetString(PyExc_RuntimeError,
348 "not holding the import lock");
349 return NULL;
350 }
351#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000352 Py_INCREF(Py_None);
353 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000354}
355
Guido van Rossumd8faa362007-04-27 19:54:29 +0000356static void
357imp_modules_reloading_clear(void)
358{
359 PyInterpreterState *interp = PyThreadState_Get()->interp;
360 if (interp->modules_reloading != NULL)
361 PyDict_Clear(interp->modules_reloading);
362}
363
Guido van Rossum25ce5661997-08-02 03:10:38 +0000364/* Helper for sys */
365
366PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000367PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000368{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000369 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000370 if (interp->modules == NULL)
371 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
372 return interp->modules;
373}
374
Guido van Rossum3f5da241990-12-20 15:06:42 +0000375
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000376/* List of names to clear in sys */
377static char* sys_deletes[] = {
Collin Winter670e6922007-03-21 02:57:17 +0000378 "path", "argv", "ps1", "ps2",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000379 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000380 "path_hooks", "path_importer_cache", "meta_path",
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000381 /* misc stuff */
382 "flags", "float_info",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000383 NULL
384};
385
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000386static char* sys_files[] = {
387 "stdin", "__stdin__",
388 "stdout", "__stdout__",
389 "stderr", "__stderr__",
390 NULL
391};
392
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000393
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000394/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000395
Guido van Rossum3f5da241990-12-20 15:06:42 +0000396void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000397PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000398{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000399 Py_ssize_t pos, ndone;
Guido van Rossum758eec01998-01-19 21:58:26 +0000400 char *name;
401 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000402 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000403 PyObject *modules = interp->modules;
404
405 if (modules == NULL)
406 return; /* Already done */
407
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000408 /* Delete some special variables first. These are common
409 places where user values hide and people complain when their
410 destructors fail. Since the modules containing them are
411 deleted *last* of all, they would come too late in the normal
412 destruction order. Sigh. */
413
Georg Brandl1a3284e2007-12-02 09:40:06 +0000414 value = PyDict_GetItemString(modules, "builtins");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000415 if (value != NULL && PyModule_Check(value)) {
416 dict = PyModule_GetDict(value);
417 if (Py_VerboseFlag)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000418 PySys_WriteStderr("# clear builtins._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000419 PyDict_SetItemString(dict, "_", Py_None);
420 }
421 value = PyDict_GetItemString(modules, "sys");
422 if (value != NULL && PyModule_Check(value)) {
423 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000424 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000425 dict = PyModule_GetDict(value);
426 for (p = sys_deletes; *p != NULL; p++) {
427 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000428 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000429 PyDict_SetItemString(dict, *p, Py_None);
430 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000431 for (p = sys_files; *p != NULL; p+=2) {
432 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000433 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000434 v = PyDict_GetItemString(dict, *(p+1));
435 if (v == NULL)
436 v = Py_None;
437 PyDict_SetItemString(dict, *p, v);
438 }
439 }
440
441 /* First, delete __main__ */
442 value = PyDict_GetItemString(modules, "__main__");
443 if (value != NULL && PyModule_Check(value)) {
444 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000445 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000446 _PyModule_Clear(value);
447 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000448 }
449
Georg Brandl1a3284e2007-12-02 09:40:06 +0000450 /* The special treatment of "builtins" here is because even
Guido van Rossum758eec01998-01-19 21:58:26 +0000451 when it's not referenced as a module, its dictionary is
452 referenced by almost every module's __builtins__. Since
453 deleting a module clears its dictionary (even if there are
Georg Brandl1a3284e2007-12-02 09:40:06 +0000454 references left to it), we need to delete the "builtins"
Guido van Rossum758eec01998-01-19 21:58:26 +0000455 module last. Likewise, we don't delete sys until the very
456 end because it is implicitly referenced (e.g. by print).
457
458 Also note that we 'delete' modules by replacing their entry
459 in the modules dict with None, rather than really deleting
460 them; this avoids a rehash of the modules dictionary and
461 also marks them as "non existent" so they won't be
462 re-imported. */
463
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000464 /* Next, repeatedly delete modules with a reference count of
Georg Brandl1a3284e2007-12-02 09:40:06 +0000465 one (skipping builtins and sys) and delete them */
Guido van Rossum758eec01998-01-19 21:58:26 +0000466 do {
467 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000468 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000469 while (PyDict_Next(modules, &pos, &key, &value)) {
470 if (value->ob_refcnt != 1)
471 continue;
Neal Norwitz80e7f272007-08-26 06:45:23 +0000472 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000473 name = _PyUnicode_AsString(key);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000474 if (strcmp(name, "builtins") == 0)
Guido van Rossum758eec01998-01-19 21:58:26 +0000475 continue;
476 if (strcmp(name, "sys") == 0)
477 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000478 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000479 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000480 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000481 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000482 PyDict_SetItem(modules, key, Py_None);
483 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000484 }
485 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000486 } while (ndone > 0);
487
Georg Brandl1a3284e2007-12-02 09:40:06 +0000488 /* Next, delete all modules (still skipping builtins and sys) */
Guido van Rossum758eec01998-01-19 21:58:26 +0000489 pos = 0;
490 while (PyDict_Next(modules, &pos, &key, &value)) {
Neal Norwitz80e7f272007-08-26 06:45:23 +0000491 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000492 name = _PyUnicode_AsString(key);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000493 if (strcmp(name, "builtins") == 0)
Guido van Rossum758eec01998-01-19 21:58:26 +0000494 continue;
495 if (strcmp(name, "sys") == 0)
496 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000497 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000498 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000499 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000500 PyDict_SetItem(modules, key, Py_None);
501 }
502 }
503
Georg Brandl1a3284e2007-12-02 09:40:06 +0000504 /* Next, delete sys and builtins (in that order) */
Guido van Rossum758eec01998-01-19 21:58:26 +0000505 value = PyDict_GetItemString(modules, "sys");
506 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000507 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000508 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000509 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000510 PyDict_SetItemString(modules, "sys", Py_None);
511 }
Georg Brandl1a3284e2007-12-02 09:40:06 +0000512 value = PyDict_GetItemString(modules, "builtins");
Guido van Rossum758eec01998-01-19 21:58:26 +0000513 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000514 if (Py_VerboseFlag)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000515 PySys_WriteStderr("# cleanup builtins\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000516 _PyModule_Clear(value);
Georg Brandl1a3284e2007-12-02 09:40:06 +0000517 PyDict_SetItemString(modules, "builtins", Py_None);
Guido van Rossum758eec01998-01-19 21:58:26 +0000518 }
519
520 /* Finally, clear and delete the modules directory */
521 PyDict_Clear(modules);
522 interp->modules = NULL;
523 Py_DECREF(modules);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000524 Py_CLEAR(interp->modules_reloading);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000525}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000526
527
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000528/* Helper for pythonrun.c -- return magic number */
529
530long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000531PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000532{
Guido van Rossum96774c12000-05-01 20:19:08 +0000533 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000534}
535
536
Guido van Rossum25ce5661997-08-02 03:10:38 +0000537/* Magic for extension modules (built-in as well as dynamically
538 loaded). To prevent initializing an extension module more than
539 once, we keep a static dictionary 'extensions' keyed by module name
540 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000541 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000542 dictionary is stored by calling _PyImport_FixupExtension()
543 immediately after the module initialization function succeeds. A
544 copy can be retrieved from there by calling
Martin v. Löwis1a214512008-06-11 05:26:20 +0000545 _PyImport_FindExtension().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000546
Martin v. Löwis1a214512008-06-11 05:26:20 +0000547 Modules which do support multiple multiple initialization set
548 their m_size field to a non-negative number (indicating the size
549 of the module-specific state). They are still recorded in the
550 extensions dictionary, to avoid loading shared libraries twice.
551*/
552
553int
554_PyImport_FixupExtension(PyObject *mod, char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000555{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000556 PyObject *modules, *dict;
557 struct PyModuleDef *def;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000558 if (extensions == NULL) {
559 extensions = PyDict_New();
560 if (extensions == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000561 return -1;
562 }
563 if (mod == NULL || !PyModule_Check(mod)) {
564 PyErr_BadInternalCall();
565 return -1;
566 }
567 def = PyModule_GetDef(mod);
568 if (!def) {
569 PyErr_BadInternalCall();
570 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000571 }
572 modules = PyImport_GetModuleDict();
Martin v. Löwis1a214512008-06-11 05:26:20 +0000573 if (PyDict_SetItemString(modules, name, mod) < 0)
574 return -1;
575 if (_PyState_AddModule(mod, def) < 0) {
576 PyDict_DelItemString(modules, name);
577 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000578 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000579 if (def->m_size == -1) {
580 if (def->m_base.m_copy) {
581 /* Somebody already imported the module,
582 likely under a different name.
583 XXX this should really not happen. */
584 Py_DECREF(def->m_base.m_copy);
585 def->m_base.m_copy = NULL;
586 }
587 dict = PyModule_GetDict(mod);
588 if (dict == NULL)
589 return -1;
590 def->m_base.m_copy = PyDict_Copy(dict);
591 if (def->m_base.m_copy == NULL)
592 return -1;
593 }
594 PyDict_SetItemString(extensions, filename, (PyObject*)def);
595 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000596}
597
598PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000599_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000600{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000601 PyObject *mod, *mdict;
602 PyModuleDef* def;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000603 if (extensions == NULL)
604 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000605 def = (PyModuleDef*)PyDict_GetItemString(extensions, filename);
606 if (def == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000607 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000608 if (def->m_size == -1) {
609 /* Module does not support repeated initialization */
610 if (def->m_base.m_copy == NULL)
611 return NULL;
612 mod = PyImport_AddModule(name);
613 if (mod == NULL)
614 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000615 mdict = PyModule_GetDict(mod);
616 if (mdict == NULL)
617 return NULL;
618 if (PyDict_Update(mdict, def->m_base.m_copy))
619 return NULL;
620 }
621 else {
622 if (def->m_base.m_init == NULL)
623 return NULL;
624 mod = def->m_base.m_init();
625 if (mod == NULL)
626 return NULL;
627 PyDict_SetItemString(PyImport_GetModuleDict(), name, mod);
Benjamin Petersonad956532008-09-04 02:28:15 +0000628 Py_DECREF(mod);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000629 }
630 if (_PyState_AddModule(mod, def) < 0) {
631 PyDict_DelItemString(PyImport_GetModuleDict(), name);
632 Py_DECREF(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000633 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000634 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000635 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000636 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Martin v. Löwis1a214512008-06-11 05:26:20 +0000637 name, filename);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000638 return mod;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000639
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000640}
641
642
643/* Get the module object corresponding to a module name.
644 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000645 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000646 Because the former action is most common, THIS DOES NOT RETURN A
647 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000650PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000652 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000653 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000654
Guido van Rossum25ce5661997-08-02 03:10:38 +0000655 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000656 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000657 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000658 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000659 if (m == NULL)
660 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000661 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000663 return NULL;
664 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000666
667 return m;
668}
669
Tim Peters1cd70172004-08-02 03:52:12 +0000670/* Remove name from sys.modules, if it's there. */
671static void
672_RemoveModule(const char *name)
673{
674 PyObject *modules = PyImport_GetModuleDict();
675 if (PyDict_GetItemString(modules, name) == NULL)
676 return;
677 if (PyDict_DelItemString(modules, name) < 0)
678 Py_FatalError("import: deleting existing key in"
679 "sys.modules failed");
680}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000681
Christian Heimes3b06e532008-01-07 20:12:44 +0000682static PyObject * get_sourcefile(const char *file);
683
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000684/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000685 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
686 * removed from sys.modules, to avoid leaving damaged module objects
687 * in sys.modules. The caller may wish to restore the original
688 * module object (if any) in this case; PyImport_ReloadModule is an
689 * example.
690 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000691PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000692PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000693{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000694 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
695}
696
697PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000698PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000699{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000700 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000701 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000702
Guido van Rossum79f25d91997-04-29 20:08:16 +0000703 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000704 if (m == NULL)
705 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000706 /* If the module is being reloaded, we get the old module back
707 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000708 d = PyModule_GetDict(m);
709 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
710 if (PyDict_SetItemString(d, "__builtins__",
711 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000712 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000713 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000714 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000715 v = NULL;
716 if (pathname != NULL) {
Christian Heimes3b06e532008-01-07 20:12:44 +0000717 v = get_sourcefile(pathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000718 if (v == NULL)
719 PyErr_Clear();
720 }
721 if (v == NULL) {
722 v = ((PyCodeObject *)co)->co_filename;
723 Py_INCREF(v);
724 }
725 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000726 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000727 Py_DECREF(v);
728
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000729 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000730 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000731 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000733
Guido van Rossum25ce5661997-08-02 03:10:38 +0000734 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000735 PyErr_Format(PyExc_ImportError,
736 "Loaded module %.200s not found in sys.modules",
737 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000738 return NULL;
739 }
740
Guido van Rossum79f25d91997-04-29 20:08:16 +0000741 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000742
743 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000744
745 error:
746 _RemoveModule(name);
747 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000748}
749
750
751/* Given a pathname for a Python source file, fill a buffer with the
752 pathname for the corresponding compiled file. Return the pathname
753 for the compiled file, or NULL if there's no space in the buffer.
754 Doesn't set an exception. */
755
756static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000757make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000758{
Tim Petersc1731372001-08-04 08:12:36 +0000759 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000760 if (len+2 > buflen)
761 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000762
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000763#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000764 /* Treat .pyw as if it were .py. The case of ".pyw" must match
765 that used in _PyImport_StandardFiletab. */
766 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
767 --len; /* pretend 'w' isn't there */
768#endif
769 memcpy(buf, pathname, len);
770 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
771 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000772
773 return buf;
774}
775
776
777/* Given a pathname for a Python source file, its time of last
778 modification, and a pathname for a compiled file, check whether the
779 compiled file represents the same version of the source. If so,
780 return a FILE pointer for the compiled file, positioned just after
781 the header; if not, return NULL.
782 Doesn't set an exception. */
783
784static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000785check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000786{
787 FILE *fp;
788 long magic;
789 long pyc_mtime;
790
791 fp = fopen(cpathname, "rb");
792 if (fp == NULL)
793 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000795 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000796 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000797 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000798 fclose(fp);
799 return NULL;
800 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000801 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000802 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000803 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000804 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000805 fclose(fp);
806 return NULL;
807 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000809 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000810 return fp;
811}
812
813
814/* Read a code object from a file and check it for validity */
815
Guido van Rossum79f25d91997-04-29 20:08:16 +0000816static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000817read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000818{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000819 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000820
Tim Petersd9b9ac82001-01-28 00:27:39 +0000821 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000822 if (co == NULL)
823 return NULL;
824 if (!PyCode_Check(co)) {
825 PyErr_Format(PyExc_ImportError,
826 "Non-code object in %.200s", cpathname);
827 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000828 return NULL;
829 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000831}
832
833
834/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000835 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000836
Guido van Rossum79f25d91997-04-29 20:08:16 +0000837static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000838load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000839{
840 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000841 PyCodeObject *co;
842 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000843
Guido van Rossum79f25d91997-04-29 20:08:16 +0000844 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000845 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000846 PyErr_Format(PyExc_ImportError,
847 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000848 return NULL;
849 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000850 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000851 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000852 if (co == NULL)
853 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000855 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000856 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000857 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000858 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000859
860 return m;
861}
862
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000863/* Parse a source file and return the corresponding code object */
864
Guido van Rossum79f25d91997-04-29 20:08:16 +0000865static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000867{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 PyCodeObject *co = NULL;
869 mod_ty mod;
Christian Heimes4d6ec852008-03-26 22:34:47 +0000870 PyCompilerFlags flags;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000871 PyArena *arena = PyArena_New();
872 if (arena == NULL)
873 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000874
Christian Heimesb1b3efc2008-03-26 23:24:27 +0000875 flags.cf_flags = 0;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000876 mod = PyParser_ASTFromFile(fp, pathname, NULL,
Christian Heimes4d6ec852008-03-26 22:34:47 +0000877 Py_file_input, 0, 0, &flags,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000878 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000880 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000882 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000883 return co;
884}
885
886
Guido van Rossum55a83382000-09-20 20:31:38 +0000887/* Helper to open a bytecode file for writing in exclusive mode */
888
889static FILE *
Christian Heimes05e8be12008-02-23 18:30:17 +0000890open_exclusive(char *filename, mode_t mode)
Guido van Rossum55a83382000-09-20 20:31:38 +0000891{
892#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
893 /* Use O_EXCL to avoid a race condition when another process tries to
894 write the same file. When that happens, our open() call fails,
895 which is just fine (since it's only a cache).
896 XXX If the file exists and is writable but the directory is not
897 writable, the file will never be written. Oh well.
898 */
899 int fd;
900 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000901 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
902#ifdef O_BINARY
903 |O_BINARY /* necessary for Windows */
904#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000905#ifdef __VMS
Christian Heimes05e8be12008-02-23 18:30:17 +0000906 , mode, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000907#else
Christian Heimes05e8be12008-02-23 18:30:17 +0000908 , mode
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000909#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000910 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000911 if (fd < 0)
912 return NULL;
913 return fdopen(fd, "wb");
914#else
915 /* Best we can do -- on Windows this can't happen anyway */
916 return fopen(filename, "wb");
917#endif
918}
919
920
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000921/* Write a compiled module to a file, placing the time of last
922 modification of its source into the header.
923 Errors are ignored, if a write error occurs an attempt is made to
924 remove the file. */
925
926static void
Christian Heimes05e8be12008-02-23 18:30:17 +0000927write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000928{
929 FILE *fp;
Christian Heimes05e8be12008-02-23 18:30:17 +0000930 time_t mtime = srcstat->st_mtime;
931 mode_t mode = srcstat->st_mode;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000932
Christian Heimes05e8be12008-02-23 18:30:17 +0000933 fp = open_exclusive(cpathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000934 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000935 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000936 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000937 "# can't create %s\n", cpathname);
938 return;
939 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000940 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000941 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000942 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
943 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000944 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000945 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000946 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000947 /* Don't keep partial file */
948 fclose(fp);
949 (void) unlink(cpathname);
950 return;
951 }
952 /* Now write the true mtime */
953 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000954 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000955 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000956 fflush(fp);
957 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000958 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000959 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000960}
961
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000962static void
963update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
964{
965 PyObject *constants, *tmp;
966 Py_ssize_t i, n;
967
968 if (PyUnicode_Compare(co->co_filename, oldname))
969 return;
970
971 tmp = co->co_filename;
972 co->co_filename = newname;
973 Py_INCREF(co->co_filename);
974 Py_DECREF(tmp);
975
976 constants = co->co_consts;
977 n = PyTuple_GET_SIZE(constants);
978 for (i = 0; i < n; i++) {
979 tmp = PyTuple_GET_ITEM(constants, i);
980 if (PyCode_Check(tmp))
981 update_code_filenames((PyCodeObject *)tmp,
982 oldname, newname);
983 }
984}
985
986static int
987update_compiled_module(PyCodeObject *co, char *pathname)
988{
989 PyObject *oldname, *newname;
990
991 if (!PyUnicode_CompareWithASCIIString(co->co_filename, pathname))
992 return 0;
993
994 newname = PyUnicode_FromString(pathname);
995 if (newname == NULL)
996 return -1;
997
998 oldname = co->co_filename;
999 Py_INCREF(oldname);
1000 update_code_filenames(co, oldname, newname);
1001 Py_DECREF(oldname);
1002 Py_DECREF(newname);
1003 return 1;
1004}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001005
1006/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001007 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
1008 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001009
Guido van Rossum79f25d91997-04-29 20:08:16 +00001010static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001011load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001012{
Christian Heimes05e8be12008-02-23 18:30:17 +00001013 struct stat st;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001014 FILE *fpc;
1015 char buf[MAXPATHLEN+1];
1016 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001017 PyCodeObject *co;
1018 PyObject *m;
Christian Heimes05e8be12008-02-23 18:30:17 +00001019
1020 if (fstat(fileno(fp), &st) != 0) {
Neal Norwitz708e51a2005-10-03 04:48:15 +00001021 PyErr_Format(PyExc_RuntimeError,
Christian Heimes05e8be12008-02-23 18:30:17 +00001022 "unable to get file status from '%s'",
Neal Norwitz708e51a2005-10-03 04:48:15 +00001023 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +00001024 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +00001025 }
Fred Drake4c82b232000-06-30 16:18:57 +00001026#if SIZEOF_TIME_T > 4
1027 /* Python's .pyc timestamp handling presumes that the timestamp fits
1028 in 4 bytes. This will be fine until sometime in the year 2038,
1029 when a 4-byte signed time_t will overflow.
1030 */
Christian Heimes05e8be12008-02-23 18:30:17 +00001031 if (st.st_mtime >> 32) {
Fred Drake4c82b232000-06-30 16:18:57 +00001032 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +00001033 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +00001034 return NULL;
1035 }
1036#endif
Tim Peters36515e22001-11-18 04:06:29 +00001037 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +00001038 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001039 if (cpathname != NULL &&
Christian Heimes05e8be12008-02-23 18:30:17 +00001040 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001041 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001042 fclose(fpc);
1043 if (co == NULL)
1044 return NULL;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001045 if (update_compiled_module(co, pathname) < 0)
1046 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001047 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001048 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001049 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001050 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001051 }
1052 else {
1053 co = parse_source_module(pathname, fp);
1054 if (co == NULL)
1055 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001056 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001057 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001058 name, pathname);
Christian Heimes790c8232008-01-07 21:14:23 +00001059 if (cpathname) {
1060 PyObject *ro = PySys_GetObject("dont_write_bytecode");
1061 if (ro == NULL || !PyObject_IsTrue(ro))
Christian Heimes05e8be12008-02-23 18:30:17 +00001062 write_compiled_module(co, cpathname, &st);
Christian Heimes790c8232008-01-07 21:14:23 +00001063 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001064 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001065 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001066 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001067
1068 return m;
1069}
1070
Christian Heimes3b06e532008-01-07 20:12:44 +00001071/* Get source file -> unicode or None
1072 * Returns the path to the py file if available, else the given path
1073 */
1074static PyObject *
1075get_sourcefile(const char *file)
1076{
1077 char py[MAXPATHLEN + 1];
1078 Py_ssize_t len;
1079 PyObject *u;
1080 struct stat statbuf;
1081
1082 if (!file || !*file) {
1083 Py_RETURN_NONE;
1084 }
1085
1086 len = strlen(file);
Christian Heimes3540b502008-01-11 07:03:05 +00001087 /* match '*.py?' */
1088 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {
Christian Heimes3b06e532008-01-07 20:12:44 +00001089 return PyUnicode_DecodeFSDefault(file);
1090 }
1091
1092 strncpy(py, file, len-1);
Martin v. Löwis5021ebc2008-03-22 22:07:13 +00001093 py[len-1] = '\0';
Christian Heimes3b06e532008-01-07 20:12:44 +00001094 if (stat(py, &statbuf) == 0 &&
1095 S_ISREG(statbuf.st_mode)) {
1096 u = PyUnicode_DecodeFSDefault(py);
1097 }
1098 else {
1099 u = PyUnicode_DecodeFSDefault(file);
1100 }
1101 return u;
1102}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001103
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001104/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001105static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
1106static struct filedescr *find_module(char *, char *, PyObject *,
1107 char *, size_t, FILE **, PyObject **);
Christian Heimes3b06e532008-01-07 20:12:44 +00001108static struct _frozen * find_frozen(char *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001109
1110/* Load a package and return its module object WITH INCREMENTED
1111 REFERENCE COUNT */
1112
1113static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001114load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001115{
Tim Peters1cd70172004-08-02 03:52:12 +00001116 PyObject *m, *d;
1117 PyObject *file = NULL;
1118 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001119 int err;
1120 char buf[MAXPATHLEN+1];
1121 FILE *fp = NULL;
1122 struct filedescr *fdp;
1123
1124 m = PyImport_AddModule(name);
1125 if (m == NULL)
1126 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001127 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001128 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001129 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001130 d = PyModule_GetDict(m);
Christian Heimes3b06e532008-01-07 20:12:44 +00001131 file = get_sourcefile(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001132 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +00001133 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001134 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +00001135 if (path == NULL)
1136 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001137 err = PyDict_SetItemString(d, "__file__", file);
1138 if (err == 0)
1139 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +00001140 if (err != 0)
1141 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001142 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001143 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001144 if (fdp == NULL) {
1145 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1146 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +00001147 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001148 }
1149 else
1150 m = NULL;
1151 goto cleanup;
1152 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001153 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001154 if (fp != NULL)
1155 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001156 goto cleanup;
1157
1158 error:
1159 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001160 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001161 Py_XDECREF(path);
1162 Py_XDECREF(file);
1163 return m;
1164}
1165
1166
1167/* Helper to test for built-in module */
1168
1169static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001170is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001171{
1172 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001173 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1174 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1175 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001176 return -1;
1177 else
1178 return 1;
1179 }
1180 }
1181 return 0;
1182}
1183
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001184
Just van Rossum52e14d62002-12-30 22:08:05 +00001185/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1186 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001187 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001188 that can handle the path item. Return None if no hook could;
1189 this tells our caller it should fall back to the builtin
1190 import mechanism. Cache the result in path_importer_cache.
1191 Returns a borrowed reference. */
1192
1193static PyObject *
1194get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1195 PyObject *p)
1196{
1197 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001198 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001199
1200 /* These conditions are the caller's responsibility: */
1201 assert(PyList_Check(path_hooks));
1202 assert(PyDict_Check(path_importer_cache));
1203
1204 nhooks = PyList_Size(path_hooks);
1205 if (nhooks < 0)
1206 return NULL; /* Shouldn't happen */
1207
1208 importer = PyDict_GetItem(path_importer_cache, p);
1209 if (importer != NULL)
1210 return importer;
1211
1212 /* set path_importer_cache[p] to None to avoid recursion */
1213 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1214 return NULL;
1215
1216 for (j = 0; j < nhooks; j++) {
1217 PyObject *hook = PyList_GetItem(path_hooks, j);
1218 if (hook == NULL)
1219 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001220 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001221 if (importer != NULL)
1222 break;
1223
1224 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1225 return NULL;
1226 }
1227 PyErr_Clear();
1228 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001229 if (importer == NULL) {
1230 importer = PyObject_CallFunctionObjArgs(
Christian Heimes9cd17752007-11-18 19:35:23 +00001231 (PyObject *)&PyNullImporter_Type, p, NULL
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001232 );
1233 if (importer == NULL) {
1234 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1235 PyErr_Clear();
1236 return Py_None;
1237 }
1238 }
1239 }
1240 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001241 int err = PyDict_SetItem(path_importer_cache, p, importer);
1242 Py_DECREF(importer);
1243 if (err != 0)
1244 return NULL;
1245 }
1246 return importer;
1247}
1248
Christian Heimes9cd17752007-11-18 19:35:23 +00001249PyAPI_FUNC(PyObject *)
1250PyImport_GetImporter(PyObject *path) {
1251 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
1252
1253 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1254 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1255 importer = get_path_importer(path_importer_cache,
1256 path_hooks, path);
1257 }
1258 }
1259 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1260 return importer;
1261}
1262
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001263/* Search the path (default sys.path) for a module. Return the
1264 corresponding filedescr struct, and (via return arguments) the
1265 pathname and an open file. Return NULL if the module is not found. */
1266
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001267#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001268extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001269 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001270#endif
1271
Martin v. Löwis18e16552006-02-15 17:27:45 +00001272static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001273static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001274static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001275
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001276static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001277find_module(char *fullname, char *subname, PyObject *path, char *buf,
1278 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001279{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001280 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001281 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001282 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001283 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001284 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001285 PyObject *path_hooks, *path_importer_cache;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001286 struct stat statbuf;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001287 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1288 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1289 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001290 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001291#if defined(PYOS_OS2)
1292 size_t saved_len;
1293 size_t saved_namelen;
1294 char *saved_buf = NULL;
1295#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001296 if (p_loader != NULL)
1297 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001298
Just van Rossum52e14d62002-12-30 22:08:05 +00001299 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001300 PyErr_SetString(PyExc_OverflowError,
1301 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001302 return NULL;
1303 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001304 strcpy(name, subname);
1305
1306 /* sys.meta_path import hook */
1307 if (p_loader != NULL) {
1308 PyObject *meta_path;
1309
1310 meta_path = PySys_GetObject("meta_path");
1311 if (meta_path == NULL || !PyList_Check(meta_path)) {
1312 PyErr_SetString(PyExc_ImportError,
1313 "sys.meta_path must be a list of "
1314 "import hooks");
1315 return NULL;
1316 }
1317 Py_INCREF(meta_path); /* zap guard */
1318 npath = PyList_Size(meta_path);
1319 for (i = 0; i < npath; i++) {
1320 PyObject *loader;
1321 PyObject *hook = PyList_GetItem(meta_path, i);
1322 loader = PyObject_CallMethod(hook, "find_module",
1323 "sO", fullname,
1324 path != NULL ?
1325 path : Py_None);
1326 if (loader == NULL) {
1327 Py_DECREF(meta_path);
1328 return NULL; /* true error */
1329 }
1330 if (loader != Py_None) {
1331 /* a loader was found */
1332 *p_loader = loader;
1333 Py_DECREF(meta_path);
1334 return &importhookdescr;
1335 }
1336 Py_DECREF(loader);
1337 }
1338 Py_DECREF(meta_path);
1339 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001340
Benjamin Petersond968e272008-11-05 22:48:33 +00001341 if (find_frozen(fullname) != NULL) {
1342 strcpy(buf, fullname);
1343 return &fd_frozen;
Guido van Rossum0506a431998-08-11 15:07:39 +00001344 }
Benjamin Petersond968e272008-11-05 22:48:33 +00001345
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001346 if (path == NULL) {
1347 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001348 strcpy(buf, name);
1349 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001350 }
Guido van Rossumac279101996-08-22 23:10:58 +00001351#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001352 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1353 if (fp != NULL) {
1354 *p_fp = fp;
1355 return fdp;
1356 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001357#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001358 path = PySys_GetObject("path");
1359 }
Benjamin Petersond968e272008-11-05 22:48:33 +00001360
Guido van Rossum79f25d91997-04-29 20:08:16 +00001361 if (path == NULL || !PyList_Check(path)) {
1362 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001363 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001364 return NULL;
1365 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001366
1367 path_hooks = PySys_GetObject("path_hooks");
1368 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1369 PyErr_SetString(PyExc_ImportError,
1370 "sys.path_hooks must be a list of "
1371 "import hooks");
1372 return NULL;
1373 }
1374 path_importer_cache = PySys_GetObject("path_importer_cache");
1375 if (path_importer_cache == NULL ||
1376 !PyDict_Check(path_importer_cache)) {
1377 PyErr_SetString(PyExc_ImportError,
1378 "sys.path_importer_cache must be a dict");
1379 return NULL;
1380 }
1381
Guido van Rossum79f25d91997-04-29 20:08:16 +00001382 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001383 namelen = strlen(name);
1384 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001385 PyObject *v = PyList_GetItem(path, i);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001386 PyObject *origv = v;
Guido van Rossum6262cc72007-05-09 23:29:27 +00001387 const char *base;
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001388 Py_ssize_t size;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001389 if (!v)
1390 return NULL;
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001391 if (PyUnicode_Check(v)) {
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001392 v = PyUnicode_AsEncodedString(v,
1393 Py_FileSystemDefaultEncoding, NULL);
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001394 if (v == NULL)
1395 return NULL;
1396 }
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001397 else if (!PyBytes_Check(v))
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +00001398 continue;
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001399 else
1400 Py_INCREF(v);
1401
Christian Heimes72b710a2008-05-26 13:28:38 +00001402 base = PyBytes_AS_STRING(v);
1403 size = PyBytes_GET_SIZE(v);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001404 len = size;
Walter Dörwald3430d702002-06-17 10:43:59 +00001405 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001406 Py_DECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001407 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001408 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001409 strcpy(buf, base);
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001410 Py_DECREF(v);
1411
Walter Dörwald3430d702002-06-17 10:43:59 +00001412 if (strlen(buf) != len) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001413 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001414 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001415
1416 /* sys.path_hooks import hook */
1417 if (p_loader != NULL) {
1418 PyObject *importer;
1419
1420 importer = get_path_importer(path_importer_cache,
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001421 path_hooks, origv);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001422 if (importer == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001423 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001424 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001425 /* Note: importer is a borrowed reference */
1426 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001427 PyObject *loader;
1428 loader = PyObject_CallMethod(importer,
1429 "find_module",
1430 "s", fullname);
1431 if (loader == NULL)
1432 return NULL; /* error */
1433 if (loader != Py_None) {
1434 /* a loader was found */
1435 *p_loader = loader;
1436 return &importhookdescr;
1437 }
1438 Py_DECREF(loader);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001439 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001440 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001441 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001442 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001443
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001444 if (len > 0 && buf[len-1] != SEP
1445#ifdef ALTSEP
1446 && buf[len-1] != ALTSEP
1447#endif
1448 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001449 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001450 strcpy(buf+len, name);
1451 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001452
1453 /* Check for package import (buf holds a directory name,
1454 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001455#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001456 if (stat(buf, &statbuf) == 0 && /* it exists */
1457 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001458 case_ok(buf, len, namelen, name)) { /* case matches */
1459 if (find_init_module(buf)) { /* and has __init__.py */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001460 return &fd_package;
1461 }
1462 else {
1463 char warnstr[MAXPATHLEN+80];
1464 sprintf(warnstr, "Not importing directory "
1465 "'%.*s': missing __init__.py",
1466 MAXPATHLEN, buf);
Skip Montanaro46fc3372007-08-12 11:44:53 +00001467 if (PyErr_WarnEx(PyExc_ImportWarning,
1468 warnstr, 1)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001469 return NULL;
1470 }
1471 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001472 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001473#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001474#if defined(PYOS_OS2)
1475 /* take a snapshot of the module spec for restoration
1476 * after the 8 character DLL hackery
1477 */
1478 saved_buf = strdup(buf);
1479 saved_len = len;
1480 saved_namelen = namelen;
1481#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001482 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum04110fb2007-08-24 16:32:05 +00001483#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001484 /* OS/2 limits DLLs to 8 character names (w/o
1485 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001486 * so if the name is longer than that and its a
1487 * dynamically loaded module we're going to try,
1488 * truncate the name before trying
1489 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001490 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001491 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001492 const struct filedescr *scan;
1493 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001494 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001495 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001496 break;
1497 else
1498 scan++;
1499 }
1500 if (scan->suffix != NULL) {
1501 /* yes, so truncate the name */
1502 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001503 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001504 buf[len] = '\0';
1505 }
1506 }
1507#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001508 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001509 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001510 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001511 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001512 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001513 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001514 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001515 if (fp != NULL) {
1516 if (case_ok(buf, len, namelen, name))
1517 break;
1518 else { /* continue search */
1519 fclose(fp);
1520 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001521 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001522 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001523#if defined(PYOS_OS2)
1524 /* restore the saved snapshot */
1525 strcpy(buf, saved_buf);
1526 len = saved_len;
1527 namelen = saved_namelen;
1528#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001529 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001530#if defined(PYOS_OS2)
1531 /* don't need/want the module name snapshot anymore */
1532 if (saved_buf)
1533 {
1534 free(saved_buf);
1535 saved_buf = NULL;
1536 }
1537#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001538 if (fp != NULL)
1539 break;
1540 }
1541 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001542 PyErr_Format(PyExc_ImportError,
1543 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001544 return NULL;
1545 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001546 *p_fp = fp;
1547 return fdp;
1548}
1549
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001550/* Helpers for main.c
1551 * Find the source file corresponding to a named module
1552 */
1553struct filedescr *
1554_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1555 size_t buflen, FILE **p_fp, PyObject **p_loader)
1556{
1557 return find_module((char *) name, (char *) name, path,
1558 buf, buflen, p_fp, p_loader);
1559}
1560
1561PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1562{
1563 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1564}
1565
Martin v. Löwis18e16552006-02-15 17:27:45 +00001566/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001567 * The arguments here are tricky, best shown by example:
1568 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1569 * ^ ^ ^ ^
1570 * |--------------------- buf ---------------------|
1571 * |------------------- len ------------------|
1572 * |------ name -------|
1573 * |----- namelen -----|
1574 * buf is the full path, but len only counts up to (& exclusive of) the
1575 * extension. name is the module name, also exclusive of extension.
1576 *
1577 * We've already done a successful stat() or fopen() on buf, so know that
1578 * there's some match, possibly case-insensitive.
1579 *
Tim Peters50d8d372001-02-28 05:34:27 +00001580 * case_ok() is to return 1 if there's a case-sensitive match for
1581 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1582 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001583 *
Tim Peters50d8d372001-02-28 05:34:27 +00001584 * case_ok() is used to implement case-sensitive import semantics even
1585 * on platforms with case-insensitive filesystems. It's trivial to implement
1586 * for case-sensitive filesystems. It's pretty much a cross-platform
1587 * nightmare for systems with case-insensitive filesystems.
1588 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001589
Tim Peters50d8d372001-02-28 05:34:27 +00001590/* First we may need a pile of platform-specific header files; the sequence
1591 * of #if's here should match the sequence in the body of case_ok().
1592 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001593#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001594#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001595
Tim Peters50d8d372001-02-28 05:34:27 +00001596#elif defined(DJGPP)
1597#include <dir.h>
1598
Jason Tishler7961aa62005-05-20 00:56:54 +00001599#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001600#include <sys/types.h>
1601#include <dirent.h>
1602
Andrew MacIntyred9400542002-02-26 11:41:34 +00001603#elif defined(PYOS_OS2)
1604#define INCL_DOS
1605#define INCL_DOSERRORS
1606#define INCL_NOPMAPI
1607#include <os2.h>
Tim Peters50d8d372001-02-28 05:34:27 +00001608#endif
1609
Guido van Rossum0980bd91998-02-13 17:18:36 +00001610static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001611case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001612{
Tim Peters50d8d372001-02-28 05:34:27 +00001613/* Pick a platform-specific implementation; the sequence of #if's here should
1614 * match the sequence just above.
1615 */
1616
Jason Tishler7961aa62005-05-20 00:56:54 +00001617/* MS_WINDOWS */
1618#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001619 WIN32_FIND_DATA data;
1620 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001621
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001622 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001623 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001624
Guido van Rossum0980bd91998-02-13 17:18:36 +00001625 h = FindFirstFile(buf, &data);
1626 if (h == INVALID_HANDLE_VALUE) {
1627 PyErr_Format(PyExc_NameError,
1628 "Can't find file for module %.100s\n(filename %.300s)",
1629 name, buf);
1630 return 0;
1631 }
1632 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001633 return strncmp(data.cFileName, name, namelen) == 0;
1634
1635/* DJGPP */
1636#elif defined(DJGPP)
1637 struct ffblk ffblk;
1638 int done;
1639
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001640 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001641 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001642
1643 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1644 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001645 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001646 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001647 name, buf);
1648 return 0;
1649 }
Tim Peters50d8d372001-02-28 05:34:27 +00001650 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001651
Jason Tishler7961aa62005-05-20 00:56:54 +00001652/* new-fangled macintosh (macosx) or Cygwin */
1653#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001654 DIR *dirp;
1655 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001656 char dirname[MAXPATHLEN + 1];
1657 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001658
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001659 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001660 return 1;
1661
Tim Petersd1e87a82001-03-01 18:12:00 +00001662 /* Copy the dir component into dirname; substitute "." if empty */
1663 if (dirlen <= 0) {
1664 dirname[0] = '.';
1665 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001666 }
1667 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001668 assert(dirlen <= MAXPATHLEN);
1669 memcpy(dirname, buf, dirlen);
1670 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001671 }
1672 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001673 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001674 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001675 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001676 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001677 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001678#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001679 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001680#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001681 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001682#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001683 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001684 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001685 (void)closedir(dirp);
1686 return 1; /* Found */
1687 }
1688 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001689 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001690 }
Tim Peters430f5d42001-03-01 01:30:56 +00001691 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001692
Andrew MacIntyred9400542002-02-26 11:41:34 +00001693/* OS/2 */
1694#elif defined(PYOS_OS2)
1695 HDIR hdir = 1;
1696 ULONG srchcnt = 1;
1697 FILEFINDBUF3 ffbuf;
1698 APIRET rc;
1699
Christian Heimes790c8232008-01-07 21:14:23 +00001700 if (Py_GETENV("PYTHONCASEOK") != NULL)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001701 return 1;
1702
1703 rc = DosFindFirst(buf,
1704 &hdir,
1705 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1706 &ffbuf, sizeof(ffbuf),
1707 &srchcnt,
1708 FIL_STANDARD);
1709 if (rc != NO_ERROR)
1710 return 0;
1711 return strncmp(ffbuf.achName, name, namelen) == 0;
1712
Tim Peters50d8d372001-02-28 05:34:27 +00001713/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1714#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001715 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001716
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001717#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001718}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001719
Guido van Rossum0980bd91998-02-13 17:18:36 +00001720
Guido van Rossum197346f1997-10-31 18:38:52 +00001721#ifdef HAVE_STAT
1722/* Helper to look for __init__.py or __init__.py[co] in potential package */
1723static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001724find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001725{
Tim Peters0f9431f2001-07-05 03:47:53 +00001726 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001727 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001728 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001729 struct stat statbuf;
1730
Tim Peters0f9431f2001-07-05 03:47:53 +00001731/* For calling case_ok(buf, len, namelen, name):
1732 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1733 * ^ ^ ^ ^
1734 * |--------------------- buf ---------------------|
1735 * |------------------- len ------------------|
1736 * |------ name -------|
1737 * |----- namelen -----|
1738 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001739 if (save_len + 13 >= MAXPATHLEN)
1740 return 0;
1741 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001742 pname = buf + i;
1743 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001744 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001745 if (case_ok(buf,
1746 save_len + 9, /* len("/__init__") */
1747 8, /* len("__init__") */
1748 pname)) {
1749 buf[save_len] = '\0';
1750 return 1;
1751 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001752 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001753 i += strlen(pname);
1754 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001755 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001756 if (case_ok(buf,
1757 save_len + 9, /* len("/__init__") */
1758 8, /* len("__init__") */
1759 pname)) {
1760 buf[save_len] = '\0';
1761 return 1;
1762 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001763 }
1764 buf[save_len] = '\0';
1765 return 0;
1766}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001767
Guido van Rossum197346f1997-10-31 18:38:52 +00001768#endif /* HAVE_STAT */
1769
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001770
Tim Petersdbd9ba62000-07-09 03:09:57 +00001771static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001772
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001773/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001774 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001775
Guido van Rossum79f25d91997-04-29 20:08:16 +00001776static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001777load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001778{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001779 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001780 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001781 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001782
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001783 /* First check that there's an open file (if we need one) */
1784 switch (type) {
1785 case PY_SOURCE:
1786 case PY_COMPILED:
1787 if (fp == NULL) {
1788 PyErr_Format(PyExc_ValueError,
1789 "file object required for import (type code %d)",
1790 type);
1791 return NULL;
1792 }
1793 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001794
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001795 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001796
1797 case PY_SOURCE:
1798 m = load_source_module(name, buf, fp);
1799 break;
1800
1801 case PY_COMPILED:
1802 m = load_compiled_module(name, buf, fp);
1803 break;
1804
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001805#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001806 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001807 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001808 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001809#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001810
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001811 case PKG_DIRECTORY:
1812 m = load_package(name, buf);
1813 break;
1814
1815 case C_BUILTIN:
1816 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001817 if (buf != NULL && buf[0] != '\0')
1818 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001819 if (type == C_BUILTIN)
1820 err = init_builtin(name);
1821 else
1822 err = PyImport_ImportFrozenModule(name);
1823 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001824 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001825 if (err == 0) {
1826 PyErr_Format(PyExc_ImportError,
1827 "Purported %s module %.200s not found",
1828 type == C_BUILTIN ?
1829 "builtin" : "frozen",
1830 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001831 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001832 }
1833 modules = PyImport_GetModuleDict();
1834 m = PyDict_GetItemString(modules, name);
1835 if (m == NULL) {
1836 PyErr_Format(
1837 PyExc_ImportError,
1838 "%s module %.200s not properly initialized",
1839 type == C_BUILTIN ?
1840 "builtin" : "frozen",
1841 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001842 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001843 }
1844 Py_INCREF(m);
1845 break;
1846
Just van Rossum52e14d62002-12-30 22:08:05 +00001847 case IMP_HOOK: {
1848 if (loader == NULL) {
1849 PyErr_SetString(PyExc_ImportError,
1850 "import hook without loader");
1851 return NULL;
1852 }
1853 m = PyObject_CallMethod(loader, "load_module", "s", name);
1854 break;
1855 }
1856
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001857 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001858 PyErr_Format(PyExc_ImportError,
1859 "Don't know how to import %.200s (type code %d)",
1860 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001861 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001862
1863 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001864
1865 return m;
1866}
1867
1868
1869/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001870 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001871 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001872
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001873static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001874init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001875{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001876 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001877
Greg Ward201baee2001-10-04 14:52:06 +00001878 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001879 return 1;
1880
Guido van Rossum771c6c81997-10-31 18:37:24 +00001881 for (p = PyImport_Inittab; p->name != NULL; p++) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00001882 PyObject *mod;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001883 if (strcmp(name, p->name) == 0) {
1884 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001885 PyErr_Format(PyExc_ImportError,
1886 "Cannot re-init internal module %.200s",
1887 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001888 return -1;
1889 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001890 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001891 PySys_WriteStderr("import %s # builtin\n", name);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001892 mod = (*p->initfunc)();
1893 if (mod == 0)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001894 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001895 if (_PyImport_FixupExtension(mod, name, name) < 0)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001896 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001897 /* FixupExtension has put the module into sys.modules,
1898 so we can release our own reference. */
1899 Py_DECREF(mod);
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001900 return 1;
1901 }
1902 }
1903 return 0;
1904}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001905
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001906
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001907/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001908
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001909static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001910find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001911{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001912 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001913
Benjamin Petersond968e272008-11-05 22:48:33 +00001914 if (!name)
1915 return NULL;
1916
Guido van Rossum79f25d91997-04-29 20:08:16 +00001917 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001918 if (p->name == NULL)
1919 return NULL;
1920 if (strcmp(p->name, name) == 0)
1921 break;
1922 }
1923 return p;
1924}
1925
Guido van Rossum79f25d91997-04-29 20:08:16 +00001926static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001927get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001928{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001929 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001930 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001931
1932 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001933 PyErr_Format(PyExc_ImportError,
1934 "No such frozen object named %.200s",
1935 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001936 return NULL;
1937 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001938 if (p->code == NULL) {
1939 PyErr_Format(PyExc_ImportError,
1940 "Excluded frozen object named %.200s",
1941 name);
1942 return NULL;
1943 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001944 size = p->size;
1945 if (size < 0)
1946 size = -size;
1947 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001948}
1949
1950/* Initialize a frozen module.
1951 Return 1 for succes, 0 if the module is not found, and -1 with
1952 an exception set if the initialization failed.
1953 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001954
1955int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001956PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001957{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001958 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001959 PyObject *co;
1960 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001961 int ispackage;
1962 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001963
1964 if (p == NULL)
1965 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001966 if (p->code == NULL) {
1967 PyErr_Format(PyExc_ImportError,
1968 "Excluded frozen object named %.200s",
1969 name);
1970 return -1;
1971 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001972 size = p->size;
1973 ispackage = (size < 0);
1974 if (ispackage)
1975 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001976 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001977 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001978 name, ispackage ? " package" : "");
1979 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001980 if (co == NULL)
1981 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001982 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001983 PyErr_Format(PyExc_TypeError,
1984 "frozen object %.200s is not a code object",
1985 name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001986 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001987 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001988 if (ispackage) {
1989 /* Set __path__ to the package name */
Benjamin Petersond968e272008-11-05 22:48:33 +00001990 PyObject *d, *s, *l;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001991 int err;
1992 m = PyImport_AddModule(name);
1993 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001994 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001995 d = PyModule_GetDict(m);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001996 s = PyUnicode_InternFromString(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001997 if (s == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001998 goto err_return;
Benjamin Petersond968e272008-11-05 22:48:33 +00001999 l = PyList_New(1);
2000 if (l == NULL) {
2001 Py_DECREF(s);
2002 goto err_return;
2003 }
2004 PyList_SET_ITEM(l, 0, s);
2005 err = PyDict_SetItemString(d, "__path__", l);
2006 Py_DECREF(l);
Guido van Rossuma5568d31998-03-05 03:45:08 +00002007 if (err != 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002008 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002009 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00002010 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002011 if (m == NULL)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002012 goto err_return;
2013 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002014 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002015 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002016err_return:
2017 Py_DECREF(co);
2018 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002019}
Guido van Rossum74e6a111994-08-29 12:54:38 +00002020
2021
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002022/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002023 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00002024
Guido van Rossum79f25d91997-04-29 20:08:16 +00002025PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002026PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00002027{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002028 PyObject *pname;
2029 PyObject *result;
2030
Martin v. Löwis5b222132007-06-10 09:51:05 +00002031 pname = PyUnicode_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00002032 if (pname == NULL)
2033 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002034 result = PyImport_Import(pname);
2035 Py_DECREF(pname);
2036 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002037}
2038
Christian Heimes072c0f12008-01-03 23:01:04 +00002039/* Import a module without blocking
2040 *
2041 * At first it tries to fetch the module from sys.modules. If the module was
2042 * never loaded before it loads it with PyImport_ImportModule() unless another
2043 * thread holds the import lock. In the latter case the function raises an
2044 * ImportError instead of blocking.
2045 *
2046 * Returns the module object with incremented ref count.
2047 */
2048PyObject *
2049PyImport_ImportModuleNoBlock(const char *name)
2050{
2051 PyObject *result;
2052 PyObject *modules;
2053 long me;
2054
2055 /* Try to get the module from sys.modules[name] */
2056 modules = PyImport_GetModuleDict();
2057 if (modules == NULL)
2058 return NULL;
2059
2060 result = PyDict_GetItemString(modules, name);
2061 if (result != NULL) {
2062 Py_INCREF(result);
2063 return result;
2064 }
2065 else {
2066 PyErr_Clear();
2067 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002068#ifdef WITH_THREAD
Christian Heimes072c0f12008-01-03 23:01:04 +00002069 /* check the import lock
2070 * me might be -1 but I ignore the error here, the lock function
2071 * takes care of the problem */
2072 me = PyThread_get_thread_ident();
2073 if (import_lock_thread == -1 || import_lock_thread == me) {
2074 /* no thread or me is holding the lock */
2075 return PyImport_ImportModule(name);
2076 }
2077 else {
2078 PyErr_Format(PyExc_ImportError,
2079 "Failed to import %.200s because the import lock"
2080 "is held by another thread.",
2081 name);
2082 return NULL;
2083 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002084#else
2085 return PyImport_ImportModule(name);
2086#endif
Christian Heimes072c0f12008-01-03 23:01:04 +00002087}
2088
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002089/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002090static PyObject *get_parent(PyObject *globals, char *buf,
2091 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002092static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002093 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002094static int mark_miss(char *name);
2095static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002096 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002097static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002098
2099/* The Magnum Opus of dotted-name import :-) */
2100
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002101static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002102import_module_level(char *name, PyObject *globals, PyObject *locals,
2103 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002104{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002105 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00002106 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002107 PyObject *parent, *head, *next, *tail;
2108
Christian Heimes454f37b2008-01-10 00:10:02 +00002109 if (strchr(name, '/') != NULL
2110#ifdef MS_WINDOWS
2111 || strchr(name, '\\') != NULL
2112#endif
2113 ) {
2114 PyErr_SetString(PyExc_ImportError,
2115 "Import by filename is not supported.");
2116 return NULL;
2117 }
2118
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002119 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002120 if (parent == NULL)
2121 return NULL;
2122
2123 head = load_next(parent, Py_None, &name, buf, &buflen);
2124 if (head == NULL)
2125 return NULL;
2126
2127 tail = head;
2128 Py_INCREF(tail);
2129 while (name) {
2130 next = load_next(tail, tail, &name, buf, &buflen);
2131 Py_DECREF(tail);
2132 if (next == NULL) {
2133 Py_DECREF(head);
2134 return NULL;
2135 }
2136 tail = next;
2137 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002138 if (tail == Py_None) {
2139 /* If tail is Py_None, both get_parent and load_next found
2140 an empty module name: someone called __import__("") or
2141 doctored faulty bytecode */
2142 Py_DECREF(tail);
2143 Py_DECREF(head);
2144 PyErr_SetString(PyExc_ValueError,
2145 "Empty module name");
2146 return NULL;
2147 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002148
2149 if (fromlist != NULL) {
2150 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2151 fromlist = NULL;
2152 }
2153
2154 if (fromlist == NULL) {
2155 Py_DECREF(tail);
2156 return head;
2157 }
2158
2159 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002160 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002161 Py_DECREF(tail);
2162 return NULL;
2163 }
2164
2165 return tail;
2166}
2167
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002168PyObject *
2169PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2170 PyObject *fromlist, int level)
2171{
2172 PyObject *result;
2173 lock_import();
2174 result = import_module_level(name, globals, locals, fromlist, level);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002175 if (unlock_import() < 0) {
2176 Py_XDECREF(result);
2177 PyErr_SetString(PyExc_RuntimeError,
2178 "not holding the import lock");
2179 return NULL;
2180 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002181 return result;
2182}
2183
Fred Drake87590902004-05-28 20:21:36 +00002184/* Return the package that an import is being performed in. If globals comes
2185 from the module foo.bar.bat (not itself a package), this returns the
2186 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002187 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002188
2189 The *name* of the returned package is returned in buf, with the length of
2190 the name in *p_buflen.
2191
2192 If globals doesn't come from a package or a module in a package, or a
2193 corresponding entry is not found in sys.modules, Py_None is returned.
2194*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002195static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002196get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002197{
2198 static PyObject *namestr = NULL;
2199 static PyObject *pathstr = NULL;
Nick Coghlande10c852007-12-04 12:22:52 +00002200 static PyObject *pkgstr = NULL;
2201 PyObject *pkgname, *modname, *modpath, *modules, *parent;
Georg Brandl2ee470f2008-07-16 12:55:28 +00002202 int orig_level = level;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002203
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002204 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002205 return Py_None;
2206
2207 if (namestr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002208 namestr = PyUnicode_InternFromString("__name__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002209 if (namestr == NULL)
2210 return NULL;
2211 }
2212 if (pathstr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002213 pathstr = PyUnicode_InternFromString("__path__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002214 if (pathstr == NULL)
2215 return NULL;
2216 }
Nick Coghlande10c852007-12-04 12:22:52 +00002217 if (pkgstr == NULL) {
2218 pkgstr = PyUnicode_InternFromString("__package__");
2219 if (pkgstr == NULL)
2220 return NULL;
2221 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002222
2223 *buf = '\0';
2224 *p_buflen = 0;
Nick Coghlande10c852007-12-04 12:22:52 +00002225 pkgname = PyDict_GetItem(globals, pkgstr);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002226
Nick Coghlande10c852007-12-04 12:22:52 +00002227 if ((pkgname != NULL) && (pkgname != Py_None)) {
2228 /* __package__ is set, so use it */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002229 char *pkgname_str;
Nick Coghlande10c852007-12-04 12:22:52 +00002230 Py_ssize_t len;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002231
Nick Coghlande10c852007-12-04 12:22:52 +00002232 if (!PyUnicode_Check(pkgname)) {
2233 PyErr_SetString(PyExc_ValueError,
2234 "__package__ set to non-string");
2235 return NULL;
2236 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002237 pkgname_str = _PyUnicode_AsStringAndSize(pkgname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002238 if (len == 0) {
2239 if (level > 0) {
2240 PyErr_SetString(PyExc_ValueError,
2241 "Attempted relative import in non-package");
2242 return NULL;
2243 }
2244 return Py_None;
2245 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002246 if (len > MAXPATHLEN) {
2247 PyErr_SetString(PyExc_ValueError,
Nick Coghlande10c852007-12-04 12:22:52 +00002248 "Package name too long");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002249 return NULL;
2250 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002251 strcpy(buf, pkgname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002252 } else {
2253 /* __package__ not set, so figure it out and set it */
2254 modname = PyDict_GetItem(globals, namestr);
2255 if (modname == NULL || !PyUnicode_Check(modname))
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002256 return Py_None;
Nick Coghlande10c852007-12-04 12:22:52 +00002257
2258 modpath = PyDict_GetItem(globals, pathstr);
2259 if (modpath != NULL) {
2260 /* __path__ is set, so modname is already the package name */
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002261 char *modname_str;
2262 Py_ssize_t len;
Nick Coghlande10c852007-12-04 12:22:52 +00002263 int error;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002264
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002265 modname_str = _PyUnicode_AsStringAndSize(modname, &len);
Nick Coghlande10c852007-12-04 12:22:52 +00002266 if (len > MAXPATHLEN) {
2267 PyErr_SetString(PyExc_ValueError,
2268 "Module name too long");
2269 return NULL;
2270 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002271 strcpy(buf, modname_str);
Nick Coghlande10c852007-12-04 12:22:52 +00002272 error = PyDict_SetItem(globals, pkgstr, modname);
2273 if (error) {
2274 PyErr_SetString(PyExc_ValueError,
2275 "Could not set __package__");
2276 return NULL;
2277 }
2278 } else {
2279 /* Normal module, so work out the package name if any */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002280 char *start = _PyUnicode_AsString(modname);
Nick Coghlande10c852007-12-04 12:22:52 +00002281 char *lastdot = strrchr(start, '.');
2282 size_t len;
2283 int error;
2284 if (lastdot == NULL && level > 0) {
2285 PyErr_SetString(PyExc_ValueError,
2286 "Attempted relative import in non-package");
2287 return NULL;
2288 }
2289 if (lastdot == NULL) {
2290 error = PyDict_SetItem(globals, pkgstr, Py_None);
2291 if (error) {
2292 PyErr_SetString(PyExc_ValueError,
2293 "Could not set __package__");
2294 return NULL;
2295 }
2296 return Py_None;
2297 }
2298 len = lastdot - start;
2299 if (len >= MAXPATHLEN) {
2300 PyErr_SetString(PyExc_ValueError,
2301 "Module name too long");
2302 return NULL;
2303 }
2304 strncpy(buf, start, len);
2305 buf[len] = '\0';
2306 pkgname = PyUnicode_FromString(buf);
2307 if (pkgname == NULL) {
2308 return NULL;
2309 }
2310 error = PyDict_SetItem(globals, pkgstr, pkgname);
2311 Py_DECREF(pkgname);
2312 if (error) {
2313 PyErr_SetString(PyExc_ValueError,
2314 "Could not set __package__");
2315 return NULL;
2316 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002317 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002318 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002319 while (--level > 0) {
2320 char *dot = strrchr(buf, '.');
2321 if (dot == NULL) {
2322 PyErr_SetString(PyExc_ValueError,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002323 "Attempted relative import beyond "
2324 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002325 return NULL;
2326 }
2327 *dot = '\0';
2328 }
2329 *p_buflen = strlen(buf);
2330
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002331 modules = PyImport_GetModuleDict();
2332 parent = PyDict_GetItemString(modules, buf);
Georg Brandl2ee470f2008-07-16 12:55:28 +00002333 if (parent == NULL) {
2334 if (orig_level < 1) {
2335 PyObject *err_msg = PyBytes_FromFormat(
2336 "Parent module '%.200s' not found "
2337 "while handling absolute import", buf);
2338 if (err_msg == NULL) {
2339 return NULL;
2340 }
2341 if (!PyErr_WarnEx(PyExc_RuntimeWarning,
2342 PyBytes_AsString(err_msg), 1)) {
2343 *buf = '\0';
2344 *p_buflen = 0;
2345 parent = Py_None;
2346 }
2347 Py_DECREF(err_msg);
2348 } else {
2349 PyErr_Format(PyExc_SystemError,
2350 "Parent module '%.200s' not loaded, "
2351 "cannot perform relative import", buf);
2352 }
2353 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002354 return parent;
2355 /* We expect, but can't guarantee, if parent != None, that:
2356 - parent.__name__ == buf
2357 - parent.__dict__ is globals
2358 If this is violated... Who cares? */
2359}
2360
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002361/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002362static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002363load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002364 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002365{
2366 char *name = *p_name;
2367 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002368 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002369 char *p;
2370 PyObject *result;
2371
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002372 if (strlen(name) == 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002373 /* completely empty module name should only happen in
2374 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002375 Py_INCREF(mod);
2376 *p_name = NULL;
2377 return mod;
2378 }
2379
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002380 if (dot == NULL) {
2381 *p_name = NULL;
2382 len = strlen(name);
2383 }
2384 else {
2385 *p_name = dot+1;
2386 len = dot-name;
2387 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002388 if (len == 0) {
2389 PyErr_SetString(PyExc_ValueError,
2390 "Empty module name");
2391 return NULL;
2392 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002393
2394 p = buf + *p_buflen;
2395 if (p != buf)
2396 *p++ = '.';
2397 if (p+len-buf >= MAXPATHLEN) {
2398 PyErr_SetString(PyExc_ValueError,
2399 "Module name too long");
2400 return NULL;
2401 }
2402 strncpy(p, name, len);
2403 p[len] = '\0';
2404 *p_buflen = p+len-buf;
2405
2406 result = import_submodule(mod, p, buf);
2407 if (result == Py_None && altmod != mod) {
2408 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002409 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002410 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002411 if (result != NULL && result != Py_None) {
2412 if (mark_miss(buf) != 0) {
2413 Py_DECREF(result);
2414 return NULL;
2415 }
2416 strncpy(buf, name, len);
2417 buf[len] = '\0';
2418 *p_buflen = len;
2419 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002420 }
2421 if (result == NULL)
2422 return NULL;
2423
2424 if (result == Py_None) {
2425 Py_DECREF(result);
2426 PyErr_Format(PyExc_ImportError,
2427 "No module named %.200s", name);
2428 return NULL;
2429 }
2430
2431 return result;
2432}
2433
2434static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002435mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002436{
2437 PyObject *modules = PyImport_GetModuleDict();
2438 return PyDict_SetItemString(modules, name, Py_None);
2439}
2440
2441static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002442ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002443 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002444{
2445 int i;
2446
2447 if (!PyObject_HasAttrString(mod, "__path__"))
2448 return 1;
2449
2450 for (i = 0; ; i++) {
2451 PyObject *item = PySequence_GetItem(fromlist, i);
2452 int hasit;
2453 if (item == NULL) {
2454 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2455 PyErr_Clear();
2456 return 1;
2457 }
2458 return 0;
2459 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002460 if (!PyUnicode_Check(item)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002461 PyErr_SetString(PyExc_TypeError,
Neal Norwitz80e7f272007-08-26 06:45:23 +00002462 "Item in ``from list'' not a string");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002463 Py_DECREF(item);
2464 return 0;
2465 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002466 if (PyUnicode_AS_UNICODE(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002467 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002468 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002469 /* See if the package defines __all__ */
2470 if (recursive)
2471 continue; /* Avoid endless recursion */
2472 all = PyObject_GetAttrString(mod, "__all__");
2473 if (all == NULL)
2474 PyErr_Clear();
2475 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002476 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002477 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002478 if (!ret)
2479 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002480 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002481 continue;
2482 }
2483 hasit = PyObject_HasAttr(mod, item);
2484 if (!hasit) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002485 PyObject *item8;
2486 char *subname;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002487 PyObject *submod;
2488 char *p;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002489 if (!Py_FileSystemDefaultEncoding) {
2490 item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item),
2491 PyUnicode_GetSize(item),
Martin v. Löwis427dbff2007-06-12 05:53:00 +00002492 NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002493 } else {
Guido van Rossum98297ee2007-11-06 21:34:58 +00002494 item8 = PyUnicode_AsEncodedString(item,
2495 Py_FileSystemDefaultEncoding, NULL);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002496 }
2497 if (!item8) {
2498 PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
2499 return 0;
2500 }
Christian Heimes72b710a2008-05-26 13:28:38 +00002501 subname = PyBytes_AS_STRING(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002502 if (buflen + strlen(subname) >= MAXPATHLEN) {
2503 PyErr_SetString(PyExc_ValueError,
2504 "Module name too long");
2505 Py_DECREF(item);
2506 return 0;
2507 }
2508 p = buf + buflen;
2509 *p++ = '.';
2510 strcpy(p, subname);
2511 submod = import_submodule(mod, subname, buf);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002512 Py_DECREF(item8);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002513 Py_XDECREF(submod);
2514 if (submod == NULL) {
2515 Py_DECREF(item);
2516 return 0;
2517 }
2518 }
2519 Py_DECREF(item);
2520 }
2521
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002522 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002523}
2524
Neil Schemenauer00b09662003-06-16 21:03:07 +00002525static int
2526add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2527 PyObject *modules)
2528{
2529 if (mod == Py_None)
2530 return 1;
2531 /* Irrespective of the success of this load, make a
2532 reference to it in the parent package module. A copy gets
2533 saved in the modules dictionary under the full name, so get a
2534 reference from there, if need be. (The exception is when the
2535 load failed with a SyntaxError -- then there's no trace in
2536 sys.modules. In that case, of course, do nothing extra.) */
2537 if (submod == NULL) {
2538 submod = PyDict_GetItemString(modules, fullname);
2539 if (submod == NULL)
2540 return 1;
2541 }
2542 if (PyModule_Check(mod)) {
2543 /* We can't use setattr here since it can give a
2544 * spurious warning if the submodule name shadows a
2545 * builtin name */
2546 PyObject *dict = PyModule_GetDict(mod);
2547 if (!dict)
2548 return 0;
2549 if (PyDict_SetItemString(dict, subname, submod) < 0)
2550 return 0;
2551 }
2552 else {
2553 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2554 return 0;
2555 }
2556 return 1;
2557}
2558
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002559static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002560import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002561{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002562 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002563 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002564
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002565 /* Require:
2566 if mod == None: subname == fullname
2567 else: mod.__name__ + "." + subname == fullname
2568 */
2569
Tim Peters50d8d372001-02-28 05:34:27 +00002570 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002571 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002572 }
2573 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002574 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002575 char buf[MAXPATHLEN+1];
2576 struct filedescr *fdp;
2577 FILE *fp = NULL;
2578
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002579 if (mod == Py_None)
2580 path = NULL;
2581 else {
2582 path = PyObject_GetAttrString(mod, "__path__");
2583 if (path == NULL) {
2584 PyErr_Clear();
2585 Py_INCREF(Py_None);
2586 return Py_None;
2587 }
2588 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002589
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002590 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002591 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2592 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002593 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002594 if (fdp == NULL) {
2595 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2596 return NULL;
2597 PyErr_Clear();
2598 Py_INCREF(Py_None);
2599 return Py_None;
2600 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002601 m = load_module(fullname, fp, buf, fdp->type, loader);
2602 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002603 if (fp)
2604 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002605 if (!add_submodule(mod, m, fullname, subname, modules)) {
2606 Py_XDECREF(m);
2607 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002608 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002609 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002610
2611 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002612}
2613
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002614
2615/* Re-import a module of any kind and return its module object, WITH
2616 INCREMENTED REFERENCE COUNT */
2617
Guido van Rossum79f25d91997-04-29 20:08:16 +00002618PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002619PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002620{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002621 PyInterpreterState *interp = PyThreadState_Get()->interp;
2622 PyObject *modules_reloading = interp->modules_reloading;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002623 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossumd8faa362007-04-27 19:54:29 +00002624 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002625 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002626 char buf[MAXPATHLEN+1];
2627 struct filedescr *fdp;
2628 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002629 PyObject *newm;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002630
2631 if (modules_reloading == NULL) {
2632 Py_FatalError("PyImport_ReloadModule: "
Guido van Rossume7ba4952007-06-06 23:52:48 +00002633 "no modules_reloading dictionary!");
Guido van Rossumd8faa362007-04-27 19:54:29 +00002634 return NULL;
2635 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002636
Guido van Rossum79f25d91997-04-29 20:08:16 +00002637 if (m == NULL || !PyModule_Check(m)) {
2638 PyErr_SetString(PyExc_TypeError,
2639 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002640 return NULL;
2641 }
Neal Norwitz5616c6d2007-08-26 05:32:41 +00002642 name = (char*)PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002643 if (name == NULL)
2644 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002645 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002646 PyErr_Format(PyExc_ImportError,
2647 "reload(): module %.200s not in sys.modules",
2648 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002649 return NULL;
2650 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002651 existing_m = PyDict_GetItemString(modules_reloading, name);
2652 if (existing_m != NULL) {
2653 /* Due to a recursive reload, this module is already
2654 being reloaded. */
2655 Py_INCREF(existing_m);
2656 return existing_m;
2657 }
2658 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
2659 return NULL;
2660
Guido van Rossum222ef561997-09-06 19:41:09 +00002661 subname = strrchr(name, '.');
2662 if (subname == NULL)
2663 subname = name;
2664 else {
2665 PyObject *parentname, *parent;
Christian Heimesc4cb3b82007-11-04 12:10:01 +00002666 parentname = PyUnicode_FromStringAndSize(name, (subname-name));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002667 if (parentname == NULL) {
2668 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002669 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002670 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002671 parent = PyDict_GetItem(modules, parentname);
2672 if (parent == NULL) {
2673 PyErr_Format(PyExc_ImportError,
2674 "reload(): parent %.200s not in sys.modules",
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002675 _PyUnicode_AsString(parentname));
Georg Brandl0c55f292005-09-14 06:56:20 +00002676 Py_DECREF(parentname);
Guido van Rossume7ba4952007-06-06 23:52:48 +00002677 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002678 return NULL;
2679 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002680 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002681 subname++;
2682 path = PyObject_GetAttrString(parent, "__path__");
2683 if (path == NULL)
2684 PyErr_Clear();
2685 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002686 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002687 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002688 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002689
2690 if (fdp == NULL) {
2691 Py_XDECREF(loader);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002692 imp_modules_reloading_clear();
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002693 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002694 }
2695
2696 newm = load_module(name, fp, buf, fdp->type, loader);
2697 Py_XDECREF(loader);
2698
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002699 if (fp)
2700 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002701 if (newm == NULL) {
2702 /* load_module probably removed name from modules because of
2703 * the error. Put back the original module object. We're
2704 * going to return NULL in this case regardless of whether
2705 * replacing name succeeds, so the return value is ignored.
2706 */
2707 PyDict_SetItemString(modules, name, m);
2708 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002709 imp_modules_reloading_clear();
Tim Peters1cd70172004-08-02 03:52:12 +00002710 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002711}
2712
2713
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002714/* Higher-level import emulator which emulates the "import" statement
2715 more accurately -- it invokes the __import__() function from the
2716 builtins of the current globals. This means that the import is
2717 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002718 environment, e.g. by "rexec".
2719 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00002720 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00002721 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002722
2723PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002724PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002725{
2726 static PyObject *silly_list = NULL;
2727 static PyObject *builtins_str = NULL;
2728 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002729 PyObject *globals = NULL;
2730 PyObject *import = NULL;
2731 PyObject *builtins = NULL;
2732 PyObject *r = NULL;
2733
2734 /* Initialize constant string objects */
2735 if (silly_list == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002736 import_str = PyUnicode_InternFromString("__import__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002737 if (import_str == NULL)
2738 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002739 builtins_str = PyUnicode_InternFromString("__builtins__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002740 if (builtins_str == NULL)
2741 return NULL;
2742 silly_list = Py_BuildValue("[s]", "__doc__");
2743 if (silly_list == NULL)
2744 return NULL;
2745 }
2746
2747 /* Get the builtins from current globals */
2748 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002749 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002750 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002751 builtins = PyObject_GetItem(globals, builtins_str);
2752 if (builtins == NULL)
2753 goto err;
2754 }
2755 else {
2756 /* No globals -- use standard builtins, and fake globals */
2757 PyErr_Clear();
2758
Georg Brandl1a3284e2007-12-02 09:40:06 +00002759 builtins = PyImport_ImportModuleLevel("builtins",
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002760 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002761 if (builtins == NULL)
2762 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002763 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2764 if (globals == NULL)
2765 goto err;
2766 }
2767
2768 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002769 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002770 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002771 if (import == NULL)
2772 PyErr_SetObject(PyExc_KeyError, import_str);
2773 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002774 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002775 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002776 if (import == NULL)
2777 goto err;
2778
Christian Heimes072c0f12008-01-03 23:01:04 +00002779 /* Call the __import__ function with the proper argument list
2780 * Always use absolute import here. */
2781 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
2782 globals, silly_list, 0, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002783
2784 err:
2785 Py_XDECREF(globals);
2786 Py_XDECREF(builtins);
2787 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002788
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002789 return r;
2790}
2791
2792
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002793/* Module 'imp' provides Python access to the primitives used for
2794 importing modules.
2795*/
2796
Guido van Rossum79f25d91997-04-29 20:08:16 +00002797static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002798imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002799{
2800 char buf[4];
2801
Guido van Rossum96774c12000-05-01 20:19:08 +00002802 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2803 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2804 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2805 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002806
Christian Heimes72b710a2008-05-26 13:28:38 +00002807 return PyBytes_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002808}
2809
Guido van Rossum79f25d91997-04-29 20:08:16 +00002810static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002811imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002812{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002813 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002814 struct filedescr *fdp;
2815
Guido van Rossum79f25d91997-04-29 20:08:16 +00002816 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002817 if (list == NULL)
2818 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002819 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2820 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002821 fdp->suffix, fdp->mode, fdp->type);
2822 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002823 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002824 return NULL;
2825 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002826 if (PyList_Append(list, item) < 0) {
2827 Py_DECREF(list);
2828 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002829 return NULL;
2830 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002831 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002832 }
2833 return list;
2834}
2835
Guido van Rossum79f25d91997-04-29 20:08:16 +00002836static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002837call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002838{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002839 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002840 PyObject *fob, *ret;
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002841 PyObject *pathobj;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002842 struct filedescr *fdp;
2843 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002844 FILE *fp = NULL;
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002845 int fd = -1;
Brett Cannon3bb42d92007-10-20 03:43:15 +00002846 char *found_encoding = NULL;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002847 char *encoding = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002848
2849 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002850 if (path == Py_None)
2851 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002852 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002853 if (fdp == NULL)
2854 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002855 if (fp != NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002856 fd = fileno(fp);
2857 if (fd != -1)
2858 fd = dup(fd);
2859 fclose(fp);
2860 fp = NULL;
2861 }
2862 if (fd != -1) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002863 if (strchr(fdp->mode, 'b') == NULL) {
Brett Cannon3bb42d92007-10-20 03:43:15 +00002864 /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
2865 memory. */
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002866 found_encoding = PyTokenizer_FindEncoding(fd);
2867 lseek(fd, 0, 0); /* Reset position */
Amaury Forgeot d'Arc1b933ed2008-09-04 22:34:09 +00002868 if (found_encoding == NULL && PyErr_Occurred())
2869 return NULL;
Brett Cannon3bb42d92007-10-20 03:43:15 +00002870 encoding = (found_encoding != NULL) ? found_encoding :
Guido van Rossumce3a72a2007-10-19 23:16:50 +00002871 (char*)PyUnicode_GetDefaultEncoding();
2872 }
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002873 fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002874 (char*)encoding, NULL, NULL, 1);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002875 if (fob == NULL) {
Guido van Rossum40d20bc2007-10-22 00:09:51 +00002876 close(fd);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002877 PyMem_FREE(found_encoding);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002878 return NULL;
2879 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002880 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002881 else {
2882 fob = Py_None;
2883 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002884 }
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002885 pathobj = PyUnicode_DecodeFSDefault(pathname);
2886 ret = Py_BuildValue("NN(ssi)",
2887 fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
Brett Cannon3bb42d92007-10-20 03:43:15 +00002888 PyMem_FREE(found_encoding);
2889
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002890 return ret;
2891}
2892
Guido van Rossum79f25d91997-04-29 20:08:16 +00002893static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002894imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002895{
2896 char *name;
Hirokazu Yamamoto90eaaf62009-01-30 03:15:05 +00002897 PyObject *ret, *path = NULL;
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +00002898 if (!PyArg_ParseTuple(args, "es|O:find_module",
2899 Py_FileSystemDefaultEncoding, &name,
2900 &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002901 return NULL;
Hirokazu Yamamoto90eaaf62009-01-30 03:15:05 +00002902 ret = call_find_module(name, path);
2903 PyMem_Free(name);
2904 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002905}
2906
2907static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002908imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002909{
2910 char *name;
2911 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002912 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002913 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002914 return NULL;
2915 ret = init_builtin(name);
2916 if (ret < 0)
2917 return NULL;
2918 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002919 Py_INCREF(Py_None);
2920 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002921 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002922 m = PyImport_AddModule(name);
2923 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002924 return m;
2925}
2926
Guido van Rossum79f25d91997-04-29 20:08:16 +00002927static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002928imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002929{
2930 char *name;
2931 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002932 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002933 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002934 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002935 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002936 if (ret < 0)
2937 return NULL;
2938 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002939 Py_INCREF(Py_None);
2940 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002941 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002942 m = PyImport_AddModule(name);
2943 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002944 return m;
2945}
2946
Guido van Rossum79f25d91997-04-29 20:08:16 +00002947static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002948imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002949{
2950 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002951
Guido van Rossum43713e52000-02-29 13:59:29 +00002952 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002953 return NULL;
2954 return get_frozen_object(name);
2955}
2956
Guido van Rossum79f25d91997-04-29 20:08:16 +00002957static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002958imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002959{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002960 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002961 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002962 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00002963 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002964}
2965
Guido van Rossum79f25d91997-04-29 20:08:16 +00002966static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002967imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002968{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002969 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002970 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002971 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002972 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002973 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002974 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002975}
2976
2977static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002978get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002979{
2980 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002981 if (mode[0] == 'U')
2982 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002983 if (fob == NULL) {
2984 fp = fopen(pathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002985 }
2986 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002987 int fd = PyObject_AsFileDescriptor(fob);
2988 if (fd == -1)
2989 return NULL;
2990 /* XXX This will leak a FILE struct. Fix this!!!!
2991 (But it doesn't leak a file descrioptor!) */
2992 fp = fdopen(fd, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002993 }
Guido van Rossumda5b8f22007-06-12 23:30:11 +00002994 if (fp == NULL)
2995 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002996 return fp;
2997}
2998
Guido van Rossum79f25d91997-04-29 20:08:16 +00002999static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003000imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003001{
3002 char *name;
3003 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003004 PyObject *fob = NULL;
3005 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003006 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003007 if (!PyArg_ParseTuple(args, "ss|O:load_compiled",
3008 &name, &pathname, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003009 return NULL;
3010 fp = get_file(pathname, fob, "rb");
3011 if (fp == NULL)
3012 return NULL;
3013 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003014 if (fob == NULL)
3015 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003016 return m;
3017}
3018
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003019#ifdef HAVE_DYNAMIC_LOADING
3020
Guido van Rossum79f25d91997-04-29 20:08:16 +00003021static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003022imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003023{
3024 char *name;
3025 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003026 PyObject *fob = NULL;
3027 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00003028 FILE *fp = NULL;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003029 if (!PyArg_ParseTuple(args, "ss|O:load_dynamic",
3030 &name, &pathname, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003031 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003032 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00003033 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003034 if (fp == NULL)
3035 return NULL;
3036 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003037 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00003038 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003039}
3040
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003041#endif /* HAVE_DYNAMIC_LOADING */
3042
Guido van Rossum79f25d91997-04-29 20:08:16 +00003043static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003044imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003045{
3046 char *name;
3047 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003048 PyObject *fob = NULL;
3049 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003050 FILE *fp;
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003051 if (!PyArg_ParseTuple(args, "ss|O:load_source",
3052 &name, &pathname, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003053 return NULL;
3054 fp = get_file(pathname, fob, "r");
3055 if (fp == NULL)
3056 return NULL;
3057 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003058 if (fob == NULL)
3059 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003060 return m;
3061}
3062
Guido van Rossum79f25d91997-04-29 20:08:16 +00003063static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003064imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003065{
3066 char *name;
3067 PyObject *fob;
3068 char *pathname;
3069 char *suffix; /* Unused */
3070 char *mode;
3071 int type;
3072 FILE *fp;
3073
Guido van Rossum43713e52000-02-29 13:59:29 +00003074 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003075 &name, &fob, &pathname,
3076 &suffix, &mode, &type))
3077 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003078 if (*mode) {
3079 /* Mode must start with 'r' or 'U' and must not contain '+'.
3080 Implicit in this test is the assumption that the mode
3081 may contain other modifiers like 'b' or 't'. */
3082
3083 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00003084 PyErr_Format(PyExc_ValueError,
3085 "invalid file open mode %.200s", mode);
3086 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003087 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003088 }
3089 if (fob == Py_None)
3090 fp = NULL;
3091 else {
Guido van Rossumda5b8f22007-06-12 23:30:11 +00003092 fp = get_file(NULL, fob, mode);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003093 if (fp == NULL)
3094 return NULL;
3095 }
Just van Rossum52e14d62002-12-30 22:08:05 +00003096 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003097}
3098
3099static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003100imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003101{
3102 char *name;
3103 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00003104 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003105 return NULL;
3106 return load_package(name, pathname);
3107}
3108
3109static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003110imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003111{
3112 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00003113 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003114 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003115 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003116}
3117
Christian Heimes13a7a212008-01-07 17:13:09 +00003118static PyObject *
3119imp_reload(PyObject *self, PyObject *v)
3120{
3121 return PyImport_ReloadModule(v);
3122}
3123
3124PyDoc_STRVAR(doc_reload,
3125"reload(module) -> module\n\
3126\n\
3127Reload the module. The module must have been successfully imported before.");
3128
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003129/* Doc strings */
3130
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003131PyDoc_STRVAR(doc_imp,
3132"This module provides the components needed to build your own\n\
3133__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003134
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003135PyDoc_STRVAR(doc_find_module,
3136"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003137Search for a module. If path is omitted or None, search for a\n\
3138built-in, frozen or special module and continue search in sys.path.\n\
3139The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003140package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003141
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003142PyDoc_STRVAR(doc_load_module,
3143"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003144Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003145The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003146
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003147PyDoc_STRVAR(doc_get_magic,
3148"get_magic() -> string\n\
3149Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003150
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003151PyDoc_STRVAR(doc_get_suffixes,
3152"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003153Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003154that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003156PyDoc_STRVAR(doc_new_module,
3157"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003158Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003159The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003160
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003161PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00003162"lock_held() -> boolean\n\
3163Return True if the import lock is currently held, else False.\n\
3164On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00003165
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003166PyDoc_STRVAR(doc_acquire_lock,
3167"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00003168Acquires the interpreter's import lock for the current thread.\n\
3169This lock should be used by import hooks to ensure thread-safety\n\
3170when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003171On platforms without threads, this function does nothing.");
3172
3173PyDoc_STRVAR(doc_release_lock,
3174"release_lock() -> None\n\
3175Release the interpreter's import lock.\n\
3176On platforms without threads, this function does nothing.");
3177
Guido van Rossum79f25d91997-04-29 20:08:16 +00003178static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00003179 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3180 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3181 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3182 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3183 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3184 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3185 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3186 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Christian Heimes13a7a212008-01-07 17:13:09 +00003187 {"reload", imp_reload, METH_O, doc_reload},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003188 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00003189 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
3190 {"init_builtin", imp_init_builtin, METH_VARARGS},
3191 {"init_frozen", imp_init_frozen, METH_VARARGS},
3192 {"is_builtin", imp_is_builtin, METH_VARARGS},
3193 {"is_frozen", imp_is_frozen, METH_VARARGS},
3194 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003195#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00003196 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003197#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00003198 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00003199 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003200 {NULL, NULL} /* sentinel */
3201};
3202
Guido van Rossum1a8791e1998-08-04 22:46:29 +00003203static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003204setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003205{
3206 PyObject *v;
3207 int err;
3208
Christian Heimes217cfd12007-12-02 14:31:20 +00003209 v = PyLong_FromLong((long)value);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003210 err = PyDict_SetItemString(d, name, v);
3211 Py_XDECREF(v);
3212 return err;
3213}
3214
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003215typedef struct {
3216 PyObject_HEAD
3217} NullImporter;
3218
3219static int
3220NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3221{
3222 char *path;
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003223 Py_ssize_t pathlen;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003224
3225 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3226 return -1;
3227
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00003228 if (!PyArg_ParseTuple(args, "es:NullImporter",
3229 Py_FileSystemDefaultEncoding, &path))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003230 return -1;
3231
Christian Heimes7d3bc0a2007-11-07 17:26:16 +00003232 pathlen = strlen(path);
3233 if (pathlen == 0) {
Georg Brandl8494d572008-07-19 10:13:15 +00003234 PyMem_Free(path);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003235 PyErr_SetString(PyExc_ImportError, "empty pathname");
3236 return -1;
3237 } else {
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003238#ifndef MS_WINDOWS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003239 struct stat statbuf;
3240 int rv;
3241
3242 rv = stat(path, &statbuf);
Georg Brandl8494d572008-07-19 10:13:15 +00003243 PyMem_Free(path);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003244 if (rv == 0) {
3245 /* it exists */
3246 if (S_ISDIR(statbuf.st_mode)) {
3247 /* it's a directory */
3248 PyErr_SetString(PyExc_ImportError,
3249 "existing directory");
3250 return -1;
3251 }
3252 }
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003253#else /* MS_WINDOWS */
3254 DWORD rv;
3255 /* see issue1293 and issue3677:
3256 * stat() on Windows doesn't recognise paths like
3257 * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
3258 */
3259 rv = GetFileAttributesA(path);
Kristján Valur Jónsson92cb4382009-01-24 10:33:25 +00003260 PyMem_Free(path);
Hirokazu Yamamoto21cbf5f2009-01-23 07:23:03 +00003261 if (rv != INVALID_FILE_ATTRIBUTES) {
3262 /* it exists */
3263 if (rv & FILE_ATTRIBUTE_DIRECTORY) {
3264 /* it's a directory */
3265 PyErr_SetString(PyExc_ImportError,
3266 "existing directory");
3267 return -1;
3268 }
3269 }
3270#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003271 }
3272 return 0;
3273}
3274
3275static PyObject *
3276NullImporter_find_module(NullImporter *self, PyObject *args)
3277{
3278 Py_RETURN_NONE;
3279}
3280
3281static PyMethodDef NullImporter_methods[] = {
3282 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3283 "Always return None"
3284 },
3285 {NULL} /* Sentinel */
3286};
3287
3288
Christian Heimes9cd17752007-11-18 19:35:23 +00003289PyTypeObject PyNullImporter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003290 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003291 "imp.NullImporter", /*tp_name*/
3292 sizeof(NullImporter), /*tp_basicsize*/
3293 0, /*tp_itemsize*/
3294 0, /*tp_dealloc*/
3295 0, /*tp_print*/
3296 0, /*tp_getattr*/
3297 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00003298 0, /*tp_reserved*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003299 0, /*tp_repr*/
3300 0, /*tp_as_number*/
3301 0, /*tp_as_sequence*/
3302 0, /*tp_as_mapping*/
3303 0, /*tp_hash */
3304 0, /*tp_call*/
3305 0, /*tp_str*/
3306 0, /*tp_getattro*/
3307 0, /*tp_setattro*/
3308 0, /*tp_as_buffer*/
3309 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3310 "Null importer object", /* tp_doc */
3311 0, /* tp_traverse */
3312 0, /* tp_clear */
3313 0, /* tp_richcompare */
3314 0, /* tp_weaklistoffset */
3315 0, /* tp_iter */
3316 0, /* tp_iternext */
3317 NullImporter_methods, /* tp_methods */
3318 0, /* tp_members */
3319 0, /* tp_getset */
3320 0, /* tp_base */
3321 0, /* tp_dict */
3322 0, /* tp_descr_get */
3323 0, /* tp_descr_set */
3324 0, /* tp_dictoffset */
3325 (initproc)NullImporter_init, /* tp_init */
3326 0, /* tp_alloc */
3327 PyType_GenericNew /* tp_new */
3328};
3329
Martin v. Löwis1a214512008-06-11 05:26:20 +00003330static struct PyModuleDef impmodule = {
3331 PyModuleDef_HEAD_INIT,
3332 "imp",
3333 doc_imp,
3334 0,
3335 imp_methods,
3336 NULL,
3337 NULL,
3338 NULL,
3339 NULL
3340};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003341
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003342PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003343PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003344{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003345 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003346
Christian Heimes9cd17752007-11-18 19:35:23 +00003347 if (PyType_Ready(&PyNullImporter_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00003348 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003349
Martin v. Löwis1a214512008-06-11 05:26:20 +00003350 m = PyModule_Create(&impmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003351 if (m == NULL)
3352 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003353 d = PyModule_GetDict(m);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003354 if (d == NULL)
3355 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003356
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003357 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3358 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3359 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3360 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3361 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3362 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3363 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3364 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003365 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003366 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003367
Christian Heimes9cd17752007-11-18 19:35:23 +00003368 Py_INCREF(&PyNullImporter_Type);
3369 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
Martin v. Löwis1a214512008-06-11 05:26:20 +00003370 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003371 failure:
Martin v. Löwis1a214512008-06-11 05:26:20 +00003372 Py_XDECREF(m);
3373 return NULL;
3374
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003375}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003376
3377
Guido van Rossumb18618d2000-05-03 23:44:39 +00003378/* API for embedding applications that want to add their own entries
3379 to the table of built-in modules. This should normally be called
3380 *before* Py_Initialize(). When the table resize fails, -1 is
3381 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003382
3383 After a similar function by Just van Rossum. */
3384
3385int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003386PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003387{
3388 static struct _inittab *our_copy = NULL;
3389 struct _inittab *p;
3390 int i, n;
3391
3392 /* Count the number of entries in both tables */
3393 for (n = 0; newtab[n].name != NULL; n++)
3394 ;
3395 if (n == 0)
3396 return 0; /* Nothing to do */
3397 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3398 ;
3399
3400 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003401 p = our_copy;
3402 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003403 if (p == NULL)
3404 return -1;
3405
3406 /* Copy the tables into the new memory */
3407 if (our_copy != PyImport_Inittab)
3408 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3409 PyImport_Inittab = our_copy = p;
3410 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3411
3412 return 0;
3413}
3414
3415/* Shorthand to add a single entry given a name and a function */
3416
3417int
Martin v. Löwis1a214512008-06-11 05:26:20 +00003418PyImport_AppendInittab(char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003419{
3420 struct _inittab newtab[2];
3421
3422 memset(newtab, '\0', sizeof newtab);
3423
3424 newtab[0].name = name;
3425 newtab[0].initfunc = initfunc;
3426
3427 return PyImport_ExtendInittab(newtab);
3428}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003429
3430#ifdef __cplusplus
3431}
3432#endif