blob: 88aced06d102799a2ddf7a6d1a30eaa11a3880f8 [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"
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +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
Anthony Baxterac6bd462006-04-13 02:06:09 +000021#ifdef __cplusplus
22extern "C" {
23#endif
Guido van Rossum55a83382000-09-20 20:31:38 +000024
Christian Heimes5e8e6d22008-02-23 23:59:45 +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
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000038 The magic numbers must be spaced apart atleast 2 values, as the
39 -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
Skip Montanaro4ec3c262006-03-25 14:12:03 +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)
Neal Norwitz0d62a062006-07-30 06:53:31 +000067 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
68 Python 2.5b3: 62111 (fix wrong code: x += yield)
Neal Norwitz9a70f952006-08-04 05:12:19 +000069 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
70 storing constants that should have been removed)
Neal Norwitzdac090d2006-09-05 03:53:08 +000071 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
Raymond Hettingereffde122007-12-18 18:26:18 +000072 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
Nick Coghlan7af53be2008-03-07 14:13:28 +000073 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
Antoine Pitroud0c35152008-12-17 00:38:28 +000074 Python 2.7a0: 62171 (optimize list comprehensions/change LIST_APPEND)
Jeffrey Yasskin68d68522009-02-28 19:03:21 +000075 Python 2.7a0: 62181 (optimize conditional branches:
76 introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
Benjamin Peterson1880d8b2009-05-25 13:13:44 +000077 Python 2.7a0 62191 (introduce SETUP_WITH)
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000078.
Tim Peters36515e22001-11-18 04:06:29 +000079*/
Benjamin Peterson1880d8b2009-05-25 13:13:44 +000080#define MAGIC (62191 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000081
Guido van Rossum96774c12000-05-01 20:19:08 +000082/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000083 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000084 compiler works which are enabled by command line switches. */
Christian Heimes61e45902008-03-27 10:35:52 +000085static long pyc_magic = MAGIC;
Guido van Rossum96774c12000-05-01 20:19:08 +000086
Guido van Rossum25ce5661997-08-02 03:10:38 +000087/* See _PyImport_FixupExtension() below */
88static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000089
Guido van Rossum771c6c81997-10-31 18:37:24 +000090/* This table is defined in config.c: */
91extern struct _inittab _PyImport_Inittab[];
92
93struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000094
Guido van Rossumed1170e1999-12-20 21:23:41 +000095/* these tables define the module suffixes that Python recognizes */
96struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000097
98#ifdef RISCOS
99static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +0000100 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +0000101 {"/pyc", "rb", PY_COMPILED},
102 {0, 0}
103};
104#else
Guido van Rossumed1170e1999-12-20 21:23:41 +0000105static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +0000106 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000107#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +0000108 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000109#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000110 {".pyc", "rb", PY_COMPILED},
111 {0, 0}
112};
Guido van Rossum48a680c2001-03-02 06:34:14 +0000113#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000114
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000115
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000116/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117
118void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000119_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000121 const struct filedescr *scan;
122 struct filedescr *filetab;
123 int countD = 0;
124 int countS = 0;
125
126 /* prepare _PyImport_Filetab: copy entries from
127 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
128 */
Georg Brandladd36e52007-08-23 18:08:06 +0000129#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000130 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
131 ++countD;
Georg Brandladd36e52007-08-23 18:08:06 +0000132#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000133 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
134 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000135 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Neal Norwitze1fdb322006-07-21 05:32:28 +0000136 if (filetab == NULL)
Neal Norwitz33722ae2006-07-21 07:59:02 +0000137 Py_FatalError("Can't initialize import file table.");
Georg Brandladd36e52007-08-23 18:08:06 +0000138#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000139 memcpy(filetab, _PyImport_DynLoadFiletab,
140 countD * sizeof(struct filedescr));
Georg Brandladd36e52007-08-23 18:08:06 +0000141#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000142 memcpy(filetab + countD, _PyImport_StandardFiletab,
143 countS * sizeof(struct filedescr));
144 filetab[countD + countS].suffix = NULL;
145
146 _PyImport_Filetab = filetab;
147
Guido van Rossum0824f631997-03-11 18:37:35 +0000148 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000149 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
150 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000151#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000152 if (strcmp(filetab->suffix, ".pyc") == 0)
153 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000154#else
155 if (strcmp(filetab->suffix, "/pyc") == 0)
156 filetab->suffix = "/pyo";
157#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000158 }
159 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000160
161 if (Py_UnicodeFlag) {
162 /* Fix the pyc_magic so that byte compiled code created
163 using the all-Unicode method doesn't interfere with
164 code created in normal operation mode. */
165 pyc_magic = MAGIC + 1;
166 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167}
168
Guido van Rossum25ce5661997-08-02 03:10:38 +0000169void
Just van Rossum52e14d62002-12-30 22:08:05 +0000170_PyImportHooks_Init(void)
171{
172 PyObject *v, *path_hooks = NULL, *zimpimport;
173 int err = 0;
174
175 /* adding sys.path_hooks and sys.path_importer_cache, setting up
176 zipimport */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000177 if (PyType_Ready(&PyNullImporter_Type) < 0)
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000178 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000179
180 if (Py_VerboseFlag)
181 PySys_WriteStderr("# installing zipimport hook\n");
182
183 v = PyList_New(0);
184 if (v == NULL)
185 goto error;
186 err = PySys_SetObject("meta_path", v);
187 Py_DECREF(v);
188 if (err)
189 goto error;
190 v = PyDict_New();
191 if (v == NULL)
192 goto error;
193 err = PySys_SetObject("path_importer_cache", v);
194 Py_DECREF(v);
195 if (err)
196 goto error;
197 path_hooks = PyList_New(0);
198 if (path_hooks == NULL)
199 goto error;
200 err = PySys_SetObject("path_hooks", path_hooks);
201 if (err) {
202 error:
203 PyErr_Print();
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000204 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
205 "path_importer_cache, or NullImporter failed"
206 );
Just van Rossum52e14d62002-12-30 22:08:05 +0000207 }
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000208
Just van Rossum52e14d62002-12-30 22:08:05 +0000209 zimpimport = PyImport_ImportModule("zipimport");
210 if (zimpimport == NULL) {
211 PyErr_Clear(); /* No zip import module -- okay */
212 if (Py_VerboseFlag)
213 PySys_WriteStderr("# can't import zipimport\n");
214 }
215 else {
216 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
217 "zipimporter");
218 Py_DECREF(zimpimport);
219 if (zipimporter == NULL) {
220 PyErr_Clear(); /* No zipimporter object -- okay */
221 if (Py_VerboseFlag)
222 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000223 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000224 }
225 else {
226 /* sys.path_hooks.append(zipimporter) */
227 err = PyList_Append(path_hooks, zipimporter);
228 Py_DECREF(zipimporter);
229 if (err)
230 goto error;
231 if (Py_VerboseFlag)
232 PySys_WriteStderr(
233 "# installed zipimport hook\n");
234 }
235 }
236 Py_DECREF(path_hooks);
237}
238
239void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000240_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000241{
242 Py_XDECREF(extensions);
243 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000244 PyMem_DEL(_PyImport_Filetab);
245 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000246}
247
248
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000249/* Locking primitives to prevent parallel imports of the same module
250 in different threads to return with a partially loaded module.
251 These calls are serialized by the global interpreter lock. */
252
253#ifdef WITH_THREAD
254
Guido van Rossum49b56061998-10-01 20:42:43 +0000255#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000256
Guido van Rossum65d5b571998-12-21 19:32:43 +0000257static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000258static long import_lock_thread = -1;
259static int import_lock_level = 0;
260
261static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000262lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000263{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000264 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000265 if (me == -1)
266 return; /* Too bad */
Neal Norwitze1fdb322006-07-21 05:32:28 +0000267 if (import_lock == NULL) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000268 import_lock = PyThread_allocate_lock();
Neal Norwitze1fdb322006-07-21 05:32:28 +0000269 if (import_lock == NULL)
270 return; /* Nothing much we can do. */
271 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000272 if (import_lock_thread == me) {
273 import_lock_level++;
274 return;
275 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000276 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
277 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000278 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000279 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000280 PyEval_RestoreThread(tstate);
281 }
282 import_lock_thread = me;
283 import_lock_level = 1;
284}
285
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000286static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000287unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000288{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000289 long me = PyThread_get_thread_ident();
Neal Norwitze1fdb322006-07-21 05:32:28 +0000290 if (me == -1 || import_lock == NULL)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000291 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000292 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000293 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000294 import_lock_level--;
295 if (import_lock_level == 0) {
296 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000297 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000298 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000299 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000300}
301
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000302/* This function is called from PyOS_AfterFork to ensure that newly
303 created child processes do not share locks with the parent. */
304
305void
306_PyImport_ReInitLock(void)
307{
308#ifdef _AIX
309 if (import_lock != NULL)
310 import_lock = PyThread_allocate_lock();
311#endif
312}
313
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000314#else
315
316#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000317#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000318
319#endif
320
Tim Peters69232342001-08-30 05:16:13 +0000321static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000322imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000323{
Tim Peters69232342001-08-30 05:16:13 +0000324#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000325 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000326#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000327 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000328#endif
329}
330
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000331static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000332imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000333{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000334#ifdef WITH_THREAD
335 lock_import();
336#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000337 Py_INCREF(Py_None);
338 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000339}
340
341static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000342imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000343{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000344#ifdef WITH_THREAD
345 if (unlock_import() < 0) {
346 PyErr_SetString(PyExc_RuntimeError,
347 "not holding the import lock");
348 return NULL;
349 }
350#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000351 Py_INCREF(Py_None);
352 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000353}
354
Collin Winter276887b2007-03-12 16:11:39 +0000355static void
Neal Norwitz75c7c802007-03-13 05:31:38 +0000356imp_modules_reloading_clear(void)
Collin Winter276887b2007-03-12 16:11:39 +0000357{
358 PyInterpreterState *interp = PyThreadState_Get()->interp;
Neal Norwitz75c7c802007-03-13 05:31:38 +0000359 if (interp->modules_reloading != NULL)
360 PyDict_Clear(interp->modules_reloading);
Collin Winter276887b2007-03-12 16:11:39 +0000361}
362
Guido van Rossum25ce5661997-08-02 03:10:38 +0000363/* Helper for sys */
364
365PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000366PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000367{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000368 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000369 if (interp->modules == NULL)
370 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
371 return interp->modules;
372}
373
Guido van Rossum3f5da241990-12-20 15:06:42 +0000374
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000375/* List of names to clear in sys */
376static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000377 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000378 "exc_type", "exc_value", "exc_traceback",
379 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000380 "path_hooks", "path_importer_cache", "meta_path",
Christian Heimes0d924432008-01-30 17:21:22 +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
414 value = PyDict_GetItemString(modules, "__builtin__");
415 if (value != NULL && PyModule_Check(value)) {
416 dict = PyModule_GetDict(value);
417 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000418 PySys_WriteStderr("# clear __builtin__._\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
Guido van Rossum758eec01998-01-19 21:58:26 +0000450 /* The special treatment of __builtin__ here is because even
451 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
454 references left to it), we need to delete the __builtin__
455 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
Guido van Rossum758eec01998-01-19 21:58:26 +0000465 one (skipping __builtin__ and sys) and delete them */
466 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;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000472 if (PyString_Check(key) && PyModule_Check(value)) {
473 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000474 if (strcmp(name, "__builtin__") == 0)
475 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
Guido van Rossum758eec01998-01-19 21:58:26 +0000488 /* Next, delete all modules (still skipping __builtin__ and sys) */
489 pos = 0;
490 while (PyDict_Next(modules, &pos, &key, &value)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000491 if (PyString_Check(key) && PyModule_Check(value)) {
492 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000493 if (strcmp(name, "__builtin__") == 0)
494 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
504 /* Next, delete sys and __builtin__ (in that order) */
505 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 }
512 value = PyDict_GetItemString(modules, "__builtin__");
513 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000514 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000515 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000516 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000517 PyDict_SetItemString(modules, "__builtin__", Py_None);
518 }
519
520 /* Finally, clear and delete the modules directory */
521 PyDict_Clear(modules);
522 interp->modules = NULL;
523 Py_DECREF(modules);
Collin Winter276887b2007-03-12 16:11:39 +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
545 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000546
Guido van Rossum79f25d91997-04-29 20:08:16 +0000547PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000548_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000549{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000550 PyObject *modules, *mod, *dict, *copy;
551 if (extensions == NULL) {
552 extensions = PyDict_New();
553 if (extensions == NULL)
554 return NULL;
555 }
556 modules = PyImport_GetModuleDict();
557 mod = PyDict_GetItemString(modules, name);
558 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000559 PyErr_Format(PyExc_SystemError,
560 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000561 return NULL;
562 }
563 dict = PyModule_GetDict(mod);
564 if (dict == NULL)
565 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000566 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000567 if (copy == NULL)
568 return NULL;
569 PyDict_SetItemString(extensions, filename, copy);
570 Py_DECREF(copy);
571 return copy;
572}
573
574PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000575_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000576{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000577 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000578 if (extensions == NULL)
579 return NULL;
580 dict = PyDict_GetItemString(extensions, filename);
581 if (dict == NULL)
582 return NULL;
583 mod = PyImport_AddModule(name);
584 if (mod == NULL)
585 return NULL;
586 mdict = PyModule_GetDict(mod);
587 if (mdict == NULL)
588 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000589 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000590 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000591 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000592 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000593 name, filename);
594 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595}
596
597
598/* Get the module object corresponding to a module name.
599 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000600 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000601 Because the former action is most common, THIS DOES NOT RETURN A
602 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000603
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000605PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000606{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000607 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000608 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000609
Guido van Rossum25ce5661997-08-02 03:10:38 +0000610 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000612 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000613 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000614 if (m == NULL)
615 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000616 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000617 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000618 return NULL;
619 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000620 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000621
622 return m;
623}
624
Tim Peters1cd70172004-08-02 03:52:12 +0000625/* Remove name from sys.modules, if it's there. */
626static void
627_RemoveModule(const char *name)
628{
629 PyObject *modules = PyImport_GetModuleDict();
630 if (PyDict_GetItemString(modules, name) == NULL)
631 return;
632 if (PyDict_DelItemString(modules, name) < 0)
633 Py_FatalError("import: deleting existing key in"
634 "sys.modules failed");
635}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000636
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000637/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000638 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
639 * removed from sys.modules, to avoid leaving damaged module objects
640 * in sys.modules. The caller may wish to restore the original
641 * module object (if any) in this case; PyImport_ReloadModule is an
642 * example.
643 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000645PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000646{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000647 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
648}
649
650PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000651PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000652{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000653 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655
Guido van Rossum79f25d91997-04-29 20:08:16 +0000656 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000657 if (m == NULL)
658 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000659 /* If the module is being reloaded, we get the old module back
660 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000661 d = PyModule_GetDict(m);
662 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
663 if (PyDict_SetItemString(d, "__builtins__",
664 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000665 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000666 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000667 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000668 v = NULL;
669 if (pathname != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000670 v = PyString_FromString(pathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000671 if (v == NULL)
672 PyErr_Clear();
673 }
674 if (v == NULL) {
675 v = ((PyCodeObject *)co)->co_filename;
676 Py_INCREF(v);
677 }
678 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000679 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000680 Py_DECREF(v);
681
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000682 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000683 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000684 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000685 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000686
Guido van Rossum25ce5661997-08-02 03:10:38 +0000687 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000688 PyErr_Format(PyExc_ImportError,
689 "Loaded module %.200s not found in sys.modules",
690 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000691 return NULL;
692 }
693
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000695
696 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000697
698 error:
699 _RemoveModule(name);
700 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000701}
702
703
704/* Given a pathname for a Python source file, fill a buffer with the
705 pathname for the corresponding compiled file. Return the pathname
706 for the compiled file, or NULL if there's no space in the buffer.
707 Doesn't set an exception. */
708
709static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000710make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000711{
Tim Petersc1731372001-08-04 08:12:36 +0000712 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000713 if (len+2 > buflen)
714 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000715
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000716#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000717 /* Treat .pyw as if it were .py. The case of ".pyw" must match
718 that used in _PyImport_StandardFiletab. */
719 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
720 --len; /* pretend 'w' isn't there */
721#endif
722 memcpy(buf, pathname, len);
723 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
724 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000725
726 return buf;
727}
728
729
730/* Given a pathname for a Python source file, its time of last
731 modification, and a pathname for a compiled file, check whether the
732 compiled file represents the same version of the source. If so,
733 return a FILE pointer for the compiled file, positioned just after
734 the header; if not, return NULL.
735 Doesn't set an exception. */
736
737static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000738check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000739{
740 FILE *fp;
741 long magic;
742 long pyc_mtime;
743
744 fp = fopen(cpathname, "rb");
745 if (fp == NULL)
746 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000748 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000749 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000750 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000751 fclose(fp);
752 return NULL;
753 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000754 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000755 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000756 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000757 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000758 fclose(fp);
759 return NULL;
760 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000761 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000762 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000763 return fp;
764}
765
766
767/* Read a code object from a file and check it for validity */
768
Guido van Rossum79f25d91997-04-29 20:08:16 +0000769static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000770read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000771{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000772 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000773
Tim Petersd9b9ac82001-01-28 00:27:39 +0000774 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000775 if (co == NULL)
776 return NULL;
777 if (!PyCode_Check(co)) {
778 PyErr_Format(PyExc_ImportError,
779 "Non-code object in %.200s", cpathname);
780 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000781 return NULL;
782 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000783 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000784}
785
786
787/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000788 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000789
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000791load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000792{
793 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 PyCodeObject *co;
795 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000796
Guido van Rossum79f25d91997-04-29 20:08:16 +0000797 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000798 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000799 PyErr_Format(PyExc_ImportError,
800 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000801 return NULL;
802 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000803 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000804 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000805 if (co == NULL)
806 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000807 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000808 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000809 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000810 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000811 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000812
813 return m;
814}
815
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000816/* Parse a source file and return the corresponding code object */
817
Guido van Rossum79f25d91997-04-29 20:08:16 +0000818static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000820{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 PyCodeObject *co = NULL;
822 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +0000823 PyCompilerFlags flags;
Neal Norwitz2a399b02006-09-11 04:28:16 +0000824 PyArena *arena = PyArena_New();
825 if (arena == NULL)
826 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000827
Christian Heimes7f23d862008-03-26 22:51:58 +0000828 flags.cf_flags = 0;
829
Christian Heimes3c608332008-03-26 22:01:37 +0000830 mod = PyParser_ASTFromFile(fp, pathname, Py_file_input, 0, 0, &flags,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000831 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000833 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 }
Neal Norwitz2a399b02006-09-11 04:28:16 +0000835 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000836 return co;
837}
838
839
Guido van Rossum55a83382000-09-20 20:31:38 +0000840/* Helper to open a bytecode file for writing in exclusive mode */
841
842static FILE *
Christian Heimes40346852008-02-23 17:52:07 +0000843open_exclusive(char *filename, mode_t mode)
Guido van Rossum55a83382000-09-20 20:31:38 +0000844{
845#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
846 /* Use O_EXCL to avoid a race condition when another process tries to
847 write the same file. When that happens, our open() call fails,
848 which is just fine (since it's only a cache).
849 XXX If the file exists and is writable but the directory is not
850 writable, the file will never be written. Oh well.
851 */
852 int fd;
853 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000854 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
855#ifdef O_BINARY
856 |O_BINARY /* necessary for Windows */
857#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000858#ifdef __VMS
Christian Heimes40346852008-02-23 17:52:07 +0000859 , mode, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000860#else
Christian Heimes40346852008-02-23 17:52:07 +0000861 , mode
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000862#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000863 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000864 if (fd < 0)
865 return NULL;
866 return fdopen(fd, "wb");
867#else
868 /* Best we can do -- on Windows this can't happen anyway */
869 return fopen(filename, "wb");
870#endif
871}
872
873
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000874/* Write a compiled module to a file, placing the time of last
875 modification of its source into the header.
876 Errors are ignored, if a write error occurs an attempt is made to
877 remove the file. */
878
879static void
Christian Heimes40346852008-02-23 17:52:07 +0000880write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000881{
882 FILE *fp;
Christian Heimes40346852008-02-23 17:52:07 +0000883 time_t mtime = srcstat->st_mtime;
884 mode_t mode = srcstat->st_mode;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000885
Christian Heimes40346852008-02-23 17:52:07 +0000886 fp = open_exclusive(cpathname, mode);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000887 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000888 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000889 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000890 "# can't create %s\n", cpathname);
891 return;
892 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000893 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000894 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000895 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
896 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000897 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000898 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000899 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000900 /* Don't keep partial file */
901 fclose(fp);
902 (void) unlink(cpathname);
903 return;
904 }
905 /* Now write the true mtime */
906 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000907 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000908 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000909 fflush(fp);
910 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000911 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000912 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000913}
914
Antoine Pitroue96d4ea2009-01-06 18:10:47 +0000915static void
916update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
917{
918 PyObject *constants, *tmp;
919 Py_ssize_t i, n;
920
921 if (!_PyString_Eq(co->co_filename, oldname))
922 return;
923
924 tmp = co->co_filename;
925 co->co_filename = newname;
926 Py_INCREF(co->co_filename);
927 Py_DECREF(tmp);
928
929 constants = co->co_consts;
930 n = PyTuple_GET_SIZE(constants);
931 for (i = 0; i < n; i++) {
932 tmp = PyTuple_GET_ITEM(constants, i);
933 if (PyCode_Check(tmp))
934 update_code_filenames((PyCodeObject *)tmp,
935 oldname, newname);
936 }
937}
938
939static int
940update_compiled_module(PyCodeObject *co, char *pathname)
941{
942 PyObject *oldname, *newname;
943
944 if (strcmp(PyString_AsString(co->co_filename), pathname) == 0)
945 return 0;
946
947 newname = PyString_FromString(pathname);
948 if (newname == NULL)
949 return -1;
950
951 oldname = co->co_filename;
952 Py_INCREF(oldname);
953 update_code_filenames(co, oldname, newname);
954 Py_DECREF(oldname);
955 Py_DECREF(newname);
956 return 1;
957}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000958
959/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000960 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
961 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000962
Guido van Rossum79f25d91997-04-29 20:08:16 +0000963static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000964load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000965{
Christian Heimes40346852008-02-23 17:52:07 +0000966 struct stat st;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000967 FILE *fpc;
968 char buf[MAXPATHLEN+1];
969 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 PyCodeObject *co;
971 PyObject *m;
Christian Heimes40346852008-02-23 17:52:07 +0000972
973 if (fstat(fileno(fp), &st) != 0) {
Neal Norwitz708e51a2005-10-03 04:48:15 +0000974 PyErr_Format(PyExc_RuntimeError,
Christian Heimes40346852008-02-23 17:52:07 +0000975 "unable to get file status from '%s'",
Neal Norwitz708e51a2005-10-03 04:48:15 +0000976 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +0000977 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +0000978 }
Fred Drake4c82b232000-06-30 16:18:57 +0000979#if SIZEOF_TIME_T > 4
980 /* Python's .pyc timestamp handling presumes that the timestamp fits
981 in 4 bytes. This will be fine until sometime in the year 2038,
982 when a 4-byte signed time_t will overflow.
983 */
Christian Heimes40346852008-02-23 17:52:07 +0000984 if (st.st_mtime >> 32) {
Fred Drake4c82b232000-06-30 16:18:57 +0000985 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000986 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000987 return NULL;
988 }
989#endif
Tim Peters36515e22001-11-18 04:06:29 +0000990 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000991 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000992 if (cpathname != NULL &&
Christian Heimes40346852008-02-23 17:52:07 +0000993 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000994 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000995 fclose(fpc);
996 if (co == NULL)
997 return NULL;
Antoine Pitroue96d4ea2009-01-06 18:10:47 +0000998 if (update_compiled_module(co, pathname) < 0)
999 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001000 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001001 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001002 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001003 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001004 }
1005 else {
1006 co = parse_source_module(pathname, fp);
1007 if (co == NULL)
1008 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001009 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001010 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001011 name, pathname);
Georg Brandl2da0fce2008-01-07 17:09:35 +00001012 if (cpathname) {
1013 PyObject *ro = PySys_GetObject("dont_write_bytecode");
1014 if (ro == NULL || !PyObject_IsTrue(ro))
Christian Heimes40346852008-02-23 17:52:07 +00001015 write_compiled_module(co, cpathname, &st);
Georg Brandl2da0fce2008-01-07 17:09:35 +00001016 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001017 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +00001018 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001019 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001020
1021 return m;
1022}
1023
1024
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001025/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001026static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
1027static struct filedescr *find_module(char *, char *, PyObject *,
1028 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001029static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001030
1031/* Load a package and return its module object WITH INCREMENTED
1032 REFERENCE COUNT */
1033
1034static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001035load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001036{
Tim Peters1cd70172004-08-02 03:52:12 +00001037 PyObject *m, *d;
1038 PyObject *file = NULL;
1039 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001040 int err;
1041 char buf[MAXPATHLEN+1];
1042 FILE *fp = NULL;
1043 struct filedescr *fdp;
1044
1045 m = PyImport_AddModule(name);
1046 if (m == NULL)
1047 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001048 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001049 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001050 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001051 d = PyModule_GetDict(m);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001052 file = PyString_FromString(pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001053 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +00001054 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001055 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +00001056 if (path == NULL)
1057 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001058 err = PyDict_SetItemString(d, "__file__", file);
1059 if (err == 0)
1060 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +00001061 if (err != 0)
1062 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001063 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001064 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001065 if (fdp == NULL) {
1066 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1067 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +00001068 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001069 }
1070 else
1071 m = NULL;
1072 goto cleanup;
1073 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001074 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001075 if (fp != NULL)
1076 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001077 goto cleanup;
1078
1079 error:
1080 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001081 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001082 Py_XDECREF(path);
1083 Py_XDECREF(file);
1084 return m;
1085}
1086
1087
1088/* Helper to test for built-in module */
1089
1090static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001091is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001092{
1093 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001094 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1095 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1096 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001097 return -1;
1098 else
1099 return 1;
1100 }
1101 }
1102 return 0;
1103}
1104
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001105
Just van Rossum52e14d62002-12-30 22:08:05 +00001106/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1107 possibly by fetching it from the path_importer_cache dict. If it
Brett Cannon94b69f62006-09-28 22:10:14 +00001108 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001109 that can handle the path item. Return None if no hook could;
1110 this tells our caller it should fall back to the builtin
1111 import mechanism. Cache the result in path_importer_cache.
1112 Returns a borrowed reference. */
1113
1114static PyObject *
1115get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1116 PyObject *p)
1117{
1118 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001119 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001120
1121 /* These conditions are the caller's responsibility: */
1122 assert(PyList_Check(path_hooks));
1123 assert(PyDict_Check(path_importer_cache));
1124
1125 nhooks = PyList_Size(path_hooks);
1126 if (nhooks < 0)
1127 return NULL; /* Shouldn't happen */
1128
1129 importer = PyDict_GetItem(path_importer_cache, p);
1130 if (importer != NULL)
1131 return importer;
1132
1133 /* set path_importer_cache[p] to None to avoid recursion */
1134 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1135 return NULL;
1136
1137 for (j = 0; j < nhooks; j++) {
1138 PyObject *hook = PyList_GetItem(path_hooks, j);
1139 if (hook == NULL)
1140 return NULL;
Georg Brandl684fd0c2006-05-25 19:15:31 +00001141 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001142 if (importer != NULL)
1143 break;
1144
1145 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1146 return NULL;
1147 }
1148 PyErr_Clear();
1149 }
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00001150 if (importer == NULL) {
1151 importer = PyObject_CallFunctionObjArgs(
Nick Coghlan327a39b2007-11-18 11:56:28 +00001152 (PyObject *)&PyNullImporter_Type, p, NULL
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00001153 );
1154 if (importer == NULL) {
1155 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1156 PyErr_Clear();
1157 return Py_None;
1158 }
1159 }
1160 }
1161 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001162 int err = PyDict_SetItem(path_importer_cache, p, importer);
1163 Py_DECREF(importer);
1164 if (err != 0)
1165 return NULL;
1166 }
1167 return importer;
1168}
1169
Nick Coghlan327a39b2007-11-18 11:56:28 +00001170PyAPI_FUNC(PyObject *)
1171PyImport_GetImporter(PyObject *path) {
1172 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
1173
1174 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1175 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1176 importer = get_path_importer(path_importer_cache,
1177 path_hooks, path);
1178 }
1179 }
1180 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1181 return importer;
1182}
1183
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001184/* Search the path (default sys.path) for a module. Return the
1185 corresponding filedescr struct, and (via return arguments) the
1186 pathname and an open file. Return NULL if the module is not found. */
1187
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001188#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001189extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001190 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001191#endif
1192
Martin v. Löwis18e16552006-02-15 17:27:45 +00001193static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001194static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001195static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001196
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001197static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001198find_module(char *fullname, char *subname, PyObject *path, char *buf,
1199 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001200{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001201 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001202 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001203 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001204 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001205 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001206 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001207#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001208 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001209#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001210 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1211 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1212 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001213 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001214#if defined(PYOS_OS2)
1215 size_t saved_len;
1216 size_t saved_namelen;
1217 char *saved_buf = NULL;
1218#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001219 if (p_loader != NULL)
1220 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001221
Just van Rossum52e14d62002-12-30 22:08:05 +00001222 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001223 PyErr_SetString(PyExc_OverflowError,
1224 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001225 return NULL;
1226 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001227 strcpy(name, subname);
1228
1229 /* sys.meta_path import hook */
1230 if (p_loader != NULL) {
1231 PyObject *meta_path;
1232
1233 meta_path = PySys_GetObject("meta_path");
1234 if (meta_path == NULL || !PyList_Check(meta_path)) {
1235 PyErr_SetString(PyExc_ImportError,
1236 "sys.meta_path must be a list of "
1237 "import hooks");
1238 return NULL;
1239 }
1240 Py_INCREF(meta_path); /* zap guard */
1241 npath = PyList_Size(meta_path);
1242 for (i = 0; i < npath; i++) {
1243 PyObject *loader;
1244 PyObject *hook = PyList_GetItem(meta_path, i);
1245 loader = PyObject_CallMethod(hook, "find_module",
1246 "sO", fullname,
1247 path != NULL ?
1248 path : Py_None);
1249 if (loader == NULL) {
1250 Py_DECREF(meta_path);
1251 return NULL; /* true error */
1252 }
1253 if (loader != Py_None) {
1254 /* a loader was found */
1255 *p_loader = loader;
1256 Py_DECREF(meta_path);
1257 return &importhookdescr;
1258 }
1259 Py_DECREF(loader);
1260 }
1261 Py_DECREF(meta_path);
1262 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001263
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001264 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001265 /* The only type of submodule allowed inside a "frozen"
1266 package are other frozen modules or packages. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001267 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
Guido van Rossum0506a431998-08-11 15:07:39 +00001268 PyErr_SetString(PyExc_ImportError,
1269 "full frozen module name too long");
1270 return NULL;
1271 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001272 strcpy(buf, PyString_AsString(path));
Guido van Rossum0506a431998-08-11 15:07:39 +00001273 strcat(buf, ".");
1274 strcat(buf, name);
1275 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001276 if (find_frozen(name) != NULL) {
1277 strcpy(buf, name);
1278 return &fd_frozen;
1279 }
1280 PyErr_Format(PyExc_ImportError,
1281 "No frozen submodule named %.200s", name);
1282 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001283 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001284 if (path == NULL) {
1285 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001286 strcpy(buf, name);
1287 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001288 }
Greg Ward201baee2001-10-04 14:52:06 +00001289 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001290 strcpy(buf, name);
1291 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001292 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001293
Guido van Rossumac279101996-08-22 23:10:58 +00001294#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001295 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1296 if (fp != NULL) {
1297 *p_fp = fp;
1298 return fdp;
1299 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001300#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001301 path = PySys_GetObject("path");
1302 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001303 if (path == NULL || !PyList_Check(path)) {
1304 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001305 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001306 return NULL;
1307 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001308
1309 path_hooks = PySys_GetObject("path_hooks");
1310 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1311 PyErr_SetString(PyExc_ImportError,
1312 "sys.path_hooks must be a list of "
1313 "import hooks");
1314 return NULL;
1315 }
1316 path_importer_cache = PySys_GetObject("path_importer_cache");
1317 if (path_importer_cache == NULL ||
1318 !PyDict_Check(path_importer_cache)) {
1319 PyErr_SetString(PyExc_ImportError,
1320 "sys.path_importer_cache must be a dict");
1321 return NULL;
1322 }
1323
Guido van Rossum79f25d91997-04-29 20:08:16 +00001324 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001325 namelen = strlen(name);
1326 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001327 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001328 PyObject *v = PyList_GetItem(path, i);
Neal Norwitz3cb31ac2006-08-13 18:10:47 +00001329 if (!v)
1330 return NULL;
Walter Dörwald3430d702002-06-17 10:43:59 +00001331#ifdef Py_USING_UNICODE
1332 if (PyUnicode_Check(v)) {
1333 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1334 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1335 if (copy == NULL)
1336 return NULL;
1337 v = copy;
1338 }
1339 else
1340#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001341 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001342 continue;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001343 len = PyString_GET_SIZE(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001344 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1345 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001346 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001347 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001348 strcpy(buf, PyString_AS_STRING(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001349 if (strlen(buf) != len) {
1350 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001351 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001352 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001353
1354 /* sys.path_hooks import hook */
1355 if (p_loader != NULL) {
1356 PyObject *importer;
1357
1358 importer = get_path_importer(path_importer_cache,
1359 path_hooks, v);
Neal Norwitza4df11d2006-07-06 04:28:59 +00001360 if (importer == NULL) {
1361 Py_XDECREF(copy);
Just van Rossum52e14d62002-12-30 22:08:05 +00001362 return NULL;
Neal Norwitza4df11d2006-07-06 04:28:59 +00001363 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001364 /* Note: importer is a borrowed reference */
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00001365 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001366 PyObject *loader;
1367 loader = PyObject_CallMethod(importer,
1368 "find_module",
1369 "s", fullname);
Neal Norwitza4df11d2006-07-06 04:28:59 +00001370 Py_XDECREF(copy);
Just van Rossum52e14d62002-12-30 22:08:05 +00001371 if (loader == NULL)
1372 return NULL; /* error */
1373 if (loader != Py_None) {
1374 /* a loader was found */
1375 *p_loader = loader;
1376 return &importhookdescr;
1377 }
1378 Py_DECREF(loader);
Georg Brandlf4ef1162006-05-26 18:03:31 +00001379 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001380 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001381 }
Georg Brandlf4ef1162006-05-26 18:03:31 +00001382 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001383
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001384 if (len > 0 && buf[len-1] != SEP
1385#ifdef ALTSEP
1386 && buf[len-1] != ALTSEP
1387#endif
1388 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001389 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001390 strcpy(buf+len, name);
1391 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001392
1393 /* Check for package import (buf holds a directory name,
1394 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001395#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001396 if (stat(buf, &statbuf) == 0 && /* it exists */
1397 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001398 case_ok(buf, len, namelen, name)) { /* case matches */
1399 if (find_init_module(buf)) { /* and has __init__.py */
1400 Py_XDECREF(copy);
1401 return &fd_package;
1402 }
1403 else {
1404 char warnstr[MAXPATHLEN+80];
1405 sprintf(warnstr, "Not importing directory "
1406 "'%.*s': missing __init__.py",
1407 MAXPATHLEN, buf);
1408 if (PyErr_Warn(PyExc_ImportWarning,
1409 warnstr)) {
1410 Py_XDECREF(copy);
1411 return NULL;
1412 }
1413 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001414 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001415#else
1416 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001417#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001418 if (isdir(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001419 case_ok(buf, len, namelen, name)) {
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001420 if (find_init_module(buf)) {
1421 Py_XDECREF(copy);
1422 return &fd_package;
1423 }
1424 else {
1425 char warnstr[MAXPATHLEN+80];
1426 sprintf(warnstr, "Not importing directory "
1427 "'%.*s': missing __init__.py",
1428 MAXPATHLEN, buf);
1429 if (PyErr_Warn(PyExc_ImportWarning,
1430 warnstr)) {
1431 Py_XDECREF(copy);
1432 return NULL;
1433 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001434 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001435#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001436#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001437#if defined(PYOS_OS2)
1438 /* take a snapshot of the module spec for restoration
1439 * after the 8 character DLL hackery
1440 */
1441 saved_buf = strdup(buf);
1442 saved_len = len;
1443 saved_namelen = namelen;
1444#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001445 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Georg Brandladd36e52007-08-23 18:08:06 +00001446#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001447 /* OS/2 limits DLLs to 8 character names (w/o
1448 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001449 * so if the name is longer than that and its a
1450 * dynamically loaded module we're going to try,
1451 * truncate the name before trying
1452 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001453 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001454 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001455 const struct filedescr *scan;
1456 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001457 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001458 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001459 break;
1460 else
1461 scan++;
1462 }
1463 if (scan->suffix != NULL) {
1464 /* yes, so truncate the name */
1465 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001466 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001467 buf[len] = '\0';
1468 }
1469 }
1470#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001471 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001472 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001473 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001474 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001475 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001476 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001477 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001478 if (fp != NULL) {
1479 if (case_ok(buf, len, namelen, name))
1480 break;
1481 else { /* continue search */
1482 fclose(fp);
1483 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001484 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001485 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001486#if defined(PYOS_OS2)
1487 /* restore the saved snapshot */
1488 strcpy(buf, saved_buf);
1489 len = saved_len;
1490 namelen = saved_namelen;
1491#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001492 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001493#if defined(PYOS_OS2)
1494 /* don't need/want the module name snapshot anymore */
1495 if (saved_buf)
1496 {
1497 free(saved_buf);
1498 saved_buf = NULL;
1499 }
1500#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001501 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001502 if (fp != NULL)
1503 break;
1504 }
1505 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001506 PyErr_Format(PyExc_ImportError,
1507 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001508 return NULL;
1509 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001510 *p_fp = fp;
1511 return fdp;
1512}
1513
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001514/* Helpers for main.c
1515 * Find the source file corresponding to a named module
1516 */
1517struct filedescr *
1518_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1519 size_t buflen, FILE **p_fp, PyObject **p_loader)
1520{
1521 return find_module((char *) name, (char *) name, path,
1522 buf, buflen, p_fp, p_loader);
1523}
1524
1525PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1526{
1527 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1528}
1529
Martin v. Löwis18e16552006-02-15 17:27:45 +00001530/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001531 * The arguments here are tricky, best shown by example:
1532 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1533 * ^ ^ ^ ^
1534 * |--------------------- buf ---------------------|
1535 * |------------------- len ------------------|
1536 * |------ name -------|
1537 * |----- namelen -----|
1538 * buf is the full path, but len only counts up to (& exclusive of) the
1539 * extension. name is the module name, also exclusive of extension.
1540 *
1541 * We've already done a successful stat() or fopen() on buf, so know that
1542 * there's some match, possibly case-insensitive.
1543 *
Tim Peters50d8d372001-02-28 05:34:27 +00001544 * case_ok() is to return 1 if there's a case-sensitive match for
1545 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1546 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001547 *
Tim Peters50d8d372001-02-28 05:34:27 +00001548 * case_ok() is used to implement case-sensitive import semantics even
1549 * on platforms with case-insensitive filesystems. It's trivial to implement
1550 * for case-sensitive filesystems. It's pretty much a cross-platform
1551 * nightmare for systems with case-insensitive filesystems.
1552 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001553
Tim Peters50d8d372001-02-28 05:34:27 +00001554/* First we may need a pile of platform-specific header files; the sequence
1555 * of #if's here should match the sequence in the body of case_ok().
1556 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001557#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001558#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001559
Tim Peters50d8d372001-02-28 05:34:27 +00001560#elif defined(DJGPP)
1561#include <dir.h>
1562
Jason Tishler7961aa62005-05-20 00:56:54 +00001563#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001564#include <sys/types.h>
1565#include <dirent.h>
1566
Andrew MacIntyred9400542002-02-26 11:41:34 +00001567#elif defined(PYOS_OS2)
1568#define INCL_DOS
1569#define INCL_DOSERRORS
1570#define INCL_NOPMAPI
1571#include <os2.h>
1572
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001573#elif defined(RISCOS)
1574#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001575#endif
1576
Guido van Rossum0980bd91998-02-13 17:18:36 +00001577static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001578case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001579{
Tim Peters50d8d372001-02-28 05:34:27 +00001580/* Pick a platform-specific implementation; the sequence of #if's here should
1581 * match the sequence just above.
1582 */
1583
Jason Tishler7961aa62005-05-20 00:56:54 +00001584/* MS_WINDOWS */
1585#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001586 WIN32_FIND_DATA data;
1587 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001588
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001589 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001590 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001591
Guido van Rossum0980bd91998-02-13 17:18:36 +00001592 h = FindFirstFile(buf, &data);
1593 if (h == INVALID_HANDLE_VALUE) {
1594 PyErr_Format(PyExc_NameError,
1595 "Can't find file for module %.100s\n(filename %.300s)",
1596 name, buf);
1597 return 0;
1598 }
1599 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001600 return strncmp(data.cFileName, name, namelen) == 0;
1601
1602/* DJGPP */
1603#elif defined(DJGPP)
1604 struct ffblk ffblk;
1605 int done;
1606
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001607 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001608 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001609
1610 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1611 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001612 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001613 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001614 name, buf);
1615 return 0;
1616 }
Tim Peters50d8d372001-02-28 05:34:27 +00001617 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001618
Jason Tishler7961aa62005-05-20 00:56:54 +00001619/* new-fangled macintosh (macosx) or Cygwin */
1620#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001621 DIR *dirp;
1622 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001623 char dirname[MAXPATHLEN + 1];
1624 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001625
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001626 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001627 return 1;
1628
Tim Petersd1e87a82001-03-01 18:12:00 +00001629 /* Copy the dir component into dirname; substitute "." if empty */
1630 if (dirlen <= 0) {
1631 dirname[0] = '.';
1632 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001633 }
1634 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001635 assert(dirlen <= MAXPATHLEN);
1636 memcpy(dirname, buf, dirlen);
1637 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001638 }
1639 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001640 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001641 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001642 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001643 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001644 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001645#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001646 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001647#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001648 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001649#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001650 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001651 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001652 (void)closedir(dirp);
1653 return 1; /* Found */
1654 }
1655 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001656 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001657 }
Tim Peters430f5d42001-03-01 01:30:56 +00001658 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001659
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001660/* RISC OS */
1661#elif defined(RISCOS)
1662 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1663 char buf2[MAXPATHLEN+2];
1664 char *nameWithExt = buf+len-namelen;
1665 int canonlen;
1666 os_error *e;
1667
1668 if (Py_GETENV("PYTHONCASEOK") != NULL)
1669 return 1;
1670
1671 /* workaround:
1672 append wildcard, otherwise case of filename wouldn't be touched */
1673 strcpy(buf2, buf);
1674 strcat(buf2, "*");
1675
1676 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1677 canonlen = MAXPATHLEN+1-canonlen;
1678 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1679 return 0;
1680 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1681 return 1; /* match */
1682
1683 return 0;
1684
Andrew MacIntyred9400542002-02-26 11:41:34 +00001685/* OS/2 */
1686#elif defined(PYOS_OS2)
1687 HDIR hdir = 1;
1688 ULONG srchcnt = 1;
1689 FILEFINDBUF3 ffbuf;
1690 APIRET rc;
1691
Georg Brandlaed6c662008-01-07 17:25:53 +00001692 if (Py_GETENV("PYTHONCASEOK") != NULL)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001693 return 1;
1694
1695 rc = DosFindFirst(buf,
1696 &hdir,
1697 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1698 &ffbuf, sizeof(ffbuf),
1699 &srchcnt,
1700 FIL_STANDARD);
1701 if (rc != NO_ERROR)
1702 return 0;
1703 return strncmp(ffbuf.achName, name, namelen) == 0;
1704
Tim Peters50d8d372001-02-28 05:34:27 +00001705/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1706#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001707 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001708
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001709#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001710}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001711
Guido van Rossum0980bd91998-02-13 17:18:36 +00001712
Guido van Rossum197346f1997-10-31 18:38:52 +00001713#ifdef HAVE_STAT
1714/* Helper to look for __init__.py or __init__.py[co] in potential package */
1715static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001716find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001717{
Tim Peters0f9431f2001-07-05 03:47:53 +00001718 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001719 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001720 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001721 struct stat statbuf;
1722
Tim Peters0f9431f2001-07-05 03:47:53 +00001723/* For calling case_ok(buf, len, namelen, name):
1724 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1725 * ^ ^ ^ ^
1726 * |--------------------- buf ---------------------|
1727 * |------------------- len ------------------|
1728 * |------ name -------|
1729 * |----- namelen -----|
1730 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001731 if (save_len + 13 >= MAXPATHLEN)
1732 return 0;
1733 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001734 pname = buf + i;
1735 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001736 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001737 if (case_ok(buf,
1738 save_len + 9, /* len("/__init__") */
1739 8, /* len("__init__") */
1740 pname)) {
1741 buf[save_len] = '\0';
1742 return 1;
1743 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001744 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001745 i += strlen(pname);
1746 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001747 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001748 if (case_ok(buf,
1749 save_len + 9, /* len("/__init__") */
1750 8, /* len("__init__") */
1751 pname)) {
1752 buf[save_len] = '\0';
1753 return 1;
1754 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001755 }
1756 buf[save_len] = '\0';
1757 return 0;
1758}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001759
1760#else
1761
1762#ifdef RISCOS
1763static int
1764find_init_module(buf)
1765 char *buf;
1766{
1767 int save_len = strlen(buf);
1768 int i = save_len;
1769
1770 if (save_len + 13 >= MAXPATHLEN)
1771 return 0;
1772 buf[i++] = SEP;
1773 strcpy(buf+i, "__init__/py");
1774 if (isfile(buf)) {
1775 buf[save_len] = '\0';
1776 return 1;
1777 }
1778
1779 if (Py_OptimizeFlag)
1780 strcpy(buf+i, "o");
1781 else
1782 strcpy(buf+i, "c");
1783 if (isfile(buf)) {
1784 buf[save_len] = '\0';
1785 return 1;
1786 }
1787 buf[save_len] = '\0';
1788 return 0;
1789}
1790#endif /*RISCOS*/
1791
Guido van Rossum197346f1997-10-31 18:38:52 +00001792#endif /* HAVE_STAT */
1793
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001794
Tim Petersdbd9ba62000-07-09 03:09:57 +00001795static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001796
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001797/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001798 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001799
Guido van Rossum79f25d91997-04-29 20:08:16 +00001800static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001801load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001802{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001803 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001804 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001805 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001806
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001807 /* First check that there's an open file (if we need one) */
1808 switch (type) {
1809 case PY_SOURCE:
1810 case PY_COMPILED:
1811 if (fp == NULL) {
1812 PyErr_Format(PyExc_ValueError,
1813 "file object required for import (type code %d)",
1814 type);
1815 return NULL;
1816 }
1817 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001818
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001819 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001820
1821 case PY_SOURCE:
1822 m = load_source_module(name, buf, fp);
1823 break;
1824
1825 case PY_COMPILED:
1826 m = load_compiled_module(name, buf, fp);
1827 break;
1828
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001829#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001830 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001831 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001832 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001833#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001834
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001835 case PKG_DIRECTORY:
1836 m = load_package(name, buf);
1837 break;
1838
1839 case C_BUILTIN:
1840 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001841 if (buf != NULL && buf[0] != '\0')
1842 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001843 if (type == C_BUILTIN)
1844 err = init_builtin(name);
1845 else
1846 err = PyImport_ImportFrozenModule(name);
1847 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001848 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001849 if (err == 0) {
1850 PyErr_Format(PyExc_ImportError,
1851 "Purported %s module %.200s not found",
1852 type == C_BUILTIN ?
1853 "builtin" : "frozen",
1854 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001855 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001856 }
1857 modules = PyImport_GetModuleDict();
1858 m = PyDict_GetItemString(modules, name);
1859 if (m == NULL) {
1860 PyErr_Format(
1861 PyExc_ImportError,
1862 "%s module %.200s not properly initialized",
1863 type == C_BUILTIN ?
1864 "builtin" : "frozen",
1865 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001866 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001867 }
1868 Py_INCREF(m);
1869 break;
1870
Just van Rossum52e14d62002-12-30 22:08:05 +00001871 case IMP_HOOK: {
1872 if (loader == NULL) {
1873 PyErr_SetString(PyExc_ImportError,
1874 "import hook without loader");
1875 return NULL;
1876 }
1877 m = PyObject_CallMethod(loader, "load_module", "s", name);
1878 break;
1879 }
1880
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001881 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001882 PyErr_Format(PyExc_ImportError,
1883 "Don't know how to import %.200s (type code %d)",
1884 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001885 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001886
1887 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001888
1889 return m;
1890}
1891
1892
1893/* Initialize a built-in module.
Brett Cannon5a9aa4f2006-10-03 21:58:55 +00001894 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001895 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001896
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001897static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001898init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001899{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001900 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001901
Greg Ward201baee2001-10-04 14:52:06 +00001902 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001903 return 1;
1904
Guido van Rossum771c6c81997-10-31 18:37:24 +00001905 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001906 if (strcmp(name, p->name) == 0) {
1907 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001908 PyErr_Format(PyExc_ImportError,
1909 "Cannot re-init internal module %.200s",
1910 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001911 return -1;
1912 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001913 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001914 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001915 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001916 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001917 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001918 if (_PyImport_FixupExtension(name, name) == NULL)
1919 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001920 return 1;
1921 }
1922 }
1923 return 0;
1924}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001925
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001926
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001927/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001928
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001929static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001930find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001931{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001932 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001933
Guido van Rossum79f25d91997-04-29 20:08:16 +00001934 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001935 if (p->name == NULL)
1936 return NULL;
1937 if (strcmp(p->name, name) == 0)
1938 break;
1939 }
1940 return p;
1941}
1942
Guido van Rossum79f25d91997-04-29 20:08:16 +00001943static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001944get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001945{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001946 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001947 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001948
1949 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001950 PyErr_Format(PyExc_ImportError,
1951 "No such frozen object named %.200s",
1952 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001953 return NULL;
1954 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001955 if (p->code == NULL) {
1956 PyErr_Format(PyExc_ImportError,
1957 "Excluded frozen object named %.200s",
1958 name);
1959 return NULL;
1960 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001961 size = p->size;
1962 if (size < 0)
1963 size = -size;
1964 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001965}
1966
1967/* Initialize a frozen module.
1968 Return 1 for succes, 0 if the module is not found, and -1 with
1969 an exception set if the initialization failed.
1970 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001971
1972int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001973PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001974{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001975 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001976 PyObject *co;
1977 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001978 int ispackage;
1979 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001980
1981 if (p == NULL)
1982 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001983 if (p->code == NULL) {
1984 PyErr_Format(PyExc_ImportError,
1985 "Excluded frozen object named %.200s",
1986 name);
1987 return -1;
1988 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001989 size = p->size;
1990 ispackage = (size < 0);
1991 if (ispackage)
1992 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001993 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001994 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001995 name, ispackage ? " package" : "");
1996 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001997 if (co == NULL)
1998 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001999 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002000 PyErr_Format(PyExc_TypeError,
2001 "frozen object %.200s is not a code object",
2002 name);
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00002003 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002004 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00002005 if (ispackage) {
2006 /* Set __path__ to the package name */
2007 PyObject *d, *s;
2008 int err;
2009 m = PyImport_AddModule(name);
2010 if (m == NULL)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00002011 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002012 d = PyModule_GetDict(m);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002013 s = PyString_InternFromString(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00002014 if (s == NULL)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00002015 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002016 err = PyDict_SetItemString(d, "__path__", s);
2017 Py_DECREF(s);
2018 if (err != 0)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00002019 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00002020 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00002021 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002022 if (m == NULL)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00002023 goto err_return;
2024 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002025 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002026 return 1;
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00002027err_return:
2028 Py_DECREF(co);
2029 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002030}
Guido van Rossum74e6a111994-08-29 12:54:38 +00002031
2032
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002033/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002034 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00002035
Guido van Rossum79f25d91997-04-29 20:08:16 +00002036PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002037PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00002038{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002039 PyObject *pname;
2040 PyObject *result;
2041
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002042 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00002043 if (pname == NULL)
2044 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002045 result = PyImport_Import(pname);
2046 Py_DECREF(pname);
2047 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002048}
2049
Christian Heimes000a0742008-01-03 22:16:32 +00002050/* Import a module without blocking
2051 *
2052 * At first it tries to fetch the module from sys.modules. If the module was
2053 * never loaded before it loads it with PyImport_ImportModule() unless another
2054 * thread holds the import lock. In the latter case the function raises an
2055 * ImportError instead of blocking.
2056 *
2057 * Returns the module object with incremented ref count.
2058 */
2059PyObject *
2060PyImport_ImportModuleNoBlock(const char *name)
2061{
2062 PyObject *result;
2063 PyObject *modules;
2064 long me;
2065
2066 /* Try to get the module from sys.modules[name] */
2067 modules = PyImport_GetModuleDict();
2068 if (modules == NULL)
2069 return NULL;
2070
2071 result = PyDict_GetItemString(modules, name);
2072 if (result != NULL) {
2073 Py_INCREF(result);
2074 return result;
2075 }
2076 else {
2077 PyErr_Clear();
2078 }
Benjamin Peterson17f03ca2008-09-01 14:18:30 +00002079#ifdef WITH_THREAD
Christian Heimes000a0742008-01-03 22:16:32 +00002080 /* check the import lock
2081 * me might be -1 but I ignore the error here, the lock function
2082 * takes care of the problem */
2083 me = PyThread_get_thread_ident();
2084 if (import_lock_thread == -1 || import_lock_thread == me) {
2085 /* no thread or me is holding the lock */
2086 return PyImport_ImportModule(name);
2087 }
2088 else {
2089 PyErr_Format(PyExc_ImportError,
2090 "Failed to import %.200s because the import lock"
2091 "is held by another thread.",
2092 name);
2093 return NULL;
2094 }
Benjamin Peterson17f03ca2008-09-01 14:18:30 +00002095#else
2096 return PyImport_ImportModule(name);
2097#endif
Christian Heimes000a0742008-01-03 22:16:32 +00002098}
2099
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002100/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002101static PyObject *get_parent(PyObject *globals, char *buf,
2102 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002103static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002104 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002105static int mark_miss(char *name);
2106static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002107 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002108static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002109
2110/* The Magnum Opus of dotted-name import :-) */
2111
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002112static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002113import_module_level(char *name, PyObject *globals, PyObject *locals,
2114 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002115{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002116 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00002117 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002118 PyObject *parent, *head, *next, *tail;
2119
Christian Heimes3403f152008-01-09 19:56:33 +00002120 if (strchr(name, '/') != NULL
2121#ifdef MS_WINDOWS
2122 || strchr(name, '\\') != NULL
2123#endif
2124 ) {
2125 PyErr_SetString(PyExc_ImportError,
2126 "Import by filename is not supported.");
2127 return NULL;
2128 }
2129
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002130 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002131 if (parent == NULL)
2132 return NULL;
2133
2134 head = load_next(parent, Py_None, &name, buf, &buflen);
2135 if (head == NULL)
2136 return NULL;
2137
2138 tail = head;
2139 Py_INCREF(tail);
2140 while (name) {
2141 next = load_next(tail, tail, &name, buf, &buflen);
2142 Py_DECREF(tail);
2143 if (next == NULL) {
2144 Py_DECREF(head);
2145 return NULL;
2146 }
2147 tail = next;
2148 }
Thomas Wouters8ddab272006-04-04 16:17:02 +00002149 if (tail == Py_None) {
2150 /* If tail is Py_None, both get_parent and load_next found
2151 an empty module name: someone called __import__("") or
2152 doctored faulty bytecode */
Thomas Wouters4bdaa272006-04-05 13:39:37 +00002153 Py_DECREF(tail);
2154 Py_DECREF(head);
Thomas Wouters8ddab272006-04-04 16:17:02 +00002155 PyErr_SetString(PyExc_ValueError,
2156 "Empty module name");
2157 return NULL;
2158 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002159
2160 if (fromlist != NULL) {
2161 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2162 fromlist = NULL;
2163 }
2164
2165 if (fromlist == NULL) {
2166 Py_DECREF(tail);
2167 return head;
2168 }
2169
2170 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002171 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002172 Py_DECREF(tail);
2173 return NULL;
2174 }
2175
2176 return tail;
2177}
2178
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002179PyObject *
2180PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2181 PyObject *fromlist, int level)
2182{
2183 PyObject *result;
2184 lock_import();
2185 result = import_module_level(name, globals, locals, fromlist, level);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002186 if (unlock_import() < 0) {
2187 Py_XDECREF(result);
2188 PyErr_SetString(PyExc_RuntimeError,
2189 "not holding the import lock");
2190 return NULL;
2191 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002192 return result;
2193}
2194
Fred Drake87590902004-05-28 20:21:36 +00002195/* Return the package that an import is being performed in. If globals comes
2196 from the module foo.bar.bat (not itself a package), this returns the
2197 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Georg Brandl5f6861d2006-05-28 21:57:35 +00002198 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002199
2200 The *name* of the returned package is returned in buf, with the length of
2201 the name in *p_buflen.
2202
2203 If globals doesn't come from a package or a module in a package, or a
2204 corresponding entry is not found in sys.modules, Py_None is returned.
2205*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002206static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002207get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002208{
2209 static PyObject *namestr = NULL;
2210 static PyObject *pathstr = NULL;
Nick Coghlanef01d822007-12-03 12:55:17 +00002211 static PyObject *pkgstr = NULL;
2212 PyObject *pkgname, *modname, *modpath, *modules, *parent;
Nick Coghlanb028f502008-07-13 14:52:36 +00002213 int orig_level = level;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002214
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002215 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002216 return Py_None;
2217
2218 if (namestr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002219 namestr = PyString_InternFromString("__name__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002220 if (namestr == NULL)
2221 return NULL;
2222 }
2223 if (pathstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002224 pathstr = PyString_InternFromString("__path__");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002225 if (pathstr == NULL)
2226 return NULL;
2227 }
Nick Coghlanef01d822007-12-03 12:55:17 +00002228 if (pkgstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002229 pkgstr = PyString_InternFromString("__package__");
Nick Coghlanef01d822007-12-03 12:55:17 +00002230 if (pkgstr == NULL)
2231 return NULL;
2232 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002233
2234 *buf = '\0';
2235 *p_buflen = 0;
Nick Coghlanef01d822007-12-03 12:55:17 +00002236 pkgname = PyDict_GetItem(globals, pkgstr);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002237
Nick Coghlanef01d822007-12-03 12:55:17 +00002238 if ((pkgname != NULL) && (pkgname != Py_None)) {
2239 /* __package__ is set, so use it */
2240 Py_ssize_t len;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002241 if (!PyString_Check(pkgname)) {
Nick Coghlanef01d822007-12-03 12:55:17 +00002242 PyErr_SetString(PyExc_ValueError,
2243 "__package__ set to non-string");
2244 return NULL;
2245 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002246 len = PyString_GET_SIZE(pkgname);
Nick Coghlanef01d822007-12-03 12:55:17 +00002247 if (len == 0) {
2248 if (level > 0) {
2249 PyErr_SetString(PyExc_ValueError,
2250 "Attempted relative import in non-package");
2251 return NULL;
2252 }
2253 return Py_None;
2254 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002255 if (len > MAXPATHLEN) {
2256 PyErr_SetString(PyExc_ValueError,
Nick Coghlanef01d822007-12-03 12:55:17 +00002257 "Package name too long");
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002258 return NULL;
2259 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002260 strcpy(buf, PyString_AS_STRING(pkgname));
Nick Coghlanef01d822007-12-03 12:55:17 +00002261 } else {
2262 /* __package__ not set, so figure it out and set it */
2263 modname = PyDict_GetItem(globals, namestr);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002264 if (modname == NULL || !PyString_Check(modname))
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002265 return Py_None;
Nick Coghlanef01d822007-12-03 12:55:17 +00002266
2267 modpath = PyDict_GetItem(globals, pathstr);
2268 if (modpath != NULL) {
2269 /* __path__ is set, so modname is already the package name */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002270 Py_ssize_t len = PyString_GET_SIZE(modname);
Nick Coghlanef01d822007-12-03 12:55:17 +00002271 int error;
2272 if (len > MAXPATHLEN) {
2273 PyErr_SetString(PyExc_ValueError,
2274 "Module name too long");
2275 return NULL;
2276 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002277 strcpy(buf, PyString_AS_STRING(modname));
Nick Coghlanef01d822007-12-03 12:55:17 +00002278 error = PyDict_SetItem(globals, pkgstr, modname);
2279 if (error) {
2280 PyErr_SetString(PyExc_ValueError,
2281 "Could not set __package__");
2282 return NULL;
2283 }
2284 } else {
2285 /* Normal module, so work out the package name if any */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002286 char *start = PyString_AS_STRING(modname);
Nick Coghlanef01d822007-12-03 12:55:17 +00002287 char *lastdot = strrchr(start, '.');
2288 size_t len;
2289 int error;
2290 if (lastdot == NULL && level > 0) {
2291 PyErr_SetString(PyExc_ValueError,
2292 "Attempted relative import in non-package");
2293 return NULL;
2294 }
2295 if (lastdot == NULL) {
2296 error = PyDict_SetItem(globals, pkgstr, Py_None);
2297 if (error) {
2298 PyErr_SetString(PyExc_ValueError,
2299 "Could not set __package__");
2300 return NULL;
2301 }
2302 return Py_None;
2303 }
2304 len = lastdot - start;
2305 if (len >= MAXPATHLEN) {
2306 PyErr_SetString(PyExc_ValueError,
2307 "Module name too long");
2308 return NULL;
2309 }
2310 strncpy(buf, start, len);
2311 buf[len] = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002312 pkgname = PyString_FromString(buf);
Nick Coghlanef01d822007-12-03 12:55:17 +00002313 if (pkgname == NULL) {
2314 return NULL;
2315 }
2316 error = PyDict_SetItem(globals, pkgstr, pkgname);
2317 Py_DECREF(pkgname);
2318 if (error) {
2319 PyErr_SetString(PyExc_ValueError,
2320 "Could not set __package__");
2321 return NULL;
2322 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002323 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002324 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002325 while (--level > 0) {
2326 char *dot = strrchr(buf, '.');
2327 if (dot == NULL) {
2328 PyErr_SetString(PyExc_ValueError,
Georg Brandl98775df2006-09-06 06:09:31 +00002329 "Attempted relative import beyond "
2330 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002331 return NULL;
2332 }
2333 *dot = '\0';
2334 }
2335 *p_buflen = strlen(buf);
2336
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002337 modules = PyImport_GetModuleDict();
2338 parent = PyDict_GetItemString(modules, buf);
Nick Coghlanb028f502008-07-13 14:52:36 +00002339 if (parent == NULL) {
2340 if (orig_level < 1) {
2341 PyObject *err_msg = PyString_FromFormat(
2342 "Parent module '%.200s' not found "
2343 "while handling absolute import", buf);
2344 if (err_msg == NULL) {
2345 return NULL;
2346 }
2347 if (!PyErr_WarnEx(PyExc_RuntimeWarning,
2348 PyString_AsString(err_msg), 1)) {
2349 *buf = '\0';
2350 *p_buflen = 0;
2351 parent = Py_None;
2352 }
2353 Py_DECREF(err_msg);
2354 } else {
2355 PyErr_Format(PyExc_SystemError,
2356 "Parent module '%.200s' not loaded, "
2357 "cannot perform relative import", buf);
2358 }
2359 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002360 return parent;
2361 /* We expect, but can't guarantee, if parent != None, that:
2362 - parent.__name__ == buf
2363 - parent.__dict__ is globals
2364 If this is violated... Who cares? */
2365}
2366
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002367/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002368static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002369load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002370 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002371{
2372 char *name = *p_name;
2373 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002374 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002375 char *p;
2376 PyObject *result;
2377
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002378 if (strlen(name) == 0) {
Thomas Wouters8ddab272006-04-04 16:17:02 +00002379 /* completely empty module name should only happen in
2380 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002381 Py_INCREF(mod);
2382 *p_name = NULL;
2383 return mod;
2384 }
2385
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002386 if (dot == NULL) {
2387 *p_name = NULL;
2388 len = strlen(name);
2389 }
2390 else {
2391 *p_name = dot+1;
2392 len = dot-name;
2393 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002394 if (len == 0) {
2395 PyErr_SetString(PyExc_ValueError,
2396 "Empty module name");
2397 return NULL;
2398 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002399
2400 p = buf + *p_buflen;
2401 if (p != buf)
2402 *p++ = '.';
2403 if (p+len-buf >= MAXPATHLEN) {
2404 PyErr_SetString(PyExc_ValueError,
2405 "Module name too long");
2406 return NULL;
2407 }
2408 strncpy(p, name, len);
2409 p[len] = '\0';
2410 *p_buflen = p+len-buf;
2411
2412 result = import_submodule(mod, p, buf);
2413 if (result == Py_None && altmod != mod) {
2414 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002415 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002416 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002417 if (result != NULL && result != Py_None) {
2418 if (mark_miss(buf) != 0) {
2419 Py_DECREF(result);
2420 return NULL;
2421 }
2422 strncpy(buf, name, len);
2423 buf[len] = '\0';
2424 *p_buflen = len;
2425 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002426 }
2427 if (result == NULL)
2428 return NULL;
2429
2430 if (result == Py_None) {
2431 Py_DECREF(result);
2432 PyErr_Format(PyExc_ImportError,
2433 "No module named %.200s", name);
2434 return NULL;
2435 }
2436
2437 return result;
2438}
2439
2440static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002441mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002442{
2443 PyObject *modules = PyImport_GetModuleDict();
2444 return PyDict_SetItemString(modules, name, Py_None);
2445}
2446
2447static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002448ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002449 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002450{
2451 int i;
2452
2453 if (!PyObject_HasAttrString(mod, "__path__"))
2454 return 1;
2455
2456 for (i = 0; ; i++) {
2457 PyObject *item = PySequence_GetItem(fromlist, i);
2458 int hasit;
2459 if (item == NULL) {
2460 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2461 PyErr_Clear();
2462 return 1;
2463 }
2464 return 0;
2465 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002466 if (!PyString_Check(item)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002467 PyErr_SetString(PyExc_TypeError,
2468 "Item in ``from list'' not a string");
2469 Py_DECREF(item);
2470 return 0;
2471 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002472 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002473 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002474 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002475 /* See if the package defines __all__ */
2476 if (recursive)
2477 continue; /* Avoid endless recursion */
2478 all = PyObject_GetAttrString(mod, "__all__");
2479 if (all == NULL)
2480 PyErr_Clear();
2481 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002482 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002483 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002484 if (!ret)
2485 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002486 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002487 continue;
2488 }
2489 hasit = PyObject_HasAttr(mod, item);
2490 if (!hasit) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002491 char *subname = PyString_AS_STRING(item);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002492 PyObject *submod;
2493 char *p;
2494 if (buflen + strlen(subname) >= MAXPATHLEN) {
2495 PyErr_SetString(PyExc_ValueError,
2496 "Module name too long");
2497 Py_DECREF(item);
2498 return 0;
2499 }
2500 p = buf + buflen;
2501 *p++ = '.';
2502 strcpy(p, subname);
2503 submod = import_submodule(mod, subname, buf);
2504 Py_XDECREF(submod);
2505 if (submod == NULL) {
2506 Py_DECREF(item);
2507 return 0;
2508 }
2509 }
2510 Py_DECREF(item);
2511 }
2512
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002513 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002514}
2515
Neil Schemenauer00b09662003-06-16 21:03:07 +00002516static int
2517add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2518 PyObject *modules)
2519{
2520 if (mod == Py_None)
2521 return 1;
2522 /* Irrespective of the success of this load, make a
2523 reference to it in the parent package module. A copy gets
2524 saved in the modules dictionary under the full name, so get a
2525 reference from there, if need be. (The exception is when the
2526 load failed with a SyntaxError -- then there's no trace in
2527 sys.modules. In that case, of course, do nothing extra.) */
2528 if (submod == NULL) {
2529 submod = PyDict_GetItemString(modules, fullname);
2530 if (submod == NULL)
2531 return 1;
2532 }
2533 if (PyModule_Check(mod)) {
2534 /* We can't use setattr here since it can give a
2535 * spurious warning if the submodule name shadows a
2536 * builtin name */
2537 PyObject *dict = PyModule_GetDict(mod);
2538 if (!dict)
2539 return 0;
2540 if (PyDict_SetItemString(dict, subname, submod) < 0)
2541 return 0;
2542 }
2543 else {
2544 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2545 return 0;
2546 }
2547 return 1;
2548}
2549
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002550static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002551import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002552{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002553 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002554 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002555
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002556 /* Require:
2557 if mod == None: subname == fullname
2558 else: mod.__name__ + "." + subname == fullname
2559 */
2560
Tim Peters50d8d372001-02-28 05:34:27 +00002561 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002562 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002563 }
2564 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002565 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002566 char buf[MAXPATHLEN+1];
2567 struct filedescr *fdp;
2568 FILE *fp = NULL;
2569
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002570 if (mod == Py_None)
2571 path = NULL;
2572 else {
2573 path = PyObject_GetAttrString(mod, "__path__");
2574 if (path == NULL) {
2575 PyErr_Clear();
2576 Py_INCREF(Py_None);
2577 return Py_None;
2578 }
2579 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002580
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002581 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002582 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2583 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002584 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002585 if (fdp == NULL) {
2586 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2587 return NULL;
2588 PyErr_Clear();
2589 Py_INCREF(Py_None);
2590 return Py_None;
2591 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002592 m = load_module(fullname, fp, buf, fdp->type, loader);
2593 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002594 if (fp)
2595 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002596 if (!add_submodule(mod, m, fullname, subname, modules)) {
2597 Py_XDECREF(m);
2598 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002599 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002600 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002601
2602 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002603}
2604
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002605
2606/* Re-import a module of any kind and return its module object, WITH
2607 INCREMENTED REFERENCE COUNT */
2608
Guido van Rossum79f25d91997-04-29 20:08:16 +00002609PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002610PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002611{
Collin Winter47c52a82007-03-13 23:02:15 +00002612 PyInterpreterState *interp = PyThreadState_Get()->interp;
2613 PyObject *modules_reloading = interp->modules_reloading;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002614 PyObject *modules = PyImport_GetModuleDict();
Collin Winter276887b2007-03-12 16:11:39 +00002615 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002616 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002617 char buf[MAXPATHLEN+1];
2618 struct filedescr *fdp;
2619 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002620 PyObject *newm;
Collin Winter47c52a82007-03-13 23:02:15 +00002621
2622 if (modules_reloading == NULL) {
2623 Py_FatalError("PyImport_ReloadModule: "
Neal Norwitz2fca81c2007-05-30 04:53:41 +00002624 "no modules_reloading dictionary!");
Collin Winter47c52a82007-03-13 23:02:15 +00002625 return NULL;
2626 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002627
Guido van Rossum79f25d91997-04-29 20:08:16 +00002628 if (m == NULL || !PyModule_Check(m)) {
2629 PyErr_SetString(PyExc_TypeError,
2630 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002631 return NULL;
2632 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002633 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002634 if (name == NULL)
2635 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002636 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002637 PyErr_Format(PyExc_ImportError,
2638 "reload(): module %.200s not in sys.modules",
2639 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002640 return NULL;
2641 }
Neal Norwitz75c7c802007-03-13 05:31:38 +00002642 existing_m = PyDict_GetItemString(modules_reloading, name);
2643 if (existing_m != NULL) {
2644 /* Due to a recursive reload, this module is already
2645 being reloaded. */
2646 Py_INCREF(existing_m);
2647 return existing_m;
Collin Winter276887b2007-03-12 16:11:39 +00002648 }
Neal Norwitz75c7c802007-03-13 05:31:38 +00002649 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
2650 return NULL;
Collin Winter276887b2007-03-12 16:11:39 +00002651
Guido van Rossum222ef561997-09-06 19:41:09 +00002652 subname = strrchr(name, '.');
2653 if (subname == NULL)
2654 subname = name;
2655 else {
2656 PyObject *parentname, *parent;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002657 parentname = PyString_FromStringAndSize(name, (subname-name));
Collin Winter276887b2007-03-12 16:11:39 +00002658 if (parentname == NULL) {
2659 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002660 return NULL;
Neal Norwitz75c7c802007-03-13 05:31:38 +00002661 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002662 parent = PyDict_GetItem(modules, parentname);
2663 if (parent == NULL) {
2664 PyErr_Format(PyExc_ImportError,
2665 "reload(): parent %.200s not in sys.modules",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002666 PyString_AS_STRING(parentname));
Georg Brandl0c55f292005-09-14 06:56:20 +00002667 Py_DECREF(parentname);
Neal Norwitz2fca81c2007-05-30 04:53:41 +00002668 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002669 return NULL;
2670 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002671 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002672 subname++;
2673 path = PyObject_GetAttrString(parent, "__path__");
2674 if (path == NULL)
2675 PyErr_Clear();
2676 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002677 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002678 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002679 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002680
2681 if (fdp == NULL) {
2682 Py_XDECREF(loader);
Collin Winter276887b2007-03-12 16:11:39 +00002683 imp_modules_reloading_clear();
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002684 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002685 }
2686
2687 newm = load_module(name, fp, buf, fdp->type, loader);
2688 Py_XDECREF(loader);
2689
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002690 if (fp)
2691 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002692 if (newm == NULL) {
2693 /* load_module probably removed name from modules because of
2694 * the error. Put back the original module object. We're
2695 * going to return NULL in this case regardless of whether
2696 * replacing name succeeds, so the return value is ignored.
2697 */
2698 PyDict_SetItemString(modules, name, m);
2699 }
Neal Norwitz75c7c802007-03-13 05:31:38 +00002700 imp_modules_reloading_clear();
Tim Peters1cd70172004-08-02 03:52:12 +00002701 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002702}
2703
2704
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002705/* Higher-level import emulator which emulates the "import" statement
2706 more accurately -- it invokes the __import__() function from the
2707 builtins of the current globals. This means that the import is
2708 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002709 environment, e.g. by "rexec".
2710 A dummy list ["__doc__"] is passed as the 4th argument so that
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002711 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00002712 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002713
2714PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002715PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002716{
2717 static PyObject *silly_list = NULL;
2718 static PyObject *builtins_str = NULL;
2719 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002720 PyObject *globals = NULL;
2721 PyObject *import = NULL;
2722 PyObject *builtins = NULL;
2723 PyObject *r = NULL;
2724
2725 /* Initialize constant string objects */
2726 if (silly_list == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002727 import_str = PyString_InternFromString("__import__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002728 if (import_str == NULL)
2729 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002730 builtins_str = PyString_InternFromString("__builtins__");
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002731 if (builtins_str == NULL)
2732 return NULL;
2733 silly_list = Py_BuildValue("[s]", "__doc__");
2734 if (silly_list == NULL)
2735 return NULL;
2736 }
2737
2738 /* Get the builtins from current globals */
2739 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002740 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002741 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002742 builtins = PyObject_GetItem(globals, builtins_str);
2743 if (builtins == NULL)
2744 goto err;
2745 }
2746 else {
2747 /* No globals -- use standard builtins, and fake globals */
2748 PyErr_Clear();
2749
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002750 builtins = PyImport_ImportModuleLevel("__builtin__",
2751 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002752 if (builtins == NULL)
2753 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002754 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2755 if (globals == NULL)
2756 goto err;
2757 }
2758
2759 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002760 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002761 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002762 if (import == NULL)
2763 PyErr_SetObject(PyExc_KeyError, import_str);
2764 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002765 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002766 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002767 if (import == NULL)
2768 goto err;
2769
Christian Heimes000a0742008-01-03 22:16:32 +00002770 /* Call the __import__ function with the proper argument list
2771 * Always use absolute import here. */
2772 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
2773 globals, silly_list, 0, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002774
2775 err:
2776 Py_XDECREF(globals);
2777 Py_XDECREF(builtins);
2778 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002779
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002780 return r;
2781}
2782
2783
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002784/* Module 'imp' provides Python access to the primitives used for
2785 importing modules.
2786*/
2787
Guido van Rossum79f25d91997-04-29 20:08:16 +00002788static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002789imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002790{
2791 char buf[4];
2792
Guido van Rossum96774c12000-05-01 20:19:08 +00002793 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2794 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2795 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2796 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002797
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002798 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002799}
2800
Guido van Rossum79f25d91997-04-29 20:08:16 +00002801static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002802imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002803{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002804 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002805 struct filedescr *fdp;
2806
Guido van Rossum79f25d91997-04-29 20:08:16 +00002807 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002808 if (list == NULL)
2809 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002810 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2811 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002812 fdp->suffix, fdp->mode, fdp->type);
2813 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002814 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002815 return NULL;
2816 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002817 if (PyList_Append(list, item) < 0) {
2818 Py_DECREF(list);
2819 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002820 return NULL;
2821 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002822 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002823 }
2824 return list;
2825}
2826
Guido van Rossum79f25d91997-04-29 20:08:16 +00002827static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002828call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002829{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002830 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002831 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002832 struct filedescr *fdp;
2833 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002834 FILE *fp = NULL;
2835
2836 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002837 if (path == Py_None)
2838 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002839 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002840 if (fdp == NULL)
2841 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002842 if (fp != NULL) {
2843 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2844 if (fob == NULL) {
2845 fclose(fp);
2846 return NULL;
2847 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002848 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002849 else {
2850 fob = Py_None;
2851 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002852 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002853 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002854 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002855 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002856 return ret;
2857}
2858
Guido van Rossum79f25d91997-04-29 20:08:16 +00002859static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002860imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002861{
2862 char *name;
2863 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002864 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002865 return NULL;
2866 return call_find_module(name, path);
2867}
2868
2869static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002870imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002871{
2872 char *name;
2873 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002874 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002875 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002876 return NULL;
2877 ret = init_builtin(name);
2878 if (ret < 0)
2879 return NULL;
2880 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002881 Py_INCREF(Py_None);
2882 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002883 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002884 m = PyImport_AddModule(name);
2885 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002886 return m;
2887}
2888
Guido van Rossum79f25d91997-04-29 20:08:16 +00002889static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002890imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002891{
2892 char *name;
2893 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002894 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002895 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002896 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002897 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002898 if (ret < 0)
2899 return NULL;
2900 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002901 Py_INCREF(Py_None);
2902 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002903 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002904 m = PyImport_AddModule(name);
2905 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002906 return m;
2907}
2908
Guido van Rossum79f25d91997-04-29 20:08:16 +00002909static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002910imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002911{
2912 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002913
Guido van Rossum43713e52000-02-29 13:59:29 +00002914 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002915 return NULL;
2916 return get_frozen_object(name);
2917}
2918
Guido van Rossum79f25d91997-04-29 20:08:16 +00002919static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002920imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002921{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002922 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002923 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002924 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002925 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002926}
2927
Guido van Rossum79f25d91997-04-29 20:08:16 +00002928static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002929imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002930{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002931 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002932 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002933 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002934 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002935 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002936 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002937}
2938
2939static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002940get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002941{
2942 FILE *fp;
2943 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002944 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002945 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002946 fp = fopen(pathname, mode);
2947 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002948 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002949 }
2950 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002951 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002952 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002953 PyErr_SetString(PyExc_ValueError,
2954 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002955 }
2956 return fp;
2957}
2958
Guido van Rossum79f25d91997-04-29 20:08:16 +00002959static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002960imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002961{
2962 char *name;
2963 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002964 PyObject *fob = NULL;
2965 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002966 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002967 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002968 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002969 return NULL;
2970 fp = get_file(pathname, fob, "rb");
2971 if (fp == NULL)
2972 return NULL;
2973 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002974 if (fob == NULL)
2975 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002976 return m;
2977}
2978
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002979#ifdef HAVE_DYNAMIC_LOADING
2980
Guido van Rossum79f25d91997-04-29 20:08:16 +00002981static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002982imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002983{
2984 char *name;
2985 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002986 PyObject *fob = NULL;
2987 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002988 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002989 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002990 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002991 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002992 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002993 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002994 if (fp == NULL)
2995 return NULL;
2996 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002997 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002998 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002999}
3000
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003001#endif /* HAVE_DYNAMIC_LOADING */
3002
Guido van Rossum79f25d91997-04-29 20:08:16 +00003003static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003004imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003005{
3006 char *name;
3007 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003008 PyObject *fob = NULL;
3009 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003010 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00003011 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00003012 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003013 return NULL;
3014 fp = get_file(pathname, fob, "r");
3015 if (fp == NULL)
3016 return NULL;
3017 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003018 if (fob == NULL)
3019 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003020 return m;
3021}
3022
Guido van Rossum79f25d91997-04-29 20:08:16 +00003023static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003024imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003025{
3026 char *name;
3027 PyObject *fob;
3028 char *pathname;
3029 char *suffix; /* Unused */
3030 char *mode;
3031 int type;
3032 FILE *fp;
3033
Guido van Rossum43713e52000-02-29 13:59:29 +00003034 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003035 &name, &fob, &pathname,
3036 &suffix, &mode, &type))
3037 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003038 if (*mode) {
3039 /* Mode must start with 'r' or 'U' and must not contain '+'.
3040 Implicit in this test is the assumption that the mode
3041 may contain other modifiers like 'b' or 't'. */
3042
3043 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00003044 PyErr_Format(PyExc_ValueError,
3045 "invalid file open mode %.200s", mode);
3046 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003047 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003048 }
3049 if (fob == Py_None)
3050 fp = NULL;
3051 else {
3052 if (!PyFile_Check(fob)) {
3053 PyErr_SetString(PyExc_ValueError,
3054 "load_module arg#2 should be a file or None");
3055 return NULL;
3056 }
3057 fp = get_file(pathname, fob, mode);
3058 if (fp == NULL)
3059 return NULL;
3060 }
Just van Rossum52e14d62002-12-30 22:08:05 +00003061 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003062}
3063
3064static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003065imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003066{
3067 char *name;
3068 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00003069 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003070 return NULL;
3071 return load_package(name, pathname);
3072}
3073
3074static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003075imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003076{
3077 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00003078 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003079 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003080 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003081}
3082
Brett Cannon3aa2a492008-08-06 22:28:09 +00003083static PyObject *
3084imp_reload(PyObject *self, PyObject *v)
3085{
3086 return PyImport_ReloadModule(v);
3087}
3088
3089
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003090/* Doc strings */
3091
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003092PyDoc_STRVAR(doc_imp,
3093"This module provides the components needed to build your own\n\
3094__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003095
Brett Cannon3aa2a492008-08-06 22:28:09 +00003096PyDoc_STRVAR(doc_reload,
3097"reload(module) -> module\n\
3098\n\
3099Reload the module. The module must have been successfully imported before.");
3100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003101PyDoc_STRVAR(doc_find_module,
3102"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003103Search for a module. If path is omitted or None, search for a\n\
3104built-in, frozen or special module and continue search in sys.path.\n\
3105The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003106package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003108PyDoc_STRVAR(doc_load_module,
3109"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003110Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003111The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003112
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003113PyDoc_STRVAR(doc_get_magic,
3114"get_magic() -> string\n\
3115Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003116
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003117PyDoc_STRVAR(doc_get_suffixes,
3118"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003119Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003120that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003122PyDoc_STRVAR(doc_new_module,
3123"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003124Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003125The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003127PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00003128"lock_held() -> boolean\n\
3129Return True if the import lock is currently held, else False.\n\
3130On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00003131
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003132PyDoc_STRVAR(doc_acquire_lock,
3133"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00003134Acquires the interpreter's import lock for the current thread.\n\
3135This lock should be used by import hooks to ensure thread-safety\n\
3136when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003137On platforms without threads, this function does nothing.");
3138
3139PyDoc_STRVAR(doc_release_lock,
3140"release_lock() -> None\n\
3141Release the interpreter's import lock.\n\
3142On platforms without threads, this function does nothing.");
3143
Guido van Rossum79f25d91997-04-29 20:08:16 +00003144static PyMethodDef imp_methods[] = {
Brett Cannon3aa2a492008-08-06 22:28:09 +00003145 {"reload", imp_reload, METH_O, doc_reload},
Neal Norwitz08ea61a2003-02-17 18:18:00 +00003146 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3147 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3148 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3149 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3150 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3151 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3152 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3153 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003154 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00003155 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
3156 {"init_builtin", imp_init_builtin, METH_VARARGS},
3157 {"init_frozen", imp_init_frozen, METH_VARARGS},
3158 {"is_builtin", imp_is_builtin, METH_VARARGS},
3159 {"is_frozen", imp_is_frozen, METH_VARARGS},
3160 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003161#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00003162 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003163#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00003164 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00003165 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003166 {NULL, NULL} /* sentinel */
3167};
3168
Guido van Rossum1a8791e1998-08-04 22:46:29 +00003169static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003170setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003171{
3172 PyObject *v;
3173 int err;
3174
3175 v = PyInt_FromLong((long)value);
3176 err = PyDict_SetItemString(d, name, v);
3177 Py_XDECREF(v);
3178 return err;
3179}
3180
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003181typedef struct {
3182 PyObject_HEAD
3183} NullImporter;
3184
3185static int
3186NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3187{
3188 char *path;
Christian Heimescea681b2007-11-07 17:50:54 +00003189 Py_ssize_t pathlen;
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003190
3191 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3192 return -1;
3193
3194 if (!PyArg_ParseTuple(args, "s:NullImporter",
3195 &path))
3196 return -1;
3197
Christian Heimescea681b2007-11-07 17:50:54 +00003198 pathlen = strlen(path);
3199 if (pathlen == 0) {
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003200 PyErr_SetString(PyExc_ImportError, "empty pathname");
3201 return -1;
3202 } else {
3203#ifndef RISCOS
Kristján Valur Jónsson3b2a6b82009-01-09 20:10:59 +00003204#ifndef MS_WINDOWS
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003205 struct stat statbuf;
3206 int rv;
3207
3208 rv = stat(path, &statbuf);
3209 if (rv == 0) {
3210 /* it exists */
3211 if (S_ISDIR(statbuf.st_mode)) {
3212 /* it's a directory */
3213 PyErr_SetString(PyExc_ImportError,
3214 "existing directory");
3215 return -1;
3216 }
3217 }
Kristján Valur Jónsson3b2a6b82009-01-09 20:10:59 +00003218#else /* MS_WINDOWS */
3219 DWORD rv;
3220 /* see issue1293 and issue3677:
3221 * stat() on Windows doesn't recognise paths like
3222 * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
3223 */
3224 rv = GetFileAttributesA(path);
3225 if (rv != INVALID_FILE_ATTRIBUTES) {
3226 /* it exists */
3227 if (rv & FILE_ATTRIBUTE_DIRECTORY) {
3228 /* it's a directory */
3229 PyErr_SetString(PyExc_ImportError,
3230 "existing directory");
3231 return -1;
3232 }
3233 }
3234#endif
3235#else /* RISCOS */
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003236 if (object_exists(path)) {
3237 /* it exists */
3238 if (isdir(path)) {
3239 /* it's a directory */
3240 PyErr_SetString(PyExc_ImportError,
3241 "existing directory");
3242 return -1;
3243 }
3244 }
3245#endif
3246 }
3247 return 0;
3248}
3249
3250static PyObject *
3251NullImporter_find_module(NullImporter *self, PyObject *args)
3252{
3253 Py_RETURN_NONE;
3254}
3255
3256static PyMethodDef NullImporter_methods[] = {
3257 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3258 "Always return None"
3259 },
3260 {NULL} /* Sentinel */
3261};
3262
3263
Nick Coghlan327a39b2007-11-18 11:56:28 +00003264PyTypeObject PyNullImporter_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003265 PyVarObject_HEAD_INIT(NULL, 0)
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003266 "imp.NullImporter", /*tp_name*/
3267 sizeof(NullImporter), /*tp_basicsize*/
3268 0, /*tp_itemsize*/
3269 0, /*tp_dealloc*/
3270 0, /*tp_print*/
3271 0, /*tp_getattr*/
3272 0, /*tp_setattr*/
3273 0, /*tp_compare*/
3274 0, /*tp_repr*/
3275 0, /*tp_as_number*/
3276 0, /*tp_as_sequence*/
3277 0, /*tp_as_mapping*/
3278 0, /*tp_hash */
3279 0, /*tp_call*/
3280 0, /*tp_str*/
3281 0, /*tp_getattro*/
3282 0, /*tp_setattro*/
3283 0, /*tp_as_buffer*/
3284 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3285 "Null importer object", /* tp_doc */
3286 0, /* tp_traverse */
3287 0, /* tp_clear */
3288 0, /* tp_richcompare */
3289 0, /* tp_weaklistoffset */
3290 0, /* tp_iter */
3291 0, /* tp_iternext */
3292 NullImporter_methods, /* tp_methods */
3293 0, /* tp_members */
3294 0, /* tp_getset */
3295 0, /* tp_base */
3296 0, /* tp_dict */
3297 0, /* tp_descr_get */
3298 0, /* tp_descr_set */
3299 0, /* tp_dictoffset */
3300 (initproc)NullImporter_init, /* tp_init */
3301 0, /* tp_alloc */
3302 PyType_GenericNew /* tp_new */
3303};
3304
3305
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003306PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003307initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003308{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003309 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003310
Nick Coghlan327a39b2007-11-18 11:56:28 +00003311 if (PyType_Ready(&PyNullImporter_Type) < 0)
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003312 goto failure;
3313
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003314 m = Py_InitModule4("imp", imp_methods, doc_imp,
3315 NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003316 if (m == NULL)
3317 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003318 d = PyModule_GetDict(m);
Neal Norwitz3cb31ac2006-08-13 18:10:47 +00003319 if (d == NULL)
3320 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003321
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003322 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3323 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3324 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3325 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3326 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3327 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3328 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3329 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003330 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003331 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003332
Nick Coghlan327a39b2007-11-18 11:56:28 +00003333 Py_INCREF(&PyNullImporter_Type);
3334 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003335 failure:
3336 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003337}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003338
3339
Guido van Rossumb18618d2000-05-03 23:44:39 +00003340/* API for embedding applications that want to add their own entries
3341 to the table of built-in modules. This should normally be called
3342 *before* Py_Initialize(). When the table resize fails, -1 is
3343 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003344
3345 After a similar function by Just van Rossum. */
3346
3347int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003348PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003349{
3350 static struct _inittab *our_copy = NULL;
3351 struct _inittab *p;
3352 int i, n;
3353
3354 /* Count the number of entries in both tables */
3355 for (n = 0; newtab[n].name != NULL; n++)
3356 ;
3357 if (n == 0)
3358 return 0; /* Nothing to do */
3359 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3360 ;
3361
3362 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003363 p = our_copy;
3364 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003365 if (p == NULL)
3366 return -1;
3367
3368 /* Copy the tables into the new memory */
3369 if (our_copy != PyImport_Inittab)
3370 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3371 PyImport_Inittab = our_copy = p;
3372 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3373
3374 return 0;
3375}
3376
3377/* Shorthand to add a single entry given a name and a function */
3378
3379int
Brett Cannonc4f90eb2009-04-02 03:17:39 +00003380PyImport_AppendInittab(const char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003381{
3382 struct _inittab newtab[2];
3383
3384 memset(newtab, '\0', sizeof newtab);
3385
Brett Cannon238cedc2009-04-02 03:34:53 +00003386 newtab[0].name = (char *)name;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003387 newtab[0].initfunc = initfunc;
3388
3389 return PyImport_ExtendInittab(newtab);
3390}
Anthony Baxterac6bd462006-04-13 02:06:09 +00003391
3392#ifdef __cplusplus
3393}
3394#endif