blob: 6a4d22f6e645ef4e26a37c48bc23edf50bd62f7f [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
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000025extern time_t PyOS_GetLastModificationTime(char *, FILE *);
26 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000027
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000028/* Magic word to reject .pyc files generated by other Python versions.
29 It should change for each incompatible change to the bytecode.
30
31 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000032 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000033 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000034
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000035 The magic numbers must be spaced apart atleast 2 values, as the
36 -U interpeter flag will cause MAGIC+1 being used. They have been
37 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000038
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000039 There were a variety of old schemes for setting the magic number.
40 The current working scheme is to increment the previous value by
41 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000042
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000043 Known values:
44 Python 1.5: 20121
45 Python 1.5.1: 20121
46 Python 1.5.2: 20121
Skip Montanaro4ec3c262006-03-25 14:12:03 +000047 Python 1.6: 50428
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000048 Python 2.0: 50823
49 Python 2.0.1: 50823
50 Python 2.1: 60202
51 Python 2.1.1: 60202
52 Python 2.1.2: 60202
53 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000054 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000055 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000056 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000057 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000058 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000059 Python 2.4b1: 62061
Michael W. Hudsondf888462005-06-03 14:41:55 +000060 Python 2.5a0: 62071
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000061 Python 2.5a0: 62081 (ast-branch)
Guido van Rossumc2e20742006-02-27 22:32:47 +000062 Python 2.5a0: 62091 (with)
Guido van Rossumf6694362006-03-10 02:28:35 +000063 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
Neal Norwitz0d62a062006-07-30 06:53:31 +000064 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
65 Python 2.5b3: 62111 (fix wrong code: x += yield)
Neal Norwitz9a70f952006-08-04 05:12:19 +000066 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
67 storing constants that should have been removed)
Neal Norwitzdac090d2006-09-05 03:53:08 +000068 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
Neal Norwitzcbeb6872006-10-14 21:33:38 +000069 Python 2.6a0: 62141 (peephole optimizations)
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000070.
Tim Peters36515e22001-11-18 04:06:29 +000071*/
Neal Norwitzcbeb6872006-10-14 21:33:38 +000072#define MAGIC (62141 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000073
Guido van Rossum96774c12000-05-01 20:19:08 +000074/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000075 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000076 compiler works which are enabled by command line switches. */
77static long pyc_magic = MAGIC;
78
Guido van Rossum25ce5661997-08-02 03:10:38 +000079/* See _PyImport_FixupExtension() below */
80static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000081
Guido van Rossum771c6c81997-10-31 18:37:24 +000082/* This table is defined in config.c: */
83extern struct _inittab _PyImport_Inittab[];
84
85struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000086
Guido van Rossumed1170e1999-12-20 21:23:41 +000087/* these tables define the module suffixes that Python recognizes */
88struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000089
90#ifdef RISCOS
91static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000092 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000093 {"/pyc", "rb", PY_COMPILED},
94 {0, 0}
95};
96#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000097static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000098 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000099#ifdef MS_WINDOWS
Jack Jansenc88da1f2002-05-28 10:58:19 +0000100 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000101#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000102 {".pyc", "rb", PY_COMPILED},
103 {0, 0}
104};
Guido van Rossum48a680c2001-03-02 06:34:14 +0000105#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000106
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000107static PyTypeObject NullImporterType; /* Forward reference */
108
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000109/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110
111void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000112_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000114 const struct filedescr *scan;
115 struct filedescr *filetab;
116 int countD = 0;
117 int countS = 0;
118
119 /* prepare _PyImport_Filetab: copy entries from
120 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
121 */
Georg Brandladd36e52007-08-23 18:08:06 +0000122#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000123 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
124 ++countD;
Georg Brandladd36e52007-08-23 18:08:06 +0000125#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000126 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
127 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000128 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Neal Norwitze1fdb322006-07-21 05:32:28 +0000129 if (filetab == NULL)
Neal Norwitz33722ae2006-07-21 07:59:02 +0000130 Py_FatalError("Can't initialize import file table.");
Georg Brandladd36e52007-08-23 18:08:06 +0000131#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumed1170e1999-12-20 21:23:41 +0000132 memcpy(filetab, _PyImport_DynLoadFiletab,
133 countD * sizeof(struct filedescr));
Georg Brandladd36e52007-08-23 18:08:06 +0000134#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000135 memcpy(filetab + countD, _PyImport_StandardFiletab,
136 countS * sizeof(struct filedescr));
137 filetab[countD + countS].suffix = NULL;
138
139 _PyImport_Filetab = filetab;
140
Guido van Rossum0824f631997-03-11 18:37:35 +0000141 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000142 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
143 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000144#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000145 if (strcmp(filetab->suffix, ".pyc") == 0)
146 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000147#else
148 if (strcmp(filetab->suffix, "/pyc") == 0)
149 filetab->suffix = "/pyo";
150#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000151 }
152 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000153
154 if (Py_UnicodeFlag) {
155 /* Fix the pyc_magic so that byte compiled code created
156 using the all-Unicode method doesn't interfere with
157 code created in normal operation mode. */
158 pyc_magic = MAGIC + 1;
159 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160}
161
Guido van Rossum25ce5661997-08-02 03:10:38 +0000162void
Just van Rossum52e14d62002-12-30 22:08:05 +0000163_PyImportHooks_Init(void)
164{
165 PyObject *v, *path_hooks = NULL, *zimpimport;
166 int err = 0;
167
168 /* adding sys.path_hooks and sys.path_importer_cache, setting up
169 zipimport */
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000170 if (PyType_Ready(&NullImporterType) < 0)
171 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000172
173 if (Py_VerboseFlag)
174 PySys_WriteStderr("# installing zipimport hook\n");
175
176 v = PyList_New(0);
177 if (v == NULL)
178 goto error;
179 err = PySys_SetObject("meta_path", v);
180 Py_DECREF(v);
181 if (err)
182 goto error;
183 v = PyDict_New();
184 if (v == NULL)
185 goto error;
186 err = PySys_SetObject("path_importer_cache", v);
187 Py_DECREF(v);
188 if (err)
189 goto error;
190 path_hooks = PyList_New(0);
191 if (path_hooks == NULL)
192 goto error;
193 err = PySys_SetObject("path_hooks", path_hooks);
194 if (err) {
195 error:
196 PyErr_Print();
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000197 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
198 "path_importer_cache, or NullImporter failed"
199 );
Just van Rossum52e14d62002-12-30 22:08:05 +0000200 }
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000201
Just van Rossum52e14d62002-12-30 22:08:05 +0000202 zimpimport = PyImport_ImportModule("zipimport");
203 if (zimpimport == NULL) {
204 PyErr_Clear(); /* No zip import module -- okay */
205 if (Py_VerboseFlag)
206 PySys_WriteStderr("# can't import zipimport\n");
207 }
208 else {
209 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
210 "zipimporter");
211 Py_DECREF(zimpimport);
212 if (zipimporter == NULL) {
213 PyErr_Clear(); /* No zipimporter object -- okay */
214 if (Py_VerboseFlag)
215 PySys_WriteStderr(
Fred Drake1e5fc552003-07-11 15:01:02 +0000216 "# can't import zipimport.zipimporter\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000217 }
218 else {
219 /* sys.path_hooks.append(zipimporter) */
220 err = PyList_Append(path_hooks, zipimporter);
221 Py_DECREF(zipimporter);
222 if (err)
223 goto error;
224 if (Py_VerboseFlag)
225 PySys_WriteStderr(
226 "# installed zipimport hook\n");
227 }
228 }
229 Py_DECREF(path_hooks);
230}
231
232void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000233_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000234{
235 Py_XDECREF(extensions);
236 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000237 PyMem_DEL(_PyImport_Filetab);
238 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000239}
240
241
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000242/* Locking primitives to prevent parallel imports of the same module
243 in different threads to return with a partially loaded module.
244 These calls are serialized by the global interpreter lock. */
245
246#ifdef WITH_THREAD
247
Guido van Rossum49b56061998-10-01 20:42:43 +0000248#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000249
Guido van Rossum65d5b571998-12-21 19:32:43 +0000250static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000251static long import_lock_thread = -1;
252static int import_lock_level = 0;
253
254static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000255lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000256{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000257 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000258 if (me == -1)
259 return; /* Too bad */
Neal Norwitze1fdb322006-07-21 05:32:28 +0000260 if (import_lock == NULL) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000261 import_lock = PyThread_allocate_lock();
Neal Norwitze1fdb322006-07-21 05:32:28 +0000262 if (import_lock == NULL)
263 return; /* Nothing much we can do. */
264 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000265 if (import_lock_thread == me) {
266 import_lock_level++;
267 return;
268 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000269 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
270 {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000271 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000272 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000273 PyEval_RestoreThread(tstate);
274 }
275 import_lock_thread = me;
276 import_lock_level = 1;
277}
278
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000279static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000280unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000281{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000282 long me = PyThread_get_thread_ident();
Neal Norwitze1fdb322006-07-21 05:32:28 +0000283 if (me == -1 || import_lock == NULL)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000284 return 0; /* Too bad */
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000285 if (import_lock_thread != me)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000286 return -1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000287 import_lock_level--;
288 if (import_lock_level == 0) {
289 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000290 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000291 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000292 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000293}
294
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000295/* This function is called from PyOS_AfterFork to ensure that newly
296 created child processes do not share locks with the parent. */
297
298void
299_PyImport_ReInitLock(void)
300{
301#ifdef _AIX
302 if (import_lock != NULL)
303 import_lock = PyThread_allocate_lock();
304#endif
305}
306
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000307#else
308
309#define lock_import()
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000310#define unlock_import() 0
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000311
312#endif
313
Tim Peters69232342001-08-30 05:16:13 +0000314static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000315imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000316{
Tim Peters69232342001-08-30 05:16:13 +0000317#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000318 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000319#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000320 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000321#endif
322}
323
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000324static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000325imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000326{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000327#ifdef WITH_THREAD
328 lock_import();
329#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000330 Py_INCREF(Py_None);
331 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000332}
333
334static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000335imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000336{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000337#ifdef WITH_THREAD
338 if (unlock_import() < 0) {
339 PyErr_SetString(PyExc_RuntimeError,
340 "not holding the import lock");
341 return NULL;
342 }
343#endif
Neal Norwitz2294c0d2003-02-12 23:02:21 +0000344 Py_INCREF(Py_None);
345 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000346}
347
Collin Winter276887b2007-03-12 16:11:39 +0000348static void
Neal Norwitz75c7c802007-03-13 05:31:38 +0000349imp_modules_reloading_clear(void)
Collin Winter276887b2007-03-12 16:11:39 +0000350{
351 PyInterpreterState *interp = PyThreadState_Get()->interp;
Neal Norwitz75c7c802007-03-13 05:31:38 +0000352 if (interp->modules_reloading != NULL)
353 PyDict_Clear(interp->modules_reloading);
Collin Winter276887b2007-03-12 16:11:39 +0000354}
355
Guido van Rossum25ce5661997-08-02 03:10:38 +0000356/* Helper for sys */
357
358PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000359PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000360{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000361 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000362 if (interp->modules == NULL)
363 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
364 return interp->modules;
365}
366
Guido van Rossum3f5da241990-12-20 15:06:42 +0000367
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000368/* List of names to clear in sys */
369static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000370 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000371 "exc_type", "exc_value", "exc_traceback",
372 "last_type", "last_value", "last_traceback",
Just van Rossum52e14d62002-12-30 22:08:05 +0000373 "path_hooks", "path_importer_cache", "meta_path",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000374 NULL
375};
376
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000377static char* sys_files[] = {
378 "stdin", "__stdin__",
379 "stdout", "__stdout__",
380 "stderr", "__stderr__",
381 NULL
382};
383
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000384
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000385/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000386
Guido van Rossum3f5da241990-12-20 15:06:42 +0000387void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000388PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000389{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000390 Py_ssize_t pos, ndone;
Guido van Rossum758eec01998-01-19 21:58:26 +0000391 char *name;
392 PyObject *key, *value, *dict;
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000393 PyInterpreterState *interp = PyThreadState_GET()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000394 PyObject *modules = interp->modules;
395
396 if (modules == NULL)
397 return; /* Already done */
398
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000399 /* Delete some special variables first. These are common
400 places where user values hide and people complain when their
401 destructors fail. Since the modules containing them are
402 deleted *last* of all, they would come too late in the normal
403 destruction order. Sigh. */
404
405 value = PyDict_GetItemString(modules, "__builtin__");
406 if (value != NULL && PyModule_Check(value)) {
407 dict = PyModule_GetDict(value);
408 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000409 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000410 PyDict_SetItemString(dict, "_", Py_None);
411 }
412 value = PyDict_GetItemString(modules, "sys");
413 if (value != NULL && PyModule_Check(value)) {
414 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000415 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000416 dict = PyModule_GetDict(value);
417 for (p = sys_deletes; *p != NULL; p++) {
418 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000419 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000420 PyDict_SetItemString(dict, *p, Py_None);
421 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000422 for (p = sys_files; *p != NULL; p+=2) {
423 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000424 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000425 v = PyDict_GetItemString(dict, *(p+1));
426 if (v == NULL)
427 v = Py_None;
428 PyDict_SetItemString(dict, *p, v);
429 }
430 }
431
432 /* First, delete __main__ */
433 value = PyDict_GetItemString(modules, "__main__");
434 if (value != NULL && PyModule_Check(value)) {
435 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000436 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000437 _PyModule_Clear(value);
438 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000439 }
440
Guido van Rossum758eec01998-01-19 21:58:26 +0000441 /* The special treatment of __builtin__ here is because even
442 when it's not referenced as a module, its dictionary is
443 referenced by almost every module's __builtins__. Since
444 deleting a module clears its dictionary (even if there are
445 references left to it), we need to delete the __builtin__
446 module last. Likewise, we don't delete sys until the very
447 end because it is implicitly referenced (e.g. by print).
448
449 Also note that we 'delete' modules by replacing their entry
450 in the modules dict with None, rather than really deleting
451 them; this avoids a rehash of the modules dictionary and
452 also marks them as "non existent" so they won't be
453 re-imported. */
454
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000455 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000456 one (skipping __builtin__ and sys) and delete them */
457 do {
458 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000459 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000460 while (PyDict_Next(modules, &pos, &key, &value)) {
461 if (value->ob_refcnt != 1)
462 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000463 if (PyString_Check(key) && PyModule_Check(value)) {
464 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000465 if (strcmp(name, "__builtin__") == 0)
466 continue;
467 if (strcmp(name, "sys") == 0)
468 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000469 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000470 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000471 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000472 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000473 PyDict_SetItem(modules, key, Py_None);
474 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000475 }
476 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000477 } while (ndone > 0);
478
Guido van Rossum758eec01998-01-19 21:58:26 +0000479 /* Next, delete all modules (still skipping __builtin__ and sys) */
480 pos = 0;
481 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000482 if (PyString_Check(key) && PyModule_Check(value)) {
483 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000484 if (strcmp(name, "__builtin__") == 0)
485 continue;
486 if (strcmp(name, "sys") == 0)
487 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000488 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000489 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000490 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000491 PyDict_SetItem(modules, key, Py_None);
492 }
493 }
494
495 /* Next, delete sys and __builtin__ (in that order) */
496 value = PyDict_GetItemString(modules, "sys");
497 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000498 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000499 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000500 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000501 PyDict_SetItemString(modules, "sys", Py_None);
502 }
503 value = PyDict_GetItemString(modules, "__builtin__");
504 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000505 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000506 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000507 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000508 PyDict_SetItemString(modules, "__builtin__", Py_None);
509 }
510
511 /* Finally, clear and delete the modules directory */
512 PyDict_Clear(modules);
513 interp->modules = NULL;
514 Py_DECREF(modules);
Collin Winter276887b2007-03-12 16:11:39 +0000515 Py_CLEAR(interp->modules_reloading);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000516}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000517
518
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000519/* Helper for pythonrun.c -- return magic number */
520
521long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000522PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000523{
Guido van Rossum96774c12000-05-01 20:19:08 +0000524 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000525}
526
527
Guido van Rossum25ce5661997-08-02 03:10:38 +0000528/* Magic for extension modules (built-in as well as dynamically
529 loaded). To prevent initializing an extension module more than
530 once, we keep a static dictionary 'extensions' keyed by module name
531 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000532 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000533 dictionary is stored by calling _PyImport_FixupExtension()
534 immediately after the module initialization function succeeds. A
535 copy can be retrieved from there by calling
536 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000537
Guido van Rossum79f25d91997-04-29 20:08:16 +0000538PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000539_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000540{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000541 PyObject *modules, *mod, *dict, *copy;
542 if (extensions == NULL) {
543 extensions = PyDict_New();
544 if (extensions == NULL)
545 return NULL;
546 }
547 modules = PyImport_GetModuleDict();
548 mod = PyDict_GetItemString(modules, name);
549 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000550 PyErr_Format(PyExc_SystemError,
551 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000552 return NULL;
553 }
554 dict = PyModule_GetDict(mod);
555 if (dict == NULL)
556 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000557 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000558 if (copy == NULL)
559 return NULL;
560 PyDict_SetItemString(extensions, filename, copy);
561 Py_DECREF(copy);
562 return copy;
563}
564
565PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000566_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000567{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000568 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000569 if (extensions == NULL)
570 return NULL;
571 dict = PyDict_GetItemString(extensions, filename);
572 if (dict == NULL)
573 return NULL;
574 mod = PyImport_AddModule(name);
575 if (mod == NULL)
576 return NULL;
577 mdict = PyModule_GetDict(mod);
578 if (mdict == NULL)
579 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000580 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000581 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000582 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000583 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000584 name, filename);
585 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586}
587
588
589/* Get the module object corresponding to a module name.
590 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000591 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000592 Because the former action is most common, THIS DOES NOT RETURN A
593 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000594
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000596PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000597{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000598 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000599 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000600
Guido van Rossum25ce5661997-08-02 03:10:38 +0000601 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000602 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000603 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000605 if (m == NULL)
606 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000607 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000608 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000609 return NULL;
610 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000612
613 return m;
614}
615
Tim Peters1cd70172004-08-02 03:52:12 +0000616/* Remove name from sys.modules, if it's there. */
617static void
618_RemoveModule(const char *name)
619{
620 PyObject *modules = PyImport_GetModuleDict();
621 if (PyDict_GetItemString(modules, name) == NULL)
622 return;
623 if (PyDict_DelItemString(modules, name) < 0)
624 Py_FatalError("import: deleting existing key in"
625 "sys.modules failed");
626}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000627
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000628/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000629 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
630 * removed from sys.modules, to avoid leaving damaged module objects
631 * in sys.modules. The caller may wish to restore the original
632 * module object (if any) in this case; PyImport_ReloadModule is an
633 * example.
634 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000635PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000636PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000637{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000638 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
639}
640
641PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000642PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000643{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000644 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000645 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000646
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648 if (m == NULL)
649 return NULL;
Jeremy Hyltonecd91292004-01-02 23:25:32 +0000650 /* If the module is being reloaded, we get the old module back
651 and re-use its dict to exec the new code. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000652 d = PyModule_GetDict(m);
653 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
654 if (PyDict_SetItemString(d, "__builtins__",
655 PyEval_GetBuiltins()) != 0)
Tim Peters1cd70172004-08-02 03:52:12 +0000656 goto error;
Guido van Rossum6135a871995-01-09 17:53:26 +0000657 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000658 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000659 v = NULL;
660 if (pathname != NULL) {
661 v = PyString_FromString(pathname);
662 if (v == NULL)
663 PyErr_Clear();
664 }
665 if (v == NULL) {
666 v = ((PyCodeObject *)co)->co_filename;
667 Py_INCREF(v);
668 }
669 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000670 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000671 Py_DECREF(v);
672
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000673 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674 if (v == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000675 goto error;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000676 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000677
Guido van Rossum25ce5661997-08-02 03:10:38 +0000678 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000679 PyErr_Format(PyExc_ImportError,
680 "Loaded module %.200s not found in sys.modules",
681 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000682 return NULL;
683 }
684
Guido van Rossum79f25d91997-04-29 20:08:16 +0000685 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000686
687 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000688
689 error:
690 _RemoveModule(name);
691 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000692}
693
694
695/* Given a pathname for a Python source file, fill a buffer with the
696 pathname for the corresponding compiled file. Return the pathname
697 for the compiled file, or NULL if there's no space in the buffer.
698 Doesn't set an exception. */
699
700static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000701make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000702{
Tim Petersc1731372001-08-04 08:12:36 +0000703 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000704 if (len+2 > buflen)
705 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000706
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000707#ifdef MS_WINDOWS
Tim Petersc1731372001-08-04 08:12:36 +0000708 /* Treat .pyw as if it were .py. The case of ".pyw" must match
709 that used in _PyImport_StandardFiletab. */
710 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
711 --len; /* pretend 'w' isn't there */
712#endif
713 memcpy(buf, pathname, len);
714 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
715 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000716
717 return buf;
718}
719
720
721/* Given a pathname for a Python source file, its time of last
722 modification, and a pathname for a compiled file, check whether the
723 compiled file represents the same version of the source. If so,
724 return a FILE pointer for the compiled file, positioned just after
725 the header; if not, return NULL.
726 Doesn't set an exception. */
727
728static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000729check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000730{
731 FILE *fp;
732 long magic;
733 long pyc_mtime;
734
735 fp = fopen(cpathname, "rb");
736 if (fp == NULL)
737 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000738 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000739 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000741 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000742 fclose(fp);
743 return NULL;
744 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000745 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000748 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000749 fclose(fp);
750 return NULL;
751 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000753 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000754 return fp;
755}
756
757
758/* Read a code object from a file and check it for validity */
759
Guido van Rossum79f25d91997-04-29 20:08:16 +0000760static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000761read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000762{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000763 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000764
Tim Petersd9b9ac82001-01-28 00:27:39 +0000765 co = PyMarshal_ReadLastObjectFromFile(fp);
Armin Rigo01ab2792004-03-26 15:09:27 +0000766 if (co == NULL)
767 return NULL;
768 if (!PyCode_Check(co)) {
769 PyErr_Format(PyExc_ImportError,
770 "Non-code object in %.200s", cpathname);
771 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000772 return NULL;
773 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000774 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000775}
776
777
778/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000779 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000780
Guido van Rossum79f25d91997-04-29 20:08:16 +0000781static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000782load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000783{
784 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000785 PyCodeObject *co;
786 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000787
Guido van Rossum79f25d91997-04-29 20:08:16 +0000788 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000789 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000790 PyErr_Format(PyExc_ImportError,
791 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000792 return NULL;
793 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000795 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000796 if (co == NULL)
797 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000798 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000799 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000800 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000801 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000803
804 return m;
805}
806
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000807/* Parse a source file and return the corresponding code object */
808
Guido van Rossum79f25d91997-04-29 20:08:16 +0000809static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000811{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 PyCodeObject *co = NULL;
813 mod_ty mod;
Neal Norwitz2a399b02006-09-11 04:28:16 +0000814 PyArena *arena = PyArena_New();
815 if (arena == NULL)
816 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000817
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 mod = PyParser_ASTFromFile(fp, pathname, Py_file_input, 0, 0, 0,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000819 NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 if (mod) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000821 co = PyAST_Compile(mod, pathname, NULL, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 }
Neal Norwitz2a399b02006-09-11 04:28:16 +0000823 PyArena_Free(arena);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000824 return co;
825}
826
827
Guido van Rossum55a83382000-09-20 20:31:38 +0000828/* Helper to open a bytecode file for writing in exclusive mode */
829
830static FILE *
831open_exclusive(char *filename)
832{
833#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
834 /* Use O_EXCL to avoid a race condition when another process tries to
835 write the same file. When that happens, our open() call fails,
836 which is just fine (since it's only a cache).
837 XXX If the file exists and is writable but the directory is not
838 writable, the file will never be written. Oh well.
839 */
840 int fd;
841 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000842 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
843#ifdef O_BINARY
844 |O_BINARY /* necessary for Windows */
845#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000846#ifdef __VMS
Martin v. Löwis18e16552006-02-15 17:27:45 +0000847 , 0666, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000848#else
Martin v. Löwis18e16552006-02-15 17:27:45 +0000849 , 0666
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000850#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000851 );
Guido van Rossum55a83382000-09-20 20:31:38 +0000852 if (fd < 0)
853 return NULL;
854 return fdopen(fd, "wb");
855#else
856 /* Best we can do -- on Windows this can't happen anyway */
857 return fopen(filename, "wb");
858#endif
859}
860
861
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000862/* Write a compiled module to a file, placing the time of last
863 modification of its source into the header.
864 Errors are ignored, if a write error occurs an attempt is made to
865 remove the file. */
866
867static void
Martin v. Löwis18e16552006-02-15 17:27:45 +0000868write_compiled_module(PyCodeObject *co, char *cpathname, time_t mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000869{
870 FILE *fp;
871
Guido van Rossum55a83382000-09-20 20:31:38 +0000872 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000873 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000874 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000875 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000876 "# can't create %s\n", cpathname);
877 return;
878 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000879 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000880 /* First write a 0 for mtime */
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000881 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
882 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Armin Rigo01ab2792004-03-26 15:09:27 +0000883 if (fflush(fp) != 0 || ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000884 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000885 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000886 /* Don't keep partial file */
887 fclose(fp);
888 (void) unlink(cpathname);
889 return;
890 }
891 /* Now write the true mtime */
892 fseek(fp, 4L, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000893 assert(mtime < LONG_MAX);
Martin v. Löwis725507b2006-03-07 12:08:51 +0000894 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000895 fflush(fp);
896 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000898 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000899}
900
901
902/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000903 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
904 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000905
Guido van Rossum79f25d91997-04-29 20:08:16 +0000906static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000907load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000908{
Fred Drake4c82b232000-06-30 16:18:57 +0000909 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000910 FILE *fpc;
911 char buf[MAXPATHLEN+1];
912 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000913 PyCodeObject *co;
914 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000915
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000916 mtime = PyOS_GetLastModificationTime(pathname, fp);
Neal Norwitz708e51a2005-10-03 04:48:15 +0000917 if (mtime == (time_t)(-1)) {
918 PyErr_Format(PyExc_RuntimeError,
919 "unable to get modification time from '%s'",
920 pathname);
Fred Drake4c82b232000-06-30 16:18:57 +0000921 return NULL;
Neal Norwitz708e51a2005-10-03 04:48:15 +0000922 }
Fred Drake4c82b232000-06-30 16:18:57 +0000923#if SIZEOF_TIME_T > 4
924 /* Python's .pyc timestamp handling presumes that the timestamp fits
925 in 4 bytes. This will be fine until sometime in the year 2038,
926 when a 4-byte signed time_t will overflow.
927 */
928 if (mtime >> 32) {
929 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000930 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000931 return NULL;
932 }
933#endif
Tim Peters36515e22001-11-18 04:06:29 +0000934 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000935 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000936 if (cpathname != NULL &&
937 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000938 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000939 fclose(fpc);
940 if (co == NULL)
941 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000942 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000943 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000944 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000945 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000946 }
947 else {
948 co = parse_source_module(pathname, fp);
949 if (co == NULL)
950 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000951 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000952 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000953 name, pathname);
Neal Norwitz3cb31ac2006-08-13 18:10:47 +0000954 if (cpathname)
955 write_compiled_module(co, cpathname, mtime);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000956 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000957 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000958 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000959
960 return m;
961}
962
963
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000964/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +0000965static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
966static struct filedescr *find_module(char *, char *, PyObject *,
967 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000968static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000969
970/* Load a package and return its module object WITH INCREMENTED
971 REFERENCE COUNT */
972
973static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000974load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000975{
Tim Peters1cd70172004-08-02 03:52:12 +0000976 PyObject *m, *d;
977 PyObject *file = NULL;
978 PyObject *path = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000979 int err;
980 char buf[MAXPATHLEN+1];
981 FILE *fp = NULL;
982 struct filedescr *fdp;
983
984 m = PyImport_AddModule(name);
985 if (m == NULL)
986 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000987 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000988 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000989 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000990 d = PyModule_GetDict(m);
991 file = PyString_FromString(pathname);
992 if (file == NULL)
Tim Peters1cd70172004-08-02 03:52:12 +0000993 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000994 path = Py_BuildValue("[O]", file);
Tim Peters1cd70172004-08-02 03:52:12 +0000995 if (path == NULL)
996 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000997 err = PyDict_SetItemString(d, "__file__", file);
998 if (err == 0)
999 err = PyDict_SetItemString(d, "__path__", path);
Tim Peters1cd70172004-08-02 03:52:12 +00001000 if (err != 0)
1001 goto error;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001002 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00001003 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001004 if (fdp == NULL) {
1005 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1006 PyErr_Clear();
Thomas Heller25653242004-06-07 15:04:10 +00001007 Py_INCREF(m);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001008 }
1009 else
1010 m = NULL;
1011 goto cleanup;
1012 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001013 m = load_module(name, fp, buf, fdp->type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001014 if (fp != NULL)
1015 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00001016 goto cleanup;
1017
1018 error:
1019 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001020 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001021 Py_XDECREF(path);
1022 Py_XDECREF(file);
1023 return m;
1024}
1025
1026
1027/* Helper to test for built-in module */
1028
1029static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001030is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001031{
1032 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +00001033 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1034 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1035 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001036 return -1;
1037 else
1038 return 1;
1039 }
1040 }
1041 return 0;
1042}
1043
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001044
Just van Rossum52e14d62002-12-30 22:08:05 +00001045/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1046 possibly by fetching it from the path_importer_cache dict. If it
Brett Cannon94b69f62006-09-28 22:10:14 +00001047 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001048 that can handle the path item. Return None if no hook could;
1049 this tells our caller it should fall back to the builtin
1050 import mechanism. Cache the result in path_importer_cache.
1051 Returns a borrowed reference. */
1052
1053static PyObject *
1054get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
1055 PyObject *p)
1056{
1057 PyObject *importer;
Martin v. Löwis725507b2006-03-07 12:08:51 +00001058 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001059
1060 /* These conditions are the caller's responsibility: */
1061 assert(PyList_Check(path_hooks));
1062 assert(PyDict_Check(path_importer_cache));
1063
1064 nhooks = PyList_Size(path_hooks);
1065 if (nhooks < 0)
1066 return NULL; /* Shouldn't happen */
1067
1068 importer = PyDict_GetItem(path_importer_cache, p);
1069 if (importer != NULL)
1070 return importer;
1071
1072 /* set path_importer_cache[p] to None to avoid recursion */
1073 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1074 return NULL;
1075
1076 for (j = 0; j < nhooks; j++) {
1077 PyObject *hook = PyList_GetItem(path_hooks, j);
1078 if (hook == NULL)
1079 return NULL;
Georg Brandl684fd0c2006-05-25 19:15:31 +00001080 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Just van Rossum52e14d62002-12-30 22:08:05 +00001081 if (importer != NULL)
1082 break;
1083
1084 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1085 return NULL;
1086 }
1087 PyErr_Clear();
1088 }
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00001089 if (importer == NULL) {
1090 importer = PyObject_CallFunctionObjArgs(
1091 (PyObject *)&NullImporterType, p, NULL
1092 );
1093 if (importer == NULL) {
1094 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1095 PyErr_Clear();
1096 return Py_None;
1097 }
1098 }
1099 }
1100 if (importer != NULL) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001101 int err = PyDict_SetItem(path_importer_cache, p, importer);
1102 Py_DECREF(importer);
1103 if (err != 0)
1104 return NULL;
1105 }
1106 return importer;
1107}
1108
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001109/* Search the path (default sys.path) for a module. Return the
1110 corresponding filedescr struct, and (via return arguments) the
1111 pathname and an open file. Return NULL if the module is not found. */
1112
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001113#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001114extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001115 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001116#endif
1117
Martin v. Löwis18e16552006-02-15 17:27:45 +00001118static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001119static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001120static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001121
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001122static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001123find_module(char *fullname, char *subname, PyObject *path, char *buf,
1124 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001125{
Martin v. Löwis725507b2006-03-07 12:08:51 +00001126 Py_ssize_t i, npath;
Fred Drake4c82b232000-06-30 16:18:57 +00001127 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001128 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001129 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001130 FILE *fp = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001131 PyObject *path_hooks, *path_importer_cache;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001132#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001133 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001134#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001135 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1136 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1137 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +00001138 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001139#if defined(PYOS_OS2)
1140 size_t saved_len;
1141 size_t saved_namelen;
1142 char *saved_buf = NULL;
1143#endif
Just van Rossum52e14d62002-12-30 22:08:05 +00001144 if (p_loader != NULL)
1145 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001146
Just van Rossum52e14d62002-12-30 22:08:05 +00001147 if (strlen(subname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001148 PyErr_SetString(PyExc_OverflowError,
1149 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +00001150 return NULL;
1151 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001152 strcpy(name, subname);
1153
1154 /* sys.meta_path import hook */
1155 if (p_loader != NULL) {
1156 PyObject *meta_path;
1157
1158 meta_path = PySys_GetObject("meta_path");
1159 if (meta_path == NULL || !PyList_Check(meta_path)) {
1160 PyErr_SetString(PyExc_ImportError,
1161 "sys.meta_path must be a list of "
1162 "import hooks");
1163 return NULL;
1164 }
1165 Py_INCREF(meta_path); /* zap guard */
1166 npath = PyList_Size(meta_path);
1167 for (i = 0; i < npath; i++) {
1168 PyObject *loader;
1169 PyObject *hook = PyList_GetItem(meta_path, i);
1170 loader = PyObject_CallMethod(hook, "find_module",
1171 "sO", fullname,
1172 path != NULL ?
1173 path : Py_None);
1174 if (loader == NULL) {
1175 Py_DECREF(meta_path);
1176 return NULL; /* true error */
1177 }
1178 if (loader != Py_None) {
1179 /* a loader was found */
1180 *p_loader = loader;
1181 Py_DECREF(meta_path);
1182 return &importhookdescr;
1183 }
1184 Py_DECREF(loader);
1185 }
1186 Py_DECREF(meta_path);
1187 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001188
1189 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001190 /* The only type of submodule allowed inside a "frozen"
1191 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +00001192 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1193 PyErr_SetString(PyExc_ImportError,
1194 "full frozen module name too long");
1195 return NULL;
1196 }
1197 strcpy(buf, PyString_AsString(path));
1198 strcat(buf, ".");
1199 strcat(buf, name);
1200 strcpy(name, buf);
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001201 if (find_frozen(name) != NULL) {
1202 strcpy(buf, name);
1203 return &fd_frozen;
1204 }
1205 PyErr_Format(PyExc_ImportError,
1206 "No frozen submodule named %.200s", name);
1207 return NULL;
Guido van Rossum0506a431998-08-11 15:07:39 +00001208 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001209 if (path == NULL) {
1210 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001211 strcpy(buf, name);
1212 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001213 }
Greg Ward201baee2001-10-04 14:52:06 +00001214 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +00001215 strcpy(buf, name);
1216 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001217 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001218
Guido van Rossumac279101996-08-22 23:10:58 +00001219#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001220 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1221 if (fp != NULL) {
1222 *p_fp = fp;
1223 return fdp;
1224 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001225#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +00001226 path = PySys_GetObject("path");
1227 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001228 if (path == NULL || !PyList_Check(path)) {
1229 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +00001230 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001231 return NULL;
1232 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001233
1234 path_hooks = PySys_GetObject("path_hooks");
1235 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
1236 PyErr_SetString(PyExc_ImportError,
1237 "sys.path_hooks must be a list of "
1238 "import hooks");
1239 return NULL;
1240 }
1241 path_importer_cache = PySys_GetObject("path_importer_cache");
1242 if (path_importer_cache == NULL ||
1243 !PyDict_Check(path_importer_cache)) {
1244 PyErr_SetString(PyExc_ImportError,
1245 "sys.path_importer_cache must be a dict");
1246 return NULL;
1247 }
1248
Guido van Rossum79f25d91997-04-29 20:08:16 +00001249 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001250 namelen = strlen(name);
1251 for (i = 0; i < npath; i++) {
Walter Dörwald3430d702002-06-17 10:43:59 +00001252 PyObject *copy = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001253 PyObject *v = PyList_GetItem(path, i);
Neal Norwitz3cb31ac2006-08-13 18:10:47 +00001254 if (!v)
1255 return NULL;
Walter Dörwald3430d702002-06-17 10:43:59 +00001256#ifdef Py_USING_UNICODE
1257 if (PyUnicode_Check(v)) {
1258 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1259 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1260 if (copy == NULL)
1261 return NULL;
1262 v = copy;
1263 }
1264 else
1265#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001266 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001267 continue;
Neal Norwitz2aa9a5d2006-03-20 01:53:23 +00001268 len = PyString_GET_SIZE(v);
Walter Dörwald3430d702002-06-17 10:43:59 +00001269 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1270 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001271 continue; /* Too long */
Walter Dörwald3430d702002-06-17 10:43:59 +00001272 }
Neal Norwitz2aa9a5d2006-03-20 01:53:23 +00001273 strcpy(buf, PyString_AS_STRING(v));
Walter Dörwald3430d702002-06-17 10:43:59 +00001274 if (strlen(buf) != len) {
1275 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001276 continue; /* v contains '\0' */
Walter Dörwald3430d702002-06-17 10:43:59 +00001277 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001278
1279 /* sys.path_hooks import hook */
1280 if (p_loader != NULL) {
1281 PyObject *importer;
1282
1283 importer = get_path_importer(path_importer_cache,
1284 path_hooks, v);
Neal Norwitza4df11d2006-07-06 04:28:59 +00001285 if (importer == NULL) {
1286 Py_XDECREF(copy);
Just van Rossum52e14d62002-12-30 22:08:05 +00001287 return NULL;
Neal Norwitza4df11d2006-07-06 04:28:59 +00001288 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001289 /* Note: importer is a borrowed reference */
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00001290 if (importer != Py_None) {
Just van Rossum52e14d62002-12-30 22:08:05 +00001291 PyObject *loader;
1292 loader = PyObject_CallMethod(importer,
1293 "find_module",
1294 "s", fullname);
Neal Norwitza4df11d2006-07-06 04:28:59 +00001295 Py_XDECREF(copy);
Just van Rossum52e14d62002-12-30 22:08:05 +00001296 if (loader == NULL)
1297 return NULL; /* error */
1298 if (loader != Py_None) {
1299 /* a loader was found */
1300 *p_loader = loader;
1301 return &importhookdescr;
1302 }
1303 Py_DECREF(loader);
Georg Brandlf4ef1162006-05-26 18:03:31 +00001304 continue;
Just van Rossum52e14d62002-12-30 22:08:05 +00001305 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001306 }
Georg Brandlf4ef1162006-05-26 18:03:31 +00001307 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001308
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001309 if (len > 0 && buf[len-1] != SEP
1310#ifdef ALTSEP
1311 && buf[len-1] != ALTSEP
1312#endif
1313 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001314 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001315 strcpy(buf+len, name);
1316 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001317
1318 /* Check for package import (buf holds a directory name,
1319 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001320#ifdef HAVE_STAT
Walter Dörwald3430d702002-06-17 10:43:59 +00001321 if (stat(buf, &statbuf) == 0 && /* it exists */
1322 S_ISDIR(statbuf.st_mode) && /* it's a directory */
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001323 case_ok(buf, len, namelen, name)) { /* case matches */
1324 if (find_init_module(buf)) { /* and has __init__.py */
1325 Py_XDECREF(copy);
1326 return &fd_package;
1327 }
1328 else {
1329 char warnstr[MAXPATHLEN+80];
1330 sprintf(warnstr, "Not importing directory "
1331 "'%.*s': missing __init__.py",
1332 MAXPATHLEN, buf);
1333 if (PyErr_Warn(PyExc_ImportWarning,
1334 warnstr)) {
1335 Py_XDECREF(copy);
1336 return NULL;
1337 }
1338 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001339 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001340#else
1341 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001342#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001343 if (isdir(buf) &&
Walter Dörwald3430d702002-06-17 10:43:59 +00001344 case_ok(buf, len, namelen, name)) {
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001345 if (find_init_module(buf)) {
1346 Py_XDECREF(copy);
1347 return &fd_package;
1348 }
1349 else {
1350 char warnstr[MAXPATHLEN+80];
1351 sprintf(warnstr, "Not importing directory "
1352 "'%.*s': missing __init__.py",
1353 MAXPATHLEN, buf);
1354 if (PyErr_Warn(PyExc_ImportWarning,
1355 warnstr)) {
1356 Py_XDECREF(copy);
1357 return NULL;
1358 }
Walter Dörwald3430d702002-06-17 10:43:59 +00001359 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001360#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001361#endif
Andrew MacIntyred9400542002-02-26 11:41:34 +00001362#if defined(PYOS_OS2)
1363 /* take a snapshot of the module spec for restoration
1364 * after the 8 character DLL hackery
1365 */
1366 saved_buf = strdup(buf);
1367 saved_len = len;
1368 saved_namelen = namelen;
1369#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001370 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Georg Brandladd36e52007-08-23 18:08:06 +00001371#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001372 /* OS/2 limits DLLs to 8 character names (w/o
1373 extension)
Andrew MacIntyred9400542002-02-26 11:41:34 +00001374 * so if the name is longer than that and its a
1375 * dynamically loaded module we're going to try,
1376 * truncate the name before trying
1377 */
Just van Rossum52e14d62002-12-30 22:08:05 +00001378 if (strlen(subname) > 8) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001379 /* is this an attempt to load a C extension? */
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001380 const struct filedescr *scan;
1381 scan = _PyImport_DynLoadFiletab;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001382 while (scan->suffix != NULL) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001383 if (!strcmp(scan->suffix, fdp->suffix))
Andrew MacIntyred9400542002-02-26 11:41:34 +00001384 break;
1385 else
1386 scan++;
1387 }
1388 if (scan->suffix != NULL) {
1389 /* yes, so truncate the name */
1390 namelen = 8;
Just van Rossum52e14d62002-12-30 22:08:05 +00001391 len -= strlen(subname) - namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001392 buf[len] = '\0';
1393 }
1394 }
1395#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001396 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001397 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001398 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansenc88da1f2002-05-28 10:58:19 +00001399 filemode = fdp->mode;
Tim Peters86c7d2f2004-08-01 23:24:21 +00001400 if (filemode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00001401 filemode = "r" PY_STDIOTEXTMODE;
Jack Jansenc88da1f2002-05-28 10:58:19 +00001402 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001403 if (fp != NULL) {
1404 if (case_ok(buf, len, namelen, name))
1405 break;
1406 else { /* continue search */
1407 fclose(fp);
1408 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001409 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001410 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001411#if defined(PYOS_OS2)
1412 /* restore the saved snapshot */
1413 strcpy(buf, saved_buf);
1414 len = saved_len;
1415 namelen = saved_namelen;
1416#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001417 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001418#if defined(PYOS_OS2)
1419 /* don't need/want the module name snapshot anymore */
1420 if (saved_buf)
1421 {
1422 free(saved_buf);
1423 saved_buf = NULL;
1424 }
1425#endif
Walter Dörwald3430d702002-06-17 10:43:59 +00001426 Py_XDECREF(copy);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001427 if (fp != NULL)
1428 break;
1429 }
1430 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001431 PyErr_Format(PyExc_ImportError,
1432 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001433 return NULL;
1434 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001435 *p_fp = fp;
1436 return fdp;
1437}
1438
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001439/* Helpers for main.c
1440 * Find the source file corresponding to a named module
1441 */
1442struct filedescr *
1443_PyImport_FindModule(const char *name, PyObject *path, char *buf,
1444 size_t buflen, FILE **p_fp, PyObject **p_loader)
1445{
1446 return find_module((char *) name, (char *) name, path,
1447 buf, buflen, p_fp, p_loader);
1448}
1449
1450PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1451{
1452 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
1453}
1454
Martin v. Löwis18e16552006-02-15 17:27:45 +00001455/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001456 * The arguments here are tricky, best shown by example:
1457 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1458 * ^ ^ ^ ^
1459 * |--------------------- buf ---------------------|
1460 * |------------------- len ------------------|
1461 * |------ name -------|
1462 * |----- namelen -----|
1463 * buf is the full path, but len only counts up to (& exclusive of) the
1464 * extension. name is the module name, also exclusive of extension.
1465 *
1466 * We've already done a successful stat() or fopen() on buf, so know that
1467 * there's some match, possibly case-insensitive.
1468 *
Tim Peters50d8d372001-02-28 05:34:27 +00001469 * case_ok() is to return 1 if there's a case-sensitive match for
1470 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1471 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001472 *
Tim Peters50d8d372001-02-28 05:34:27 +00001473 * case_ok() is used to implement case-sensitive import semantics even
1474 * on platforms with case-insensitive filesystems. It's trivial to implement
1475 * for case-sensitive filesystems. It's pretty much a cross-platform
1476 * nightmare for systems with case-insensitive filesystems.
1477 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001478
Tim Peters50d8d372001-02-28 05:34:27 +00001479/* First we may need a pile of platform-specific header files; the sequence
1480 * of #if's here should match the sequence in the body of case_ok().
1481 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001482#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001483#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001484
Tim Peters50d8d372001-02-28 05:34:27 +00001485#elif defined(DJGPP)
1486#include <dir.h>
1487
Jason Tishler7961aa62005-05-20 00:56:54 +00001488#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001489#include <sys/types.h>
1490#include <dirent.h>
1491
Andrew MacIntyred9400542002-02-26 11:41:34 +00001492#elif defined(PYOS_OS2)
1493#define INCL_DOS
1494#define INCL_DOSERRORS
1495#define INCL_NOPMAPI
1496#include <os2.h>
1497
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001498#elif defined(RISCOS)
1499#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001500#endif
1501
Guido van Rossum0980bd91998-02-13 17:18:36 +00001502static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001503case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001504{
Tim Peters50d8d372001-02-28 05:34:27 +00001505/* Pick a platform-specific implementation; the sequence of #if's here should
1506 * match the sequence just above.
1507 */
1508
Jason Tishler7961aa62005-05-20 00:56:54 +00001509/* MS_WINDOWS */
1510#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001511 WIN32_FIND_DATA data;
1512 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001513
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001514 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001515 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001516
Guido van Rossum0980bd91998-02-13 17:18:36 +00001517 h = FindFirstFile(buf, &data);
1518 if (h == INVALID_HANDLE_VALUE) {
1519 PyErr_Format(PyExc_NameError,
1520 "Can't find file for module %.100s\n(filename %.300s)",
1521 name, buf);
1522 return 0;
1523 }
1524 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001525 return strncmp(data.cFileName, name, namelen) == 0;
1526
1527/* DJGPP */
1528#elif defined(DJGPP)
1529 struct ffblk ffblk;
1530 int done;
1531
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001532 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001533 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001534
1535 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1536 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001537 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001538 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001539 name, buf);
1540 return 0;
1541 }
Tim Peters50d8d372001-02-28 05:34:27 +00001542 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001543
Jason Tishler7961aa62005-05-20 00:56:54 +00001544/* new-fangled macintosh (macosx) or Cygwin */
1545#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001546 DIR *dirp;
1547 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001548 char dirname[MAXPATHLEN + 1];
1549 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001550
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001551 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001552 return 1;
1553
Tim Petersd1e87a82001-03-01 18:12:00 +00001554 /* Copy the dir component into dirname; substitute "." if empty */
1555 if (dirlen <= 0) {
1556 dirname[0] = '.';
1557 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001558 }
1559 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001560 assert(dirlen <= MAXPATHLEN);
1561 memcpy(dirname, buf, dirlen);
1562 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001563 }
1564 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001565 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001566 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001567 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001568 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001569 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001570#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001571 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001572#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001573 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001574#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001575 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001576 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001577 (void)closedir(dirp);
1578 return 1; /* Found */
1579 }
1580 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001581 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001582 }
Tim Peters430f5d42001-03-01 01:30:56 +00001583 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001584
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001585/* RISC OS */
1586#elif defined(RISCOS)
1587 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1588 char buf2[MAXPATHLEN+2];
1589 char *nameWithExt = buf+len-namelen;
1590 int canonlen;
1591 os_error *e;
1592
1593 if (Py_GETENV("PYTHONCASEOK") != NULL)
1594 return 1;
1595
1596 /* workaround:
1597 append wildcard, otherwise case of filename wouldn't be touched */
1598 strcpy(buf2, buf);
1599 strcat(buf2, "*");
1600
1601 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1602 canonlen = MAXPATHLEN+1-canonlen;
1603 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1604 return 0;
1605 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1606 return 1; /* match */
1607
1608 return 0;
1609
Andrew MacIntyred9400542002-02-26 11:41:34 +00001610/* OS/2 */
1611#elif defined(PYOS_OS2)
1612 HDIR hdir = 1;
1613 ULONG srchcnt = 1;
1614 FILEFINDBUF3 ffbuf;
1615 APIRET rc;
1616
1617 if (getenv("PYTHONCASEOK") != NULL)
1618 return 1;
1619
1620 rc = DosFindFirst(buf,
1621 &hdir,
1622 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1623 &ffbuf, sizeof(ffbuf),
1624 &srchcnt,
1625 FIL_STANDARD);
1626 if (rc != NO_ERROR)
1627 return 0;
1628 return strncmp(ffbuf.achName, name, namelen) == 0;
1629
Tim Peters50d8d372001-02-28 05:34:27 +00001630/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1631#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001632 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001633
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001634#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001635}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001636
Guido van Rossum0980bd91998-02-13 17:18:36 +00001637
Guido van Rossum197346f1997-10-31 18:38:52 +00001638#ifdef HAVE_STAT
1639/* Helper to look for __init__.py or __init__.py[co] in potential package */
1640static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001641find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001642{
Tim Peters0f9431f2001-07-05 03:47:53 +00001643 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001644 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001645 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001646 struct stat statbuf;
1647
Tim Peters0f9431f2001-07-05 03:47:53 +00001648/* For calling case_ok(buf, len, namelen, name):
1649 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1650 * ^ ^ ^ ^
1651 * |--------------------- buf ---------------------|
1652 * |------------------- len ------------------|
1653 * |------ name -------|
1654 * |----- namelen -----|
1655 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001656 if (save_len + 13 >= MAXPATHLEN)
1657 return 0;
1658 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001659 pname = buf + i;
1660 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001661 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001662 if (case_ok(buf,
1663 save_len + 9, /* len("/__init__") */
1664 8, /* len("__init__") */
1665 pname)) {
1666 buf[save_len] = '\0';
1667 return 1;
1668 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001669 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001670 i += strlen(pname);
1671 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001672 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001673 if (case_ok(buf,
1674 save_len + 9, /* len("/__init__") */
1675 8, /* len("__init__") */
1676 pname)) {
1677 buf[save_len] = '\0';
1678 return 1;
1679 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001680 }
1681 buf[save_len] = '\0';
1682 return 0;
1683}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001684
1685#else
1686
1687#ifdef RISCOS
1688static int
1689find_init_module(buf)
1690 char *buf;
1691{
1692 int save_len = strlen(buf);
1693 int i = save_len;
1694
1695 if (save_len + 13 >= MAXPATHLEN)
1696 return 0;
1697 buf[i++] = SEP;
1698 strcpy(buf+i, "__init__/py");
1699 if (isfile(buf)) {
1700 buf[save_len] = '\0';
1701 return 1;
1702 }
1703
1704 if (Py_OptimizeFlag)
1705 strcpy(buf+i, "o");
1706 else
1707 strcpy(buf+i, "c");
1708 if (isfile(buf)) {
1709 buf[save_len] = '\0';
1710 return 1;
1711 }
1712 buf[save_len] = '\0';
1713 return 0;
1714}
1715#endif /*RISCOS*/
1716
Guido van Rossum197346f1997-10-31 18:38:52 +00001717#endif /* HAVE_STAT */
1718
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001719
Tim Petersdbd9ba62000-07-09 03:09:57 +00001720static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001721
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001722/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001723 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001724
Guido van Rossum79f25d91997-04-29 20:08:16 +00001725static PyObject *
Just van Rossum52e14d62002-12-30 22:08:05 +00001726load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001727{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001728 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001729 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001730 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001731
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001732 /* First check that there's an open file (if we need one) */
1733 switch (type) {
1734 case PY_SOURCE:
1735 case PY_COMPILED:
1736 if (fp == NULL) {
1737 PyErr_Format(PyExc_ValueError,
1738 "file object required for import (type code %d)",
1739 type);
1740 return NULL;
1741 }
1742 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001743
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001744 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001745
1746 case PY_SOURCE:
1747 m = load_source_module(name, buf, fp);
1748 break;
1749
1750 case PY_COMPILED:
1751 m = load_compiled_module(name, buf, fp);
1752 break;
1753
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001754#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001755 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001756 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001757 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001758#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001759
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001760 case PKG_DIRECTORY:
1761 m = load_package(name, buf);
1762 break;
1763
1764 case C_BUILTIN:
1765 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001766 if (buf != NULL && buf[0] != '\0')
1767 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001768 if (type == C_BUILTIN)
1769 err = init_builtin(name);
1770 else
1771 err = PyImport_ImportFrozenModule(name);
1772 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001773 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001774 if (err == 0) {
1775 PyErr_Format(PyExc_ImportError,
1776 "Purported %s module %.200s not found",
1777 type == C_BUILTIN ?
1778 "builtin" : "frozen",
1779 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001780 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001781 }
1782 modules = PyImport_GetModuleDict();
1783 m = PyDict_GetItemString(modules, name);
1784 if (m == NULL) {
1785 PyErr_Format(
1786 PyExc_ImportError,
1787 "%s module %.200s not properly initialized",
1788 type == C_BUILTIN ?
1789 "builtin" : "frozen",
1790 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001791 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001792 }
1793 Py_INCREF(m);
1794 break;
1795
Just van Rossum52e14d62002-12-30 22:08:05 +00001796 case IMP_HOOK: {
1797 if (loader == NULL) {
1798 PyErr_SetString(PyExc_ImportError,
1799 "import hook without loader");
1800 return NULL;
1801 }
1802 m = PyObject_CallMethod(loader, "load_module", "s", name);
1803 break;
1804 }
1805
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001806 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001807 PyErr_Format(PyExc_ImportError,
1808 "Don't know how to import %.200s (type code %d)",
1809 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001810 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001811
1812 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001813
1814 return m;
1815}
1816
1817
1818/* Initialize a built-in module.
Brett Cannon5a9aa4f2006-10-03 21:58:55 +00001819 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001820 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001821
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001822static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001823init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001824{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001825 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001826
Greg Ward201baee2001-10-04 14:52:06 +00001827 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001828 return 1;
1829
Guido van Rossum771c6c81997-10-31 18:37:24 +00001830 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001831 if (strcmp(name, p->name) == 0) {
1832 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001833 PyErr_Format(PyExc_ImportError,
1834 "Cannot re-init internal module %.200s",
1835 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001836 return -1;
1837 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001838 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001839 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001840 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001841 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001842 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001843 if (_PyImport_FixupExtension(name, name) == NULL)
1844 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001845 return 1;
1846 }
1847 }
1848 return 0;
1849}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001850
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001851
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001852/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001853
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001854static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001855find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001856{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001857 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001858
Guido van Rossum79f25d91997-04-29 20:08:16 +00001859 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001860 if (p->name == NULL)
1861 return NULL;
1862 if (strcmp(p->name, name) == 0)
1863 break;
1864 }
1865 return p;
1866}
1867
Guido van Rossum79f25d91997-04-29 20:08:16 +00001868static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001869get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001870{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001871 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001872 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001873
1874 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001875 PyErr_Format(PyExc_ImportError,
1876 "No such frozen object named %.200s",
1877 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001878 return NULL;
1879 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001880 if (p->code == NULL) {
1881 PyErr_Format(PyExc_ImportError,
1882 "Excluded frozen object named %.200s",
1883 name);
1884 return NULL;
1885 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001886 size = p->size;
1887 if (size < 0)
1888 size = -size;
1889 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001890}
1891
1892/* Initialize a frozen module.
1893 Return 1 for succes, 0 if the module is not found, and -1 with
1894 an exception set if the initialization failed.
1895 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001896
1897int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001898PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001899{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001900 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001901 PyObject *co;
1902 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001903 int ispackage;
1904 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001905
1906 if (p == NULL)
1907 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001908 if (p->code == NULL) {
1909 PyErr_Format(PyExc_ImportError,
1910 "Excluded frozen object named %.200s",
1911 name);
1912 return -1;
1913 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001914 size = p->size;
1915 ispackage = (size < 0);
1916 if (ispackage)
1917 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001918 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001919 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001920 name, ispackage ? " package" : "");
1921 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001922 if (co == NULL)
1923 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001924 if (!PyCode_Check(co)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001925 PyErr_Format(PyExc_TypeError,
1926 "frozen object %.200s is not a code object",
1927 name);
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001928 goto err_return;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001929 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001930 if (ispackage) {
1931 /* Set __path__ to the package name */
1932 PyObject *d, *s;
1933 int err;
1934 m = PyImport_AddModule(name);
1935 if (m == NULL)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001936 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001937 d = PyModule_GetDict(m);
1938 s = PyString_InternFromString(name);
1939 if (s == NULL)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001940 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001941 err = PyDict_SetItemString(d, "__path__", s);
1942 Py_DECREF(s);
1943 if (err != 0)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001944 goto err_return;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001945 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001946 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001947 if (m == NULL)
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001948 goto err_return;
1949 Py_DECREF(co);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001950 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001951 return 1;
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00001952err_return:
1953 Py_DECREF(co);
1954 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001955}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001956
1957
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001958/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001959 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001960
Guido van Rossum79f25d91997-04-29 20:08:16 +00001961PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001962PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001963{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001964 PyObject *pname;
1965 PyObject *result;
1966
1967 pname = PyString_FromString(name);
Neal Norwitza11e4c12003-03-23 14:31:01 +00001968 if (pname == NULL)
1969 return NULL;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001970 result = PyImport_Import(pname);
1971 Py_DECREF(pname);
1972 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001973}
1974
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001975/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001976static PyObject *get_parent(PyObject *globals, char *buf,
1977 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001978static PyObject *load_next(PyObject *mod, PyObject *altmod,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001979 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001980static int mark_miss(char *name);
1981static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001982 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001983static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001984
1985/* The Magnum Opus of dotted-name import :-) */
1986
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001987static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001988import_module_level(char *name, PyObject *globals, PyObject *locals,
1989 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001990{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001991 char buf[MAXPATHLEN+1];
Martin v. Löwis18e16552006-02-15 17:27:45 +00001992 Py_ssize_t buflen = 0;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001993 PyObject *parent, *head, *next, *tail;
1994
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001995 parent = get_parent(globals, buf, &buflen, level);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001996 if (parent == NULL)
1997 return NULL;
1998
1999 head = load_next(parent, Py_None, &name, buf, &buflen);
2000 if (head == NULL)
2001 return NULL;
2002
2003 tail = head;
2004 Py_INCREF(tail);
2005 while (name) {
2006 next = load_next(tail, tail, &name, buf, &buflen);
2007 Py_DECREF(tail);
2008 if (next == NULL) {
2009 Py_DECREF(head);
2010 return NULL;
2011 }
2012 tail = next;
2013 }
Thomas Wouters8ddab272006-04-04 16:17:02 +00002014 if (tail == Py_None) {
2015 /* If tail is Py_None, both get_parent and load_next found
2016 an empty module name: someone called __import__("") or
2017 doctored faulty bytecode */
Thomas Wouters4bdaa272006-04-05 13:39:37 +00002018 Py_DECREF(tail);
2019 Py_DECREF(head);
Thomas Wouters8ddab272006-04-04 16:17:02 +00002020 PyErr_SetString(PyExc_ValueError,
2021 "Empty module name");
2022 return NULL;
2023 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002024
2025 if (fromlist != NULL) {
2026 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2027 fromlist = NULL;
2028 }
2029
2030 if (fromlist == NULL) {
2031 Py_DECREF(tail);
2032 return head;
2033 }
2034
2035 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002036 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002037 Py_DECREF(tail);
2038 return NULL;
2039 }
2040
2041 return tail;
2042}
2043
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002044/* For DLL compatibility */
2045#undef PyImport_ImportModuleEx
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002046PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002047PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
2048 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002049{
2050 PyObject *result;
2051 lock_import();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002052 result = import_module_level(name, globals, locals, fromlist, -1);
2053 if (unlock_import() < 0) {
2054 Py_XDECREF(result);
2055 PyErr_SetString(PyExc_RuntimeError,
2056 "not holding the import lock");
2057 return NULL;
2058 }
2059 return result;
2060}
2061#define PyImport_ImportModuleEx(n, g, l, f) \
2062 PyImport_ImportModuleLevel(n, g, l, f, -1);
2063
2064PyObject *
2065PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2066 PyObject *fromlist, int level)
2067{
2068 PyObject *result;
2069 lock_import();
2070 result = import_module_level(name, globals, locals, fromlist, level);
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002071 if (unlock_import() < 0) {
2072 Py_XDECREF(result);
2073 PyErr_SetString(PyExc_RuntimeError,
2074 "not holding the import lock");
2075 return NULL;
2076 }
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002077 return result;
2078}
2079
Fred Drake87590902004-05-28 20:21:36 +00002080/* Return the package that an import is being performed in. If globals comes
2081 from the module foo.bar.bat (not itself a package), this returns the
2082 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Georg Brandl5f6861d2006-05-28 21:57:35 +00002083 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002084
2085 The *name* of the returned package is returned in buf, with the length of
2086 the name in *p_buflen.
2087
2088 If globals doesn't come from a package or a module in a package, or a
2089 corresponding entry is not found in sys.modules, Py_None is returned.
2090*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002091static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002092get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002093{
2094 static PyObject *namestr = NULL;
2095 static PyObject *pathstr = NULL;
2096 PyObject *modname, *modpath, *modules, *parent;
2097
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002098 if (globals == NULL || !PyDict_Check(globals) || !level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002099 return Py_None;
2100
2101 if (namestr == NULL) {
2102 namestr = PyString_InternFromString("__name__");
2103 if (namestr == NULL)
2104 return NULL;
2105 }
2106 if (pathstr == NULL) {
2107 pathstr = PyString_InternFromString("__path__");
2108 if (pathstr == NULL)
2109 return NULL;
2110 }
2111
2112 *buf = '\0';
2113 *p_buflen = 0;
2114 modname = PyDict_GetItem(globals, namestr);
2115 if (modname == NULL || !PyString_Check(modname))
2116 return Py_None;
2117
2118 modpath = PyDict_GetItem(globals, pathstr);
2119 if (modpath != NULL) {
Martin v. Löwis725507b2006-03-07 12:08:51 +00002120 Py_ssize_t len = PyString_GET_SIZE(modname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002121 if (len > MAXPATHLEN) {
2122 PyErr_SetString(PyExc_ValueError,
2123 "Module name too long");
2124 return NULL;
2125 }
2126 strcpy(buf, PyString_AS_STRING(modname));
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002127 }
2128 else {
2129 char *start = PyString_AS_STRING(modname);
2130 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002131 size_t len;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002132 if (lastdot == NULL && level > 0) {
2133 PyErr_SetString(PyExc_ValueError,
Georg Brandl98775df2006-09-06 06:09:31 +00002134 "Attempted relative import in non-package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002135 return NULL;
2136 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002137 if (lastdot == NULL)
2138 return Py_None;
2139 len = lastdot - start;
2140 if (len >= MAXPATHLEN) {
2141 PyErr_SetString(PyExc_ValueError,
2142 "Module name too long");
2143 return NULL;
2144 }
2145 strncpy(buf, start, len);
2146 buf[len] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002147 }
2148
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002149 while (--level > 0) {
2150 char *dot = strrchr(buf, '.');
2151 if (dot == NULL) {
2152 PyErr_SetString(PyExc_ValueError,
Georg Brandl98775df2006-09-06 06:09:31 +00002153 "Attempted relative import beyond "
2154 "toplevel package");
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002155 return NULL;
2156 }
2157 *dot = '\0';
2158 }
2159 *p_buflen = strlen(buf);
2160
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002161 modules = PyImport_GetModuleDict();
2162 parent = PyDict_GetItemString(modules, buf);
2163 if (parent == NULL)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002164 PyErr_Format(PyExc_SystemError,
2165 "Parent module '%.200s' not loaded", buf);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002166 return parent;
2167 /* We expect, but can't guarantee, if parent != None, that:
2168 - parent.__name__ == buf
2169 - parent.__dict__ is globals
2170 If this is violated... Who cares? */
2171}
2172
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002173/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002174static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002175load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Martin v. Löwis18e16552006-02-15 17:27:45 +00002176 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002177{
2178 char *name = *p_name;
2179 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00002180 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002181 char *p;
2182 PyObject *result;
2183
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002184 if (strlen(name) == 0) {
Thomas Wouters8ddab272006-04-04 16:17:02 +00002185 /* completely empty module name should only happen in
2186 'from . import' (or '__import__("")')*/
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002187 Py_INCREF(mod);
2188 *p_name = NULL;
2189 return mod;
2190 }
2191
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002192 if (dot == NULL) {
2193 *p_name = NULL;
2194 len = strlen(name);
2195 }
2196 else {
2197 *p_name = dot+1;
2198 len = dot-name;
2199 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00002200 if (len == 0) {
2201 PyErr_SetString(PyExc_ValueError,
2202 "Empty module name");
2203 return NULL;
2204 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002205
2206 p = buf + *p_buflen;
2207 if (p != buf)
2208 *p++ = '.';
2209 if (p+len-buf >= MAXPATHLEN) {
2210 PyErr_SetString(PyExc_ValueError,
2211 "Module name too long");
2212 return NULL;
2213 }
2214 strncpy(p, name, len);
2215 p[len] = '\0';
2216 *p_buflen = p+len-buf;
2217
2218 result = import_submodule(mod, p, buf);
2219 if (result == Py_None && altmod != mod) {
2220 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002221 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00002222 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002223 if (result != NULL && result != Py_None) {
2224 if (mark_miss(buf) != 0) {
2225 Py_DECREF(result);
2226 return NULL;
2227 }
2228 strncpy(buf, name, len);
2229 buf[len] = '\0';
2230 *p_buflen = len;
2231 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002232 }
2233 if (result == NULL)
2234 return NULL;
2235
2236 if (result == Py_None) {
2237 Py_DECREF(result);
2238 PyErr_Format(PyExc_ImportError,
2239 "No module named %.200s", name);
2240 return NULL;
2241 }
2242
2243 return result;
2244}
2245
2246static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002247mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002248{
2249 PyObject *modules = PyImport_GetModuleDict();
2250 return PyDict_SetItemString(modules, name, Py_None);
2251}
2252
2253static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002254ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002255 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002256{
2257 int i;
2258
2259 if (!PyObject_HasAttrString(mod, "__path__"))
2260 return 1;
2261
2262 for (i = 0; ; i++) {
2263 PyObject *item = PySequence_GetItem(fromlist, i);
2264 int hasit;
2265 if (item == NULL) {
2266 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2267 PyErr_Clear();
2268 return 1;
2269 }
2270 return 0;
2271 }
2272 if (!PyString_Check(item)) {
2273 PyErr_SetString(PyExc_TypeError,
2274 "Item in ``from list'' not a string");
2275 Py_DECREF(item);
2276 return 0;
2277 }
2278 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00002279 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002280 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002281 /* See if the package defines __all__ */
2282 if (recursive)
2283 continue; /* Avoid endless recursion */
2284 all = PyObject_GetAttrString(mod, "__all__");
2285 if (all == NULL)
2286 PyErr_Clear();
2287 else {
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002288 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
Guido van Rossum9905ef91997-09-08 16:07:11 +00002289 Py_DECREF(all);
Martin v. Löwis83969ee2004-03-23 16:28:13 +00002290 if (!ret)
2291 return 0;
Guido van Rossum9905ef91997-09-08 16:07:11 +00002292 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002293 continue;
2294 }
2295 hasit = PyObject_HasAttr(mod, item);
2296 if (!hasit) {
2297 char *subname = PyString_AS_STRING(item);
2298 PyObject *submod;
2299 char *p;
2300 if (buflen + strlen(subname) >= MAXPATHLEN) {
2301 PyErr_SetString(PyExc_ValueError,
2302 "Module name too long");
2303 Py_DECREF(item);
2304 return 0;
2305 }
2306 p = buf + buflen;
2307 *p++ = '.';
2308 strcpy(p, subname);
2309 submod = import_submodule(mod, subname, buf);
2310 Py_XDECREF(submod);
2311 if (submod == NULL) {
2312 Py_DECREF(item);
2313 return 0;
2314 }
2315 }
2316 Py_DECREF(item);
2317 }
2318
Guido van Rossuma7f2e811997-10-03 15:33:32 +00002319 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002320}
2321
Neil Schemenauer00b09662003-06-16 21:03:07 +00002322static int
2323add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
2324 PyObject *modules)
2325{
2326 if (mod == Py_None)
2327 return 1;
2328 /* Irrespective of the success of this load, make a
2329 reference to it in the parent package module. A copy gets
2330 saved in the modules dictionary under the full name, so get a
2331 reference from there, if need be. (The exception is when the
2332 load failed with a SyntaxError -- then there's no trace in
2333 sys.modules. In that case, of course, do nothing extra.) */
2334 if (submod == NULL) {
2335 submod = PyDict_GetItemString(modules, fullname);
2336 if (submod == NULL)
2337 return 1;
2338 }
2339 if (PyModule_Check(mod)) {
2340 /* We can't use setattr here since it can give a
2341 * spurious warning if the submodule name shadows a
2342 * builtin name */
2343 PyObject *dict = PyModule_GetDict(mod);
2344 if (!dict)
2345 return 0;
2346 if (PyDict_SetItemString(dict, subname, submod) < 0)
2347 return 0;
2348 }
2349 else {
2350 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2351 return 0;
2352 }
2353 return 1;
2354}
2355
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002356static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002357import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002358{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002359 PyObject *modules = PyImport_GetModuleDict();
Neil Schemenauer00b09662003-06-16 21:03:07 +00002360 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002361
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002362 /* Require:
2363 if mod == None: subname == fullname
2364 else: mod.__name__ + "." + subname == fullname
2365 */
2366
Tim Peters50d8d372001-02-28 05:34:27 +00002367 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002368 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002369 }
2370 else {
Just van Rossum52e14d62002-12-30 22:08:05 +00002371 PyObject *path, *loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002372 char buf[MAXPATHLEN+1];
2373 struct filedescr *fdp;
2374 FILE *fp = NULL;
2375
Guido van Rossum9c0afe51998-05-19 15:09:05 +00002376 if (mod == Py_None)
2377 path = NULL;
2378 else {
2379 path = PyObject_GetAttrString(mod, "__path__");
2380 if (path == NULL) {
2381 PyErr_Clear();
2382 Py_INCREF(Py_None);
2383 return Py_None;
2384 }
2385 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002386
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002387 buf[0] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +00002388 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2389 &fp, &loader);
Guido van Rossumb68cd421998-07-01 17:36:26 +00002390 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002391 if (fdp == NULL) {
2392 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2393 return NULL;
2394 PyErr_Clear();
2395 Py_INCREF(Py_None);
2396 return Py_None;
2397 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002398 m = load_module(fullname, fp, buf, fdp->type, loader);
2399 Py_XDECREF(loader);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002400 if (fp)
2401 fclose(fp);
Neil Schemenauer00b09662003-06-16 21:03:07 +00002402 if (!add_submodule(mod, m, fullname, subname, modules)) {
2403 Py_XDECREF(m);
2404 m = NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002405 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00002406 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002407
2408 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002409}
2410
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002411
2412/* Re-import a module of any kind and return its module object, WITH
2413 INCREMENTED REFERENCE COUNT */
2414
Guido van Rossum79f25d91997-04-29 20:08:16 +00002415PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002416PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002417{
Collin Winter47c52a82007-03-13 23:02:15 +00002418 PyInterpreterState *interp = PyThreadState_Get()->interp;
2419 PyObject *modules_reloading = interp->modules_reloading;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002420 PyObject *modules = PyImport_GetModuleDict();
Collin Winter276887b2007-03-12 16:11:39 +00002421 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
Guido van Rossum222ef561997-09-06 19:41:09 +00002422 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002423 char buf[MAXPATHLEN+1];
2424 struct filedescr *fdp;
2425 FILE *fp = NULL;
Tim Peters1cd70172004-08-02 03:52:12 +00002426 PyObject *newm;
Collin Winter47c52a82007-03-13 23:02:15 +00002427
2428 if (modules_reloading == NULL) {
2429 Py_FatalError("PyImport_ReloadModule: "
Neal Norwitz2fca81c2007-05-30 04:53:41 +00002430 "no modules_reloading dictionary!");
Collin Winter47c52a82007-03-13 23:02:15 +00002431 return NULL;
2432 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002433
Guido van Rossum79f25d91997-04-29 20:08:16 +00002434 if (m == NULL || !PyModule_Check(m)) {
2435 PyErr_SetString(PyExc_TypeError,
2436 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002437 return NULL;
2438 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002439 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002440 if (name == NULL)
2441 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002442 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002443 PyErr_Format(PyExc_ImportError,
2444 "reload(): module %.200s not in sys.modules",
2445 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002446 return NULL;
2447 }
Neal Norwitz75c7c802007-03-13 05:31:38 +00002448 existing_m = PyDict_GetItemString(modules_reloading, name);
2449 if (existing_m != NULL) {
2450 /* Due to a recursive reload, this module is already
2451 being reloaded. */
2452 Py_INCREF(existing_m);
2453 return existing_m;
Collin Winter276887b2007-03-12 16:11:39 +00002454 }
Neal Norwitz75c7c802007-03-13 05:31:38 +00002455 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
2456 return NULL;
Collin Winter276887b2007-03-12 16:11:39 +00002457
Guido van Rossum222ef561997-09-06 19:41:09 +00002458 subname = strrchr(name, '.');
2459 if (subname == NULL)
2460 subname = name;
2461 else {
2462 PyObject *parentname, *parent;
2463 parentname = PyString_FromStringAndSize(name, (subname-name));
Collin Winter276887b2007-03-12 16:11:39 +00002464 if (parentname == NULL) {
2465 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002466 return NULL;
Neal Norwitz75c7c802007-03-13 05:31:38 +00002467 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002468 parent = PyDict_GetItem(modules, parentname);
2469 if (parent == NULL) {
2470 PyErr_Format(PyExc_ImportError,
2471 "reload(): parent %.200s not in sys.modules",
Georg Brandl0c55f292005-09-14 06:56:20 +00002472 PyString_AS_STRING(parentname));
2473 Py_DECREF(parentname);
Neal Norwitz2fca81c2007-05-30 04:53:41 +00002474 imp_modules_reloading_clear();
Guido van Rossum222ef561997-09-06 19:41:09 +00002475 return NULL;
2476 }
Georg Brandl0c55f292005-09-14 06:56:20 +00002477 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002478 subname++;
2479 path = PyObject_GetAttrString(parent, "__path__");
2480 if (path == NULL)
2481 PyErr_Clear();
2482 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002483 buf[0] = '\0';
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002484 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
Guido van Rossum222ef561997-09-06 19:41:09 +00002485 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002486
2487 if (fdp == NULL) {
2488 Py_XDECREF(loader);
Collin Winter276887b2007-03-12 16:11:39 +00002489 imp_modules_reloading_clear();
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002490 return NULL;
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002491 }
2492
2493 newm = load_module(name, fp, buf, fdp->type, loader);
2494 Py_XDECREF(loader);
2495
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002496 if (fp)
2497 fclose(fp);
Tim Peters1cd70172004-08-02 03:52:12 +00002498 if (newm == NULL) {
2499 /* load_module probably removed name from modules because of
2500 * the error. Put back the original module object. We're
2501 * going to return NULL in this case regardless of whether
2502 * replacing name succeeds, so the return value is ignored.
2503 */
2504 PyDict_SetItemString(modules, name, m);
2505 }
Neal Norwitz75c7c802007-03-13 05:31:38 +00002506 imp_modules_reloading_clear();
Tim Peters1cd70172004-08-02 03:52:12 +00002507 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002508}
2509
2510
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002511/* Higher-level import emulator which emulates the "import" statement
2512 more accurately -- it invokes the __import__() function from the
2513 builtins of the current globals. This means that the import is
2514 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002515 environment, e.g. by "rexec".
2516 A dummy list ["__doc__"] is passed as the 4th argument so that
2517 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2518 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002519
2520PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002521PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002522{
2523 static PyObject *silly_list = NULL;
2524 static PyObject *builtins_str = NULL;
2525 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002526 PyObject *globals = NULL;
2527 PyObject *import = NULL;
2528 PyObject *builtins = NULL;
2529 PyObject *r = NULL;
2530
2531 /* Initialize constant string objects */
2532 if (silly_list == NULL) {
2533 import_str = PyString_InternFromString("__import__");
2534 if (import_str == NULL)
2535 return NULL;
2536 builtins_str = PyString_InternFromString("__builtins__");
2537 if (builtins_str == NULL)
2538 return NULL;
2539 silly_list = Py_BuildValue("[s]", "__doc__");
2540 if (silly_list == NULL)
2541 return NULL;
2542 }
2543
2544 /* Get the builtins from current globals */
2545 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002546 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002547 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002548 builtins = PyObject_GetItem(globals, builtins_str);
2549 if (builtins == NULL)
2550 goto err;
2551 }
2552 else {
2553 /* No globals -- use standard builtins, and fake globals */
2554 PyErr_Clear();
2555
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002556 builtins = PyImport_ImportModuleLevel("__builtin__",
2557 NULL, NULL, NULL, 0);
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002558 if (builtins == NULL)
2559 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002560 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2561 if (globals == NULL)
2562 goto err;
2563 }
2564
2565 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002566 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002567 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002568 if (import == NULL)
2569 PyErr_SetObject(PyExc_KeyError, import_str);
2570 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002571 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002572 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002573 if (import == NULL)
2574 goto err;
2575
Georg Brandl74780962007-03-10 07:38:14 +00002576 /* Call the __import__ function with the proper argument list */
Georg Brandl684fd0c2006-05-25 19:15:31 +00002577 r = PyObject_CallFunctionObjArgs(import, module_name, globals,
2578 globals, silly_list, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002579
2580 err:
2581 Py_XDECREF(globals);
2582 Py_XDECREF(builtins);
2583 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002584
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002585 return r;
2586}
2587
2588
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002589/* Module 'imp' provides Python access to the primitives used for
2590 importing modules.
2591*/
2592
Guido van Rossum79f25d91997-04-29 20:08:16 +00002593static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002594imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002595{
2596 char buf[4];
2597
Guido van Rossum96774c12000-05-01 20:19:08 +00002598 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2599 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2600 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2601 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002602
Guido van Rossum79f25d91997-04-29 20:08:16 +00002603 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002604}
2605
Guido van Rossum79f25d91997-04-29 20:08:16 +00002606static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002607imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002608{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002609 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002610 struct filedescr *fdp;
2611
Guido van Rossum79f25d91997-04-29 20:08:16 +00002612 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002613 if (list == NULL)
2614 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002615 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2616 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002617 fdp->suffix, fdp->mode, fdp->type);
2618 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002619 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002620 return NULL;
2621 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002622 if (PyList_Append(list, item) < 0) {
2623 Py_DECREF(list);
2624 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002625 return NULL;
2626 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002627 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002628 }
2629 return list;
2630}
2631
Guido van Rossum79f25d91997-04-29 20:08:16 +00002632static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002633call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002634{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002635 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002636 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002637 struct filedescr *fdp;
2638 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002639 FILE *fp = NULL;
2640
2641 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002642 if (path == Py_None)
2643 path = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00002644 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002645 if (fdp == NULL)
2646 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002647 if (fp != NULL) {
2648 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2649 if (fob == NULL) {
2650 fclose(fp);
2651 return NULL;
2652 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002653 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002654 else {
2655 fob = Py_None;
2656 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002657 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002658 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002659 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002660 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002661 return ret;
2662}
2663
Guido van Rossum79f25d91997-04-29 20:08:16 +00002664static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002665imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002666{
2667 char *name;
2668 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002669 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002670 return NULL;
2671 return call_find_module(name, path);
2672}
2673
2674static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002675imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002676{
2677 char *name;
2678 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002679 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002680 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002681 return NULL;
2682 ret = init_builtin(name);
2683 if (ret < 0)
2684 return NULL;
2685 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002686 Py_INCREF(Py_None);
2687 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002688 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002689 m = PyImport_AddModule(name);
2690 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002691 return m;
2692}
2693
Guido van Rossum79f25d91997-04-29 20:08:16 +00002694static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002695imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002696{
2697 char *name;
2698 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002699 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002700 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002701 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002702 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002703 if (ret < 0)
2704 return NULL;
2705 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002706 Py_INCREF(Py_None);
2707 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002708 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002709 m = PyImport_AddModule(name);
2710 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002711 return m;
2712}
2713
Guido van Rossum79f25d91997-04-29 20:08:16 +00002714static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002715imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002716{
2717 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002718
Guido van Rossum43713e52000-02-29 13:59:29 +00002719 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002720 return NULL;
2721 return get_frozen_object(name);
2722}
2723
Guido van Rossum79f25d91997-04-29 20:08:16 +00002724static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002725imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002726{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002727 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002728 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002729 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002730 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002731}
2732
Guido van Rossum79f25d91997-04-29 20:08:16 +00002733static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002734imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002735{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002736 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002737 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002738 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002739 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002740 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002741 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002742}
2743
2744static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002745get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002746{
2747 FILE *fp;
2748 if (fob == NULL) {
Tim Peters86c7d2f2004-08-01 23:24:21 +00002749 if (mode[0] == 'U')
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002750 mode = "r" PY_STDIOTEXTMODE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002751 fp = fopen(pathname, mode);
2752 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002753 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002754 }
2755 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002756 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002757 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002758 PyErr_SetString(PyExc_ValueError,
2759 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002760 }
2761 return fp;
2762}
2763
Guido van Rossum79f25d91997-04-29 20:08:16 +00002764static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002765imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002766{
2767 char *name;
2768 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002769 PyObject *fob = NULL;
2770 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002771 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002772 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002773 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002774 return NULL;
2775 fp = get_file(pathname, fob, "rb");
2776 if (fp == NULL)
2777 return NULL;
2778 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002779 if (fob == NULL)
2780 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002781 return m;
2782}
2783
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002784#ifdef HAVE_DYNAMIC_LOADING
2785
Guido van Rossum79f25d91997-04-29 20:08:16 +00002786static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002787imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002788{
2789 char *name;
2790 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002791 PyObject *fob = NULL;
2792 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002793 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002794 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002795 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002796 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002797 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002798 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002799 if (fp == NULL)
2800 return NULL;
2801 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002802 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002803 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002804}
2805
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002806#endif /* HAVE_DYNAMIC_LOADING */
2807
Guido van Rossum79f25d91997-04-29 20:08:16 +00002808static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002809imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002810{
2811 char *name;
2812 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002813 PyObject *fob = NULL;
2814 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002815 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002816 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002817 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002818 return NULL;
2819 fp = get_file(pathname, fob, "r");
2820 if (fp == NULL)
2821 return NULL;
2822 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002823 if (fob == NULL)
2824 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002825 return m;
2826}
2827
Guido van Rossum79f25d91997-04-29 20:08:16 +00002828static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002829imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002830{
2831 char *name;
2832 PyObject *fob;
2833 char *pathname;
2834 char *suffix; /* Unused */
2835 char *mode;
2836 int type;
2837 FILE *fp;
2838
Guido van Rossum43713e52000-02-29 13:59:29 +00002839 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002840 &name, &fob, &pathname,
2841 &suffix, &mode, &type))
2842 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002843 if (*mode) {
2844 /* Mode must start with 'r' or 'U' and must not contain '+'.
2845 Implicit in this test is the assumption that the mode
2846 may contain other modifiers like 'b' or 't'. */
2847
2848 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
Jeremy Hylton4ae6fae2002-05-30 17:15:25 +00002849 PyErr_Format(PyExc_ValueError,
2850 "invalid file open mode %.200s", mode);
2851 return NULL;
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00002852 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002853 }
2854 if (fob == Py_None)
2855 fp = NULL;
2856 else {
2857 if (!PyFile_Check(fob)) {
2858 PyErr_SetString(PyExc_ValueError,
2859 "load_module arg#2 should be a file or None");
2860 return NULL;
2861 }
2862 fp = get_file(pathname, fob, mode);
2863 if (fp == NULL)
2864 return NULL;
2865 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002866 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002867}
2868
2869static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002870imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002871{
2872 char *name;
2873 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002874 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002875 return NULL;
2876 return load_package(name, pathname);
2877}
2878
2879static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002880imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002881{
2882 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002883 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002884 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002885 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002886}
2887
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002888/* Doc strings */
2889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002890PyDoc_STRVAR(doc_imp,
2891"This module provides the components needed to build your own\n\
2892__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002893
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002894PyDoc_STRVAR(doc_find_module,
2895"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002896Search for a module. If path is omitted or None, search for a\n\
2897built-in, frozen or special module and continue search in sys.path.\n\
2898The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002899package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002901PyDoc_STRVAR(doc_load_module,
2902"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002903Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002904The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002905
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002906PyDoc_STRVAR(doc_get_magic,
2907"get_magic() -> string\n\
2908Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002910PyDoc_STRVAR(doc_get_suffixes,
2911"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002912Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002913that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002914
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002915PyDoc_STRVAR(doc_new_module,
2916"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002917Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002918The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002919
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002920PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00002921"lock_held() -> boolean\n\
2922Return True if the import lock is currently held, else False.\n\
2923On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00002924
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002925PyDoc_STRVAR(doc_acquire_lock,
2926"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00002927Acquires the interpreter's import lock for the current thread.\n\
2928This lock should be used by import hooks to ensure thread-safety\n\
2929when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00002930On platforms without threads, this function does nothing.");
2931
2932PyDoc_STRVAR(doc_release_lock,
2933"release_lock() -> None\n\
2934Release the interpreter's import lock.\n\
2935On platforms without threads, this function does nothing.");
2936
Guido van Rossum79f25d91997-04-29 20:08:16 +00002937static PyMethodDef imp_methods[] = {
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002938 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2939 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
2940 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
2941 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2942 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2943 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
2944 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
2945 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002946 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002947 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2948 {"init_builtin", imp_init_builtin, METH_VARARGS},
2949 {"init_frozen", imp_init_frozen, METH_VARARGS},
2950 {"is_builtin", imp_is_builtin, METH_VARARGS},
2951 {"is_frozen", imp_is_frozen, METH_VARARGS},
2952 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002953#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002954 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002955#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002956 {"load_package", imp_load_package, METH_VARARGS},
Neal Norwitz031829d2002-03-31 14:37:44 +00002957 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002958 {NULL, NULL} /* sentinel */
2959};
2960
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002961static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002962setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002963{
2964 PyObject *v;
2965 int err;
2966
2967 v = PyInt_FromLong((long)value);
2968 err = PyDict_SetItemString(d, name, v);
2969 Py_XDECREF(v);
2970 return err;
2971}
2972
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00002973typedef struct {
2974 PyObject_HEAD
2975} NullImporter;
2976
2977static int
2978NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
2979{
2980 char *path;
Christian Heimescea681b2007-11-07 17:50:54 +00002981 Py_ssize_t pathlen;
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00002982
2983 if (!_PyArg_NoKeywords("NullImporter()", kwds))
2984 return -1;
2985
2986 if (!PyArg_ParseTuple(args, "s:NullImporter",
2987 &path))
2988 return -1;
2989
Christian Heimescea681b2007-11-07 17:50:54 +00002990 pathlen = strlen(path);
2991 if (pathlen == 0) {
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00002992 PyErr_SetString(PyExc_ImportError, "empty pathname");
2993 return -1;
2994 } else {
2995#ifndef RISCOS
2996 struct stat statbuf;
2997 int rv;
2998
2999 rv = stat(path, &statbuf);
Christian Heimes004c1c12007-11-07 18:30:22 +00003000#ifdef MS_WINDOWS
3001 /* MS Windows stat() chokes on paths like C:\path\. Try to
3002 * recover *one* time by stripping off a trailing slash or
3003 * backslash. http://bugs.python.org/issue1293
3004 */
Christian Heimescea681b2007-11-07 17:50:54 +00003005 if (rv != 0 && pathlen <= MAXPATHLEN &&
3006 (path[pathlen-1] == '/' || path[pathlen-1] == '\\')) {
3007 char mangled[MAXPATHLEN+1];
3008
3009 strcpy(mangled, path);
3010 mangled[pathlen-1] = '\0';
3011 rv = stat(mangled, &statbuf);
3012 }
Christian Heimescea681b2007-11-07 17:50:54 +00003013#endif
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003014 if (rv == 0) {
3015 /* it exists */
3016 if (S_ISDIR(statbuf.st_mode)) {
3017 /* it's a directory */
3018 PyErr_SetString(PyExc_ImportError,
3019 "existing directory");
3020 return -1;
3021 }
3022 }
3023#else
3024 if (object_exists(path)) {
3025 /* it exists */
3026 if (isdir(path)) {
3027 /* it's a directory */
3028 PyErr_SetString(PyExc_ImportError,
3029 "existing directory");
3030 return -1;
3031 }
3032 }
3033#endif
3034 }
3035 return 0;
3036}
3037
3038static PyObject *
3039NullImporter_find_module(NullImporter *self, PyObject *args)
3040{
3041 Py_RETURN_NONE;
3042}
3043
3044static PyMethodDef NullImporter_methods[] = {
3045 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3046 "Always return None"
3047 },
3048 {NULL} /* Sentinel */
3049};
3050
3051
3052static PyTypeObject NullImporterType = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003053 PyVarObject_HEAD_INIT(NULL, 0)
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003054 "imp.NullImporter", /*tp_name*/
3055 sizeof(NullImporter), /*tp_basicsize*/
3056 0, /*tp_itemsize*/
3057 0, /*tp_dealloc*/
3058 0, /*tp_print*/
3059 0, /*tp_getattr*/
3060 0, /*tp_setattr*/
3061 0, /*tp_compare*/
3062 0, /*tp_repr*/
3063 0, /*tp_as_number*/
3064 0, /*tp_as_sequence*/
3065 0, /*tp_as_mapping*/
3066 0, /*tp_hash */
3067 0, /*tp_call*/
3068 0, /*tp_str*/
3069 0, /*tp_getattro*/
3070 0, /*tp_setattro*/
3071 0, /*tp_as_buffer*/
3072 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3073 "Null importer object", /* tp_doc */
3074 0, /* tp_traverse */
3075 0, /* tp_clear */
3076 0, /* tp_richcompare */
3077 0, /* tp_weaklistoffset */
3078 0, /* tp_iter */
3079 0, /* tp_iternext */
3080 NullImporter_methods, /* tp_methods */
3081 0, /* tp_members */
3082 0, /* tp_getset */
3083 0, /* tp_base */
3084 0, /* tp_dict */
3085 0, /* tp_descr_get */
3086 0, /* tp_descr_set */
3087 0, /* tp_dictoffset */
3088 (initproc)NullImporter_init, /* tp_init */
3089 0, /* tp_alloc */
3090 PyType_GenericNew /* tp_new */
3091};
3092
3093
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003094PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003095initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003096{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003097 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003098
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003099 if (PyType_Ready(&NullImporterType) < 0)
3100 goto failure;
3101
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003102 m = Py_InitModule4("imp", imp_methods, doc_imp,
3103 NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003104 if (m == NULL)
3105 goto failure;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003106 d = PyModule_GetDict(m);
Neal Norwitz3cb31ac2006-08-13 18:10:47 +00003107 if (d == NULL)
3108 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003109
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003110 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3111 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3112 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3113 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3114 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3115 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3116 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3117 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00003118 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Just van Rossum52e14d62002-12-30 22:08:05 +00003119 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003120
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003121 Py_INCREF(&NullImporterType);
3122 PyModule_AddObject(m, "NullImporter", (PyObject *)&NullImporterType);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003123 failure:
3124 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003125}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003126
3127
Guido van Rossumb18618d2000-05-03 23:44:39 +00003128/* API for embedding applications that want to add their own entries
3129 to the table of built-in modules. This should normally be called
3130 *before* Py_Initialize(). When the table resize fails, -1 is
3131 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003132
3133 After a similar function by Just van Rossum. */
3134
3135int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003136PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003137{
3138 static struct _inittab *our_copy = NULL;
3139 struct _inittab *p;
3140 int i, n;
3141
3142 /* Count the number of entries in both tables */
3143 for (n = 0; newtab[n].name != NULL; n++)
3144 ;
3145 if (n == 0)
3146 return 0; /* Nothing to do */
3147 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3148 ;
3149
3150 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00003151 p = our_copy;
3152 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003153 if (p == NULL)
3154 return -1;
3155
3156 /* Copy the tables into the new memory */
3157 if (our_copy != PyImport_Inittab)
3158 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3159 PyImport_Inittab = our_copy = p;
3160 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
3161
3162 return 0;
3163}
3164
3165/* Shorthand to add a single entry given a name and a function */
3166
3167int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003168PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003169{
3170 struct _inittab newtab[2];
3171
3172 memset(newtab, '\0', sizeof newtab);
3173
3174 newtab[0].name = name;
3175 newtab[0].initfunc = initfunc;
3176
3177 return PyImport_ExtendInittab(newtab);
3178}
Anthony Baxterac6bd462006-04-13 02:06:09 +00003179
3180#ifdef __cplusplus
3181}
3182#endif