blob: d04fa50961e24cc86e64636bfa12271d2ac68894 [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
Brett Cannonb166afc2010-05-05 20:25:47 +000022extern "C" {
Anthony Baxterac6bd462006-04-13 02:06:09 +000023#endif
Guido van Rossum55a83382000-09-20 20:31:38 +000024
Christian Heimes5e8e6d22008-02-23 23:59:45 +000025#ifdef MS_WINDOWS
26/* for stat.st_mode */
27typedef unsigned short mode_t;
28#endif
29
Guido van Rossum21d335e1993-10-15 13:01:11 +000030
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000031/* Magic word to reject .pyc files generated by other Python versions.
32 It should change for each incompatible change to the bytecode.
33
34 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000035 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000036 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000037
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000038 The magic numbers must be spaced apart atleast 2 values, as the
39 -U interpeter flag will cause MAGIC+1 being used. They have been
40 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000041
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000042 There were a variety of old schemes for setting the magic number.
43 The current working scheme is to increment the previous value by
44 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000045
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000046 Known values:
47 Python 1.5: 20121
48 Python 1.5.1: 20121
49 Python 1.5.2: 20121
Skip Montanaro4ec3c262006-03-25 14:12:03 +000050 Python 1.6: 50428
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000051 Python 2.0: 50823
52 Python 2.0.1: 50823
53 Python 2.1: 60202
54 Python 2.1.1: 60202
55 Python 2.1.2: 60202
56 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000057 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000058 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000059 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000060 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000061 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000062 Python 2.4b1: 62061
Michael W. Hudsondf888462005-06-03 14:41:55 +000063 Python 2.5a0: 62071
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000064 Python 2.5a0: 62081 (ast-branch)
Guido van Rossumc2e20742006-02-27 22:32:47 +000065 Python 2.5a0: 62091 (with)
Guido van Rossumf6694362006-03-10 02:28:35 +000066 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
Neal Norwitz0d62a062006-07-30 06:53:31 +000067 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
68 Python 2.5b3: 62111 (fix wrong code: x += yield)
Neal Norwitz9a70f952006-08-04 05:12:19 +000069 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
Antoine Pitrouc83ea132010-05-09 14:46:46 +000070 storing constants that should have been removed)
Neal Norwitzdac090d2006-09-05 03:53:08 +000071 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
Raymond Hettingereffde122007-12-18 18:26:18 +000072 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
Nick Coghlan7af53be2008-03-07 14:13:28 +000073 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
Antoine Pitroud0c35152008-12-17 00:38:28 +000074 Python 2.7a0: 62171 (optimize list comprehensions/change LIST_APPEND)
Jeffrey Yasskin68d68522009-02-28 19:03:21 +000075 Python 2.7a0: 62181 (optimize conditional branches:
Antoine Pitrouc83ea132010-05-09 14:46:46 +000076 introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
Benjamin Peterson1880d8b2009-05-25 13:13:44 +000077 Python 2.7a0 62191 (introduce SETUP_WITH)
Alexandre Vassalottiee936a22010-01-09 23:35:54 +000078 Python 2.7a0 62201 (introduce BUILD_SET)
Alexandre Vassalottib6465472010-01-11 22:36:12 +000079 Python 2.7a0 62211 (introduce MAP_ADD and SET_ADD)
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000080.
Tim Peters36515e22001-11-18 04:06:29 +000081*/
Alexandre Vassalottib6465472010-01-11 22:36:12 +000082#define MAGIC (62211 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000083
Guido van Rossum96774c12000-05-01 20:19:08 +000084/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000085 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000086 compiler works which are enabled by command line switches. */
Christian Heimes61e45902008-03-27 10:35:52 +000087static long pyc_magic = MAGIC;
Guido van Rossum96774c12000-05-01 20:19:08 +000088
Guido van Rossum25ce5661997-08-02 03:10:38 +000089/* See _PyImport_FixupExtension() below */
90static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000091
Guido van Rossum771c6c81997-10-31 18:37:24 +000092/* This table is defined in config.c: */
93extern struct _inittab _PyImport_Inittab[];
94
95struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000096
Guido van Rossumed1170e1999-12-20 21:23:41 +000097/* these tables define the module suffixes that Python recognizes */
98struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000099
100#ifdef RISCOS
101static const struct filedescr _PyImport_StandardFiletab[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000102 {"/py", "U", PY_SOURCE},
103 {"/pyc", "rb", PY_COMPILED},
104 {0, 0}
Guido van Rossum48a680c2001-03-02 06:34:14 +0000105};
106#else
Guido van Rossumed1170e1999-12-20 21:23:41 +0000107static const struct filedescr _PyImport_StandardFiletab[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000108 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000109#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000110 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000111#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000112 {".pyc", "rb", PY_COMPILED},
113 {0, 0}
Guido van Rossumed1170e1999-12-20 21:23:41 +0000114};
Guido van Rossum48a680c2001-03-02 06:34:14 +0000115#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +0000116
Jason R. Coombs925ff742012-01-13 17:12:25 -0500117#ifdef HAVE_STAT
118int isdir(char *path) {
119 struct stat statbuf;
120 return stat(path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode);
121}
122#else
123/* with RISCOS, isdir is in unixstuff */
124#ifndef RISCOS
125int isdir(char *path) {
126 return 0;
127}
128#endif
129#endif
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000130
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000131/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132
133void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000134_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000136 const struct filedescr *scan;
137 struct filedescr *filetab;
138 int countD = 0;
139 int countS = 0;
Guido van Rossumed1170e1999-12-20 21:23:41 +0000140
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000141 /* prepare _PyImport_Filetab: copy entries from
142 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
143 */
Georg Brandladd36e52007-08-23 18:08:06 +0000144#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000145 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
146 ++countD;
Georg Brandladd36e52007-08-23 18:08:06 +0000147#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000148 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
149 ++countS;
150 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
151 if (filetab == NULL)
152 Py_FatalError("Can't initialize import file table.");
Georg Brandladd36e52007-08-23 18:08:06 +0000153#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000154 memcpy(filetab, _PyImport_DynLoadFiletab,
155 countD * sizeof(struct filedescr));
Georg Brandladd36e52007-08-23 18:08:06 +0000156#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000157 memcpy(filetab + countD, _PyImport_StandardFiletab,
158 countS * sizeof(struct filedescr));
159 filetab[countD + countS].suffix = NULL;
Guido van Rossumed1170e1999-12-20 21:23:41 +0000160
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000161 _PyImport_Filetab = filetab;
Guido van Rossumed1170e1999-12-20 21:23:41 +0000162
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000163 if (Py_OptimizeFlag) {
164 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
165 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000166#ifndef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167 if (strcmp(filetab->suffix, ".pyc") == 0)
168 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000169#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000170 if (strcmp(filetab->suffix, "/pyc") == 0)
171 filetab->suffix = "/pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000172#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000173 }
174 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000175
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000176 if (Py_UnicodeFlag) {
177 /* Fix the pyc_magic so that byte compiled code created
178 using the all-Unicode method doesn't interfere with
179 code created in normal operation mode. */
180 pyc_magic = MAGIC + 1;
181 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182}
183
Guido van Rossum25ce5661997-08-02 03:10:38 +0000184void
Just van Rossum52e14d62002-12-30 22:08:05 +0000185_PyImportHooks_Init(void)
186{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000187 PyObject *v, *path_hooks = NULL, *zimpimport;
188 int err = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +0000189
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000190 /* adding sys.path_hooks and sys.path_importer_cache, setting up
191 zipimport */
192 if (PyType_Ready(&PyNullImporter_Type) < 0)
193 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000194
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000195 if (Py_VerboseFlag)
196 PySys_WriteStderr("# installing zipimport hook\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000197
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000198 v = PyList_New(0);
199 if (v == NULL)
200 goto error;
201 err = PySys_SetObject("meta_path", v);
202 Py_DECREF(v);
203 if (err)
204 goto error;
205 v = PyDict_New();
206 if (v == NULL)
207 goto error;
208 err = PySys_SetObject("path_importer_cache", v);
209 Py_DECREF(v);
210 if (err)
211 goto error;
212 path_hooks = PyList_New(0);
213 if (path_hooks == NULL)
214 goto error;
215 err = PySys_SetObject("path_hooks", path_hooks);
216 if (err) {
Just van Rossum52e14d62002-12-30 22:08:05 +0000217 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000218 PyErr_Print();
219 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
220 "path_importer_cache, or NullImporter failed"
221 );
222 }
Phillip J. Ebyf7575d02006-07-28 21:12:07 +0000223
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000224 zimpimport = PyImport_ImportModule("zipimport");
225 if (zimpimport == NULL) {
226 PyErr_Clear(); /* No zip import module -- okay */
227 if (Py_VerboseFlag)
228 PySys_WriteStderr("# can't import zipimport\n");
229 }
230 else {
231 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
232 "zipimporter");
233 Py_DECREF(zimpimport);
234 if (zipimporter == NULL) {
235 PyErr_Clear(); /* No zipimporter object -- okay */
236 if (Py_VerboseFlag)
237 PySys_WriteStderr(
238 "# can't import zipimport.zipimporter\n");
239 }
240 else {
241 /* sys.path_hooks.append(zipimporter) */
242 err = PyList_Append(path_hooks, zipimporter);
243 Py_DECREF(zipimporter);
244 if (err)
245 goto error;
246 if (Py_VerboseFlag)
247 PySys_WriteStderr(
248 "# installed zipimport hook\n");
249 }
250 }
251 Py_DECREF(path_hooks);
Just van Rossum52e14d62002-12-30 22:08:05 +0000252}
253
254void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000255_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000256{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000257 Py_XDECREF(extensions);
258 extensions = NULL;
259 PyMem_DEL(_PyImport_Filetab);
260 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000261}
262
263
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000264/* Locking primitives to prevent parallel imports of the same module
265 in different threads to return with a partially loaded module.
266 These calls are serialized by the global interpreter lock. */
267
268#ifdef WITH_THREAD
269
Guido van Rossum49b56061998-10-01 20:42:43 +0000270#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000271
Guido van Rossum65d5b571998-12-21 19:32:43 +0000272static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000273static long import_lock_thread = -1;
274static int import_lock_level = 0;
275
Thomas Woutersc4dcb382009-09-16 19:55:54 +0000276void
277_PyImport_AcquireLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000278{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000279 long me = PyThread_get_thread_ident();
280 if (me == -1)
281 return; /* Too bad */
282 if (import_lock == NULL) {
283 import_lock = PyThread_allocate_lock();
284 if (import_lock == NULL)
285 return; /* Nothing much we can do. */
286 }
287 if (import_lock_thread == me) {
288 import_lock_level++;
289 return;
290 }
291 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
292 {
293 PyThreadState *tstate = PyEval_SaveThread();
294 PyThread_acquire_lock(import_lock, 1);
295 PyEval_RestoreThread(tstate);
296 }
297 import_lock_thread = me;
298 import_lock_level = 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000299}
300
Thomas Woutersc4dcb382009-09-16 19:55:54 +0000301int
302_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000303{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 long me = PyThread_get_thread_ident();
305 if (me == -1 || import_lock == NULL)
306 return 0; /* Too bad */
307 if (import_lock_thread != me)
308 return -1;
309 import_lock_level--;
310 if (import_lock_level == 0) {
311 import_lock_thread = -1;
312 PyThread_release_lock(import_lock);
313 }
314 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000315}
316
Gregory P. Smith9e5d1322010-03-01 01:22:39 +0000317/* This function is called from PyOS_AfterFork to ensure that newly
318 created child processes do not share locks with the parent.
319 We now acquire the import lock around fork() calls but on some platforms
320 (Solaris 9 and earlier? see isue7242) that still left us with problems. */
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000321
322void
323_PyImport_ReInitLock(void)
324{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000325 if (import_lock != NULL)
326 import_lock = PyThread_allocate_lock();
327 import_lock_thread = -1;
328 import_lock_level = 0;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000329}
330
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000331#endif
332
Tim Peters69232342001-08-30 05:16:13 +0000333static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000334imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000335{
Tim Peters69232342001-08-30 05:16:13 +0000336#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000337 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000338#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000339 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000340#endif
341}
342
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000343static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000344imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000345{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000346#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000347 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000348#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000349 Py_INCREF(Py_None);
350 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000351}
352
353static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000354imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000355{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000356#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000357 if (_PyImport_ReleaseLock() < 0) {
358 PyErr_SetString(PyExc_RuntimeError,
359 "not holding the import lock");
360 return NULL;
361 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000362#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000363 Py_INCREF(Py_None);
364 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000365}
366
Collin Winter276887b2007-03-12 16:11:39 +0000367static void
Neal Norwitz75c7c802007-03-13 05:31:38 +0000368imp_modules_reloading_clear(void)
Collin Winter276887b2007-03-12 16:11:39 +0000369{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000370 PyInterpreterState *interp = PyThreadState_Get()->interp;
371 if (interp->modules_reloading != NULL)
372 PyDict_Clear(interp->modules_reloading);
Collin Winter276887b2007-03-12 16:11:39 +0000373}
374
Guido van Rossum25ce5661997-08-02 03:10:38 +0000375/* Helper for sys */
376
377PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000378PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000379{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000380 PyInterpreterState *interp = PyThreadState_GET()->interp;
381 if (interp->modules == NULL)
382 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
383 return interp->modules;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000384}
385
Guido van Rossum3f5da241990-12-20 15:06:42 +0000386
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000387/* List of names to clear in sys */
388static char* sys_deletes[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000389 "path", "argv", "ps1", "ps2", "exitfunc",
390 "exc_type", "exc_value", "exc_traceback",
391 "last_type", "last_value", "last_traceback",
392 "path_hooks", "path_importer_cache", "meta_path",
393 /* misc stuff */
394 "flags", "float_info",
395 NULL
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000396};
397
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000398static char* sys_files[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000399 "stdin", "__stdin__",
400 "stdout", "__stdout__",
401 "stderr", "__stderr__",
402 NULL
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000403};
404
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000405
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000406/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000407
Guido van Rossum3f5da241990-12-20 15:06:42 +0000408void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000409PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000410{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000411 Py_ssize_t pos, ndone;
412 char *name;
413 PyObject *key, *value, *dict;
414 PyInterpreterState *interp = PyThreadState_GET()->interp;
415 PyObject *modules = interp->modules;
Guido van Rossum758eec01998-01-19 21:58:26 +0000416
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000417 if (modules == NULL)
418 return; /* Already done */
Guido van Rossum758eec01998-01-19 21:58:26 +0000419
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000420 /* Delete some special variables first. These are common
421 places where user values hide and people complain when their
422 destructors fail. Since the modules containing them are
423 deleted *last* of all, they would come too late in the normal
424 destruction order. Sigh. */
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000425
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000426 value = PyDict_GetItemString(modules, "__builtin__");
427 if (value != NULL && PyModule_Check(value)) {
428 dict = PyModule_GetDict(value);
429 if (Py_VerboseFlag)
430 PySys_WriteStderr("# clear __builtin__._\n");
431 PyDict_SetItemString(dict, "_", Py_None);
432 }
433 value = PyDict_GetItemString(modules, "sys");
434 if (value != NULL && PyModule_Check(value)) {
435 char **p;
436 PyObject *v;
437 dict = PyModule_GetDict(value);
438 for (p = sys_deletes; *p != NULL; p++) {
439 if (Py_VerboseFlag)
440 PySys_WriteStderr("# clear sys.%s\n", *p);
441 PyDict_SetItemString(dict, *p, Py_None);
442 }
443 for (p = sys_files; *p != NULL; p+=2) {
444 if (Py_VerboseFlag)
445 PySys_WriteStderr("# restore sys.%s\n", *p);
446 v = PyDict_GetItemString(dict, *(p+1));
447 if (v == NULL)
448 v = Py_None;
449 PyDict_SetItemString(dict, *p, v);
450 }
451 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000452
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000453 /* First, delete __main__ */
454 value = PyDict_GetItemString(modules, "__main__");
455 if (value != NULL && PyModule_Check(value)) {
456 if (Py_VerboseFlag)
457 PySys_WriteStderr("# cleanup __main__\n");
458 _PyModule_Clear(value);
459 PyDict_SetItemString(modules, "__main__", Py_None);
460 }
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000461
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000462 /* The special treatment of __builtin__ here is because even
463 when it's not referenced as a module, its dictionary is
464 referenced by almost every module's __builtins__. Since
465 deleting a module clears its dictionary (even if there are
466 references left to it), we need to delete the __builtin__
467 module last. Likewise, we don't delete sys until the very
468 end because it is implicitly referenced (e.g. by print).
Guido van Rossum758eec01998-01-19 21:58:26 +0000469
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000470 Also note that we 'delete' modules by replacing their entry
471 in the modules dict with None, rather than really deleting
472 them; this avoids a rehash of the modules dictionary and
473 also marks them as "non existent" so they won't be
474 re-imported. */
Guido van Rossum758eec01998-01-19 21:58:26 +0000475
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000476 /* Next, repeatedly delete modules with a reference count of
477 one (skipping __builtin__ and sys) and delete them */
478 do {
479 ndone = 0;
480 pos = 0;
481 while (PyDict_Next(modules, &pos, &key, &value)) {
482 if (value->ob_refcnt != 1)
483 continue;
484 if (PyString_Check(key) && PyModule_Check(value)) {
485 name = PyString_AS_STRING(key);
486 if (strcmp(name, "__builtin__") == 0)
487 continue;
488 if (strcmp(name, "sys") == 0)
489 continue;
490 if (Py_VerboseFlag)
491 PySys_WriteStderr(
492 "# cleanup[1] %s\n", name);
493 _PyModule_Clear(value);
494 PyDict_SetItem(modules, key, Py_None);
495 ndone++;
496 }
497 }
498 } while (ndone > 0);
Guido van Rossum758eec01998-01-19 21:58:26 +0000499
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000500 /* Next, delete all modules (still skipping __builtin__ and sys) */
501 pos = 0;
502 while (PyDict_Next(modules, &pos, &key, &value)) {
503 if (PyString_Check(key) && PyModule_Check(value)) {
504 name = PyString_AS_STRING(key);
505 if (strcmp(name, "__builtin__") == 0)
506 continue;
507 if (strcmp(name, "sys") == 0)
508 continue;
509 if (Py_VerboseFlag)
510 PySys_WriteStderr("# cleanup[2] %s\n", name);
511 _PyModule_Clear(value);
512 PyDict_SetItem(modules, key, Py_None);
513 }
514 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000515
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000516 /* Next, delete sys and __builtin__ (in that order) */
517 value = PyDict_GetItemString(modules, "sys");
518 if (value != NULL && PyModule_Check(value)) {
519 if (Py_VerboseFlag)
520 PySys_WriteStderr("# cleanup sys\n");
521 _PyModule_Clear(value);
522 PyDict_SetItemString(modules, "sys", Py_None);
523 }
524 value = PyDict_GetItemString(modules, "__builtin__");
525 if (value != NULL && PyModule_Check(value)) {
526 if (Py_VerboseFlag)
527 PySys_WriteStderr("# cleanup __builtin__\n");
528 _PyModule_Clear(value);
529 PyDict_SetItemString(modules, "__builtin__", Py_None);
530 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000531
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000532 /* Finally, clear and delete the modules directory */
533 PyDict_Clear(modules);
534 interp->modules = NULL;
535 Py_DECREF(modules);
536 Py_CLEAR(interp->modules_reloading);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000537}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000538
539
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000540/* Helper for pythonrun.c -- return magic number */
541
542long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000543PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000544{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000545 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000546}
547
548
Guido van Rossum25ce5661997-08-02 03:10:38 +0000549/* Magic for extension modules (built-in as well as dynamically
550 loaded). To prevent initializing an extension module more than
551 once, we keep a static dictionary 'extensions' keyed by module name
552 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000553 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000554 dictionary is stored by calling _PyImport_FixupExtension()
555 immediately after the module initialization function succeeds. A
556 copy can be retrieved from there by calling
557 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000558
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000560_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000561{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000562 PyObject *modules, *mod, *dict, *copy;
563 if (extensions == NULL) {
564 extensions = PyDict_New();
565 if (extensions == NULL)
566 return NULL;
567 }
568 modules = PyImport_GetModuleDict();
569 mod = PyDict_GetItemString(modules, name);
570 if (mod == NULL || !PyModule_Check(mod)) {
571 PyErr_Format(PyExc_SystemError,
572 "_PyImport_FixupExtension: module %.200s not loaded", name);
573 return NULL;
574 }
575 dict = PyModule_GetDict(mod);
576 if (dict == NULL)
577 return NULL;
578 copy = PyDict_Copy(dict);
579 if (copy == NULL)
580 return NULL;
581 PyDict_SetItemString(extensions, filename, copy);
582 Py_DECREF(copy);
583 return copy;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000584}
585
586PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000587_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000588{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000589 PyObject *dict, *mod, *mdict;
590 if (extensions == NULL)
591 return NULL;
592 dict = PyDict_GetItemString(extensions, filename);
593 if (dict == NULL)
594 return NULL;
595 mod = PyImport_AddModule(name);
596 if (mod == NULL)
597 return NULL;
598 mdict = PyModule_GetDict(mod);
599 if (mdict == NULL)
600 return NULL;
601 if (PyDict_Update(mdict, dict))
602 return NULL;
603 if (Py_VerboseFlag)
604 PySys_WriteStderr("import %s # previously loaded (%s)\n",
605 name, filename);
606 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000607}
608
609
610/* Get the module object corresponding to a module name.
611 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000612 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000613 Because the former action is most common, THIS DOES NOT RETURN A
614 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000615
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000617PyImport_AddModule(const char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000618{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000619 PyObject *modules = PyImport_GetModuleDict();
620 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000621
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000622 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
623 PyModule_Check(m))
624 return m;
625 m = PyModule_New(name);
626 if (m == NULL)
627 return NULL;
628 if (PyDict_SetItemString(modules, name, m) != 0) {
629 Py_DECREF(m);
630 return NULL;
631 }
632 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000633
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000634 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000635}
636
Tim Peters1cd70172004-08-02 03:52:12 +0000637/* Remove name from sys.modules, if it's there. */
638static void
Benjamin Peterson06638732010-03-25 23:27:16 +0000639remove_module(const char *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000640{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000641 PyObject *modules = PyImport_GetModuleDict();
642 if (PyDict_GetItemString(modules, name) == NULL)
643 return;
644 if (PyDict_DelItemString(modules, name) < 0)
645 Py_FatalError("import: deleting existing key in"
646 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000647}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000649/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000650 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
651 * removed from sys.modules, to avoid leaving damaged module objects
652 * in sys.modules. The caller may wish to restore the original
653 * module object (if any) in this case; PyImport_ReloadModule is an
654 * example.
655 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000656PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000657PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000658{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000659 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000660}
661
662PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000663PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000664{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000665 PyObject *modules = PyImport_GetModuleDict();
666 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000667
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000668 m = PyImport_AddModule(name);
669 if (m == NULL)
670 return NULL;
671 /* If the module is being reloaded, we get the old module back
672 and re-use its dict to exec the new code. */
673 d = PyModule_GetDict(m);
674 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
675 if (PyDict_SetItemString(d, "__builtins__",
676 PyEval_GetBuiltins()) != 0)
677 goto error;
678 }
679 /* Remember the filename as the __file__ attribute */
680 v = NULL;
681 if (pathname != NULL) {
682 v = PyString_FromString(pathname);
683 if (v == NULL)
684 PyErr_Clear();
685 }
686 if (v == NULL) {
687 v = ((PyCodeObject *)co)->co_filename;
688 Py_INCREF(v);
689 }
690 if (PyDict_SetItemString(d, "__file__", v) != 0)
691 PyErr_Clear(); /* Not important enough to report */
692 Py_DECREF(v);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000693
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000694 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
695 if (v == NULL)
696 goto error;
697 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000698
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000699 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
700 PyErr_Format(PyExc_ImportError,
701 "Loaded module %.200s not found in sys.modules",
702 name);
703 return NULL;
704 }
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000705
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000706 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000708 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000709
710 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000711 remove_module(name);
712 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000713}
714
715
716/* Given a pathname for a Python source file, fill a buffer with the
717 pathname for the corresponding compiled file. Return the pathname
718 for the compiled file, or NULL if there's no space in the buffer.
719 Doesn't set an exception. */
720
721static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000722make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000723{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000724 size_t len = strlen(pathname);
725 if (len+2 > buflen)
726 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000727
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000728#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000729 /* Treat .pyw as if it were .py. The case of ".pyw" must match
730 that used in _PyImport_StandardFiletab. */
731 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
732 --len; /* pretend 'w' isn't there */
Tim Petersc1731372001-08-04 08:12:36 +0000733#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000734 memcpy(buf, pathname, len);
735 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
736 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000737
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000738 return buf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000739}
740
741
742/* Given a pathname for a Python source file, its time of last
743 modification, and a pathname for a compiled file, check whether the
744 compiled file represents the same version of the source. If so,
745 return a FILE pointer for the compiled file, positioned just after
746 the header; if not, return NULL.
747 Doesn't set an exception. */
748
749static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000750check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000751{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000752 FILE *fp;
753 long magic;
754 long pyc_mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000755
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000756 fp = fopen(cpathname, "rb");
757 if (fp == NULL)
758 return NULL;
759 magic = PyMarshal_ReadLongFromFile(fp);
760 if (magic != pyc_magic) {
761 if (Py_VerboseFlag)
762 PySys_WriteStderr("# %s has bad magic\n", cpathname);
763 fclose(fp);
764 return NULL;
765 }
766 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
767 if (pyc_mtime != mtime) {
768 if (Py_VerboseFlag)
769 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
770 fclose(fp);
771 return NULL;
772 }
773 if (Py_VerboseFlag)
774 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
775 return fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000776}
777
778
779/* Read a code object from a file and check it for validity */
780
Guido van Rossum79f25d91997-04-29 20:08:16 +0000781static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000782read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000783{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000784 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000785
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000786 co = PyMarshal_ReadLastObjectFromFile(fp);
787 if (co == NULL)
788 return NULL;
789 if (!PyCode_Check(co)) {
790 PyErr_Format(PyExc_ImportError,
791 "Non-code object in %.200s", cpathname);
792 Py_DECREF(co);
793 return NULL;
794 }
795 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000796}
797
798
799/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000800 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000801
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000803load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000804{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000805 long magic;
806 PyCodeObject *co;
807 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000808
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000809 magic = PyMarshal_ReadLongFromFile(fp);
810 if (magic != pyc_magic) {
811 PyErr_Format(PyExc_ImportError,
812 "Bad magic number in %.200s", cpathname);
813 return NULL;
814 }
815 (void) PyMarshal_ReadLongFromFile(fp);
816 co = read_compiled_module(cpathname, fp);
817 if (co == NULL)
818 return NULL;
819 if (Py_VerboseFlag)
820 PySys_WriteStderr("import %s # precompiled from %s\n",
821 name, cpathname);
822 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
823 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000824
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000825 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000826}
827
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000828/* Parse a source file and return the corresponding code object */
829
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000832{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000833 PyCodeObject *co = NULL;
834 mod_ty mod;
835 PyCompilerFlags flags;
836 PyArena *arena = PyArena_New();
837 if (arena == NULL)
838 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000839
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000840 flags.cf_flags = 0;
Christian Heimes7f23d862008-03-26 22:51:58 +0000841
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000842 mod = PyParser_ASTFromFile(fp, pathname, Py_file_input, 0, 0, &flags,
843 NULL, arena);
844 if (mod) {
845 co = PyAST_Compile(mod, pathname, NULL, arena);
846 }
847 PyArena_Free(arena);
848 return co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000849}
850
851
Guido van Rossum55a83382000-09-20 20:31:38 +0000852/* Helper to open a bytecode file for writing in exclusive mode */
853
854static FILE *
Christian Heimes40346852008-02-23 17:52:07 +0000855open_exclusive(char *filename, mode_t mode)
Guido van Rossum55a83382000-09-20 20:31:38 +0000856{
857#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000858 /* Use O_EXCL to avoid a race condition when another process tries to
859 write the same file. When that happens, our open() call fails,
860 which is just fine (since it's only a cache).
861 XXX If the file exists and is writable but the directory is not
862 writable, the file will never be written. Oh well.
863 */
864 int fd;
865 (void) unlink(filename);
866 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
Tim Peters42c83af2000-09-29 04:03:10 +0000867#ifdef O_BINARY
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000868 |O_BINARY /* necessary for Windows */
Tim Peters42c83af2000-09-29 04:03:10 +0000869#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000870#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000871 , mode, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000872#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000873 , mode
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000874#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000875 );
876 if (fd < 0)
877 return NULL;
878 return fdopen(fd, "wb");
Guido van Rossum55a83382000-09-20 20:31:38 +0000879#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000880 /* Best we can do -- on Windows this can't happen anyway */
881 return fopen(filename, "wb");
Guido van Rossum55a83382000-09-20 20:31:38 +0000882#endif
883}
884
885
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000886/* Write a compiled module to a file, placing the time of last
887 modification of its source into the header.
888 Errors are ignored, if a write error occurs an attempt is made to
889 remove the file. */
890
891static void
Christian Heimes40346852008-02-23 17:52:07 +0000892write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000893{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000894 FILE *fp;
895 time_t mtime = srcstat->st_mtime;
R. David Murray3310a102009-07-07 09:54:16 +0000896#ifdef MS_WINDOWS /* since Windows uses different permissions */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000897 mode_t mode = srcstat->st_mode & ~S_IEXEC;
R. David Murray3310a102009-07-07 09:54:16 +0000898#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000899 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;
Brett Cannonb166afc2010-05-05 20:25:47 +0000900#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000901
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000902 fp = open_exclusive(cpathname, mode);
903 if (fp == NULL) {
904 if (Py_VerboseFlag)
905 PySys_WriteStderr(
906 "# can't create %s\n", cpathname);
907 return;
908 }
909 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
910 /* First write a 0 for mtime */
911 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
912 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
913 if (fflush(fp) != 0 || ferror(fp)) {
914 if (Py_VerboseFlag)
915 PySys_WriteStderr("# can't write %s\n", cpathname);
916 /* Don't keep partial file */
917 fclose(fp);
918 (void) unlink(cpathname);
919 return;
920 }
Antoine Pitrou6f25d752012-01-25 15:38:32 +0100921 /* Now write the true mtime (as a 32-bit field) */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000922 fseek(fp, 4L, 0);
Antoine Pitrou6f25d752012-01-25 15:38:32 +0100923 assert(mtime <= 0xFFFFFFFF);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000924 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
925 fflush(fp);
926 fclose(fp);
927 if (Py_VerboseFlag)
928 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000929}
930
Antoine Pitroue96d4ea2009-01-06 18:10:47 +0000931static void
932update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
933{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000934 PyObject *constants, *tmp;
935 Py_ssize_t i, n;
Antoine Pitroue96d4ea2009-01-06 18:10:47 +0000936
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000937 if (!_PyString_Eq(co->co_filename, oldname))
938 return;
Antoine Pitroue96d4ea2009-01-06 18:10:47 +0000939
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000940 tmp = co->co_filename;
941 co->co_filename = newname;
942 Py_INCREF(co->co_filename);
943 Py_DECREF(tmp);
Antoine Pitroue96d4ea2009-01-06 18:10:47 +0000944
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000945 constants = co->co_consts;
946 n = PyTuple_GET_SIZE(constants);
947 for (i = 0; i < n; i++) {
948 tmp = PyTuple_GET_ITEM(constants, i);
949 if (PyCode_Check(tmp))
950 update_code_filenames((PyCodeObject *)tmp,
951 oldname, newname);
952 }
Antoine Pitroue96d4ea2009-01-06 18:10:47 +0000953}
954
955static int
956update_compiled_module(PyCodeObject *co, char *pathname)
957{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000958 PyObject *oldname, *newname;
Antoine Pitroue96d4ea2009-01-06 18:10:47 +0000959
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000960 if (strcmp(PyString_AsString(co->co_filename), pathname) == 0)
961 return 0;
Antoine Pitroue96d4ea2009-01-06 18:10:47 +0000962
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000963 newname = PyString_FromString(pathname);
964 if (newname == NULL)
965 return -1;
Antoine Pitroue96d4ea2009-01-06 18:10:47 +0000966
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000967 oldname = co->co_filename;
968 Py_INCREF(oldname);
969 update_code_filenames(co, oldname, newname);
970 Py_DECREF(oldname);
971 Py_DECREF(newname);
972 return 1;
Antoine Pitroue96d4ea2009-01-06 18:10:47 +0000973}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000974
975/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000976 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
977 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000978
Guido van Rossum79f25d91997-04-29 20:08:16 +0000979static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000980load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000981{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000982 struct stat st;
983 FILE *fpc;
984 char buf[MAXPATHLEN+1];
985 char *cpathname;
986 PyCodeObject *co;
987 PyObject *m;
Brett Cannonb166afc2010-05-05 20:25:47 +0000988
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000989 if (fstat(fileno(fp), &st) != 0) {
990 PyErr_Format(PyExc_RuntimeError,
991 "unable to get file status from '%s'",
992 pathname);
993 return NULL;
994 }
Antoine Pitrou0e5fd592012-01-25 03:31:39 +0100995 if (sizeof st.st_mtime > 4) {
996 /* Python's .pyc timestamp handling presumes that the timestamp fits
997 in 4 bytes. Since the code only does an equality comparison,
998 ordering is not important and we can safely ignore the higher bits
999 (collisions are extremely unlikely).
1000 */
1001 st.st_mtime &= 0xFFFFFFFF;
1002 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001003 cpathname = make_compiled_pathname(pathname, buf,
1004 (size_t)MAXPATHLEN + 1);
1005 if (cpathname != NULL &&
1006 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
1007 co = read_compiled_module(cpathname, fpc);
1008 fclose(fpc);
1009 if (co == NULL)
1010 return NULL;
1011 if (update_compiled_module(co, pathname) < 0)
1012 return NULL;
1013 if (Py_VerboseFlag)
1014 PySys_WriteStderr("import %s # precompiled from %s\n",
1015 name, cpathname);
1016 pathname = cpathname;
1017 }
1018 else {
1019 co = parse_source_module(pathname, fp);
1020 if (co == NULL)
1021 return NULL;
1022 if (Py_VerboseFlag)
1023 PySys_WriteStderr("import %s # from %s\n",
1024 name, pathname);
1025 if (cpathname) {
1026 PyObject *ro = PySys_GetObject("dont_write_bytecode");
1027 if (ro == NULL || !PyObject_IsTrue(ro))
1028 write_compiled_module(co, cpathname, &st);
1029 }
1030 }
1031 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
1032 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001033
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001034 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001035}
1036
1037
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001038/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001039static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
1040static struct filedescr *find_module(char *, char *, PyObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001041 char *, size_t, FILE **, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001042static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001043
1044/* Load a package and return its module object WITH INCREMENTED
1045 REFERENCE COUNT */
1046
1047static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001048load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001049{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001050 PyObject *m, *d;
1051 PyObject *file = NULL;
1052 PyObject *path = NULL;
1053 int err;
1054 char buf[MAXPATHLEN+1];
1055 FILE *fp = NULL;
1056 struct filedescr *fdp;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001057
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001058 m = PyImport_AddModule(name);
1059 if (m == NULL)
1060 return NULL;
1061 if (Py_VerboseFlag)
1062 PySys_WriteStderr("import %s # directory %s\n",
1063 name, pathname);
1064 d = PyModule_GetDict(m);
1065 file = PyString_FromString(pathname);
1066 if (file == NULL)
1067 goto error;
1068 path = Py_BuildValue("[O]", file);
1069 if (path == NULL)
1070 goto error;
1071 err = PyDict_SetItemString(d, "__file__", file);
1072 if (err == 0)
1073 err = PyDict_SetItemString(d, "__path__", path);
1074 if (err != 0)
1075 goto error;
1076 buf[0] = '\0';
1077 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
1078 if (fdp == NULL) {
1079 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1080 PyErr_Clear();
1081 Py_INCREF(m);
1082 }
1083 else
1084 m = NULL;
1085 goto cleanup;
1086 }
1087 m = load_module(name, fp, buf, fdp->type, NULL);
1088 if (fp != NULL)
1089 fclose(fp);
1090 goto cleanup;
Tim Peters1cd70172004-08-02 03:52:12 +00001091
1092 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001093 m = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001094 cleanup:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001095 Py_XDECREF(path);
1096 Py_XDECREF(file);
1097 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001098}
1099
1100
1101/* Helper to test for built-in module */
1102
1103static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001104is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001105{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001106 int i;
1107 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1108 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
1109 if (PyImport_Inittab[i].initfunc == NULL)
1110 return -1;
1111 else
1112 return 1;
1113 }
1114 }
1115 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001116}
1117
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001118
Just van Rossum52e14d62002-12-30 22:08:05 +00001119/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1120 possibly by fetching it from the path_importer_cache dict. If it
Brett Cannon94b69f62006-09-28 22:10:14 +00001121 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001122 that can handle the path item. Return None if no hook could;
1123 this tells our caller it should fall back to the builtin
1124 import mechanism. Cache the result in path_importer_cache.
1125 Returns a borrowed reference. */
1126
1127static PyObject *
1128get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001129 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +00001130{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001131 PyObject *importer;
1132 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001133
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001134 /* These conditions are the caller's responsibility: */
1135 assert(PyList_Check(path_hooks));
1136 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +00001137
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001138 nhooks = PyList_Size(path_hooks);
1139 if (nhooks < 0)
1140 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +00001141
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001142 importer = PyDict_GetItem(path_importer_cache, p);
1143 if (importer != NULL)
1144 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001145
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001146 /* set path_importer_cache[p] to None to avoid recursion */
1147 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1148 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001149
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001150 for (j = 0; j < nhooks; j++) {
1151 PyObject *hook = PyList_GetItem(path_hooks, j);
1152 if (hook == NULL)
1153 return NULL;
1154 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
1155 if (importer != NULL)
1156 break;
Just van Rossum52e14d62002-12-30 22:08:05 +00001157
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001158 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1159 return NULL;
1160 }
1161 PyErr_Clear();
1162 }
1163 if (importer == NULL) {
1164 importer = PyObject_CallFunctionObjArgs(
1165 (PyObject *)&PyNullImporter_Type, p, NULL
1166 );
1167 if (importer == NULL) {
1168 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1169 PyErr_Clear();
1170 return Py_None;
1171 }
1172 }
1173 }
1174 if (importer != NULL) {
1175 int err = PyDict_SetItem(path_importer_cache, p, importer);
1176 Py_DECREF(importer);
1177 if (err != 0)
1178 return NULL;
1179 }
1180 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001181}
1182
Nick Coghlan327a39b2007-11-18 11:56:28 +00001183PyAPI_FUNC(PyObject *)
1184PyImport_GetImporter(PyObject *path) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001185 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Nick Coghlan327a39b2007-11-18 11:56:28 +00001186
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001187 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1188 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1189 importer = get_path_importer(path_importer_cache,
1190 path_hooks, path);
1191 }
1192 }
1193 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1194 return importer;
Nick Coghlan327a39b2007-11-18 11:56:28 +00001195}
1196
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001197/* Search the path (default sys.path) for a module. Return the
1198 corresponding filedescr struct, and (via return arguments) the
1199 pathname and an open file. Return NULL if the module is not found. */
1200
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001201#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00001202extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001203 char *, Py_ssize_t);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001204#endif
1205
Martin v. Löwis18e16552006-02-15 17:27:45 +00001206static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001207static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001208static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001209
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001210static struct filedescr *
Just van Rossum52e14d62002-12-30 22:08:05 +00001211find_module(char *fullname, char *subname, PyObject *path, char *buf,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001212 size_t buflen, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001213{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001214 Py_ssize_t i, npath;
1215 size_t len, namelen;
1216 struct filedescr *fdp = NULL;
1217 char *filemode;
1218 FILE *fp = NULL;
1219 PyObject *path_hooks, *path_importer_cache;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001220 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1221 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1222 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
1223 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +00001224#if defined(PYOS_OS2)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001225 size_t saved_len;
1226 size_t saved_namelen;
1227 char *saved_buf = NULL;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001228#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001229 if (p_loader != NULL)
1230 *p_loader = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001231
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001232 if (strlen(subname) > MAXPATHLEN) {
1233 PyErr_SetString(PyExc_OverflowError,
1234 "module name is too long");
1235 return NULL;
1236 }
1237 strcpy(name, subname);
Just van Rossum52e14d62002-12-30 22:08:05 +00001238
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001239 /* sys.meta_path import hook */
1240 if (p_loader != NULL) {
1241 PyObject *meta_path;
Just van Rossum52e14d62002-12-30 22:08:05 +00001242
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001243 meta_path = PySys_GetObject("meta_path");
1244 if (meta_path == NULL || !PyList_Check(meta_path)) {
Victor Stinnered36c062011-09-15 19:45:53 +02001245 PyErr_SetString(PyExc_RuntimeError,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001246 "sys.meta_path must be a list of "
1247 "import hooks");
1248 return NULL;
1249 }
1250 Py_INCREF(meta_path); /* zap guard */
1251 npath = PyList_Size(meta_path);
1252 for (i = 0; i < npath; i++) {
1253 PyObject *loader;
1254 PyObject *hook = PyList_GetItem(meta_path, i);
1255 loader = PyObject_CallMethod(hook, "find_module",
1256 "sO", fullname,
1257 path != NULL ?
1258 path : Py_None);
1259 if (loader == NULL) {
1260 Py_DECREF(meta_path);
1261 return NULL; /* true error */
1262 }
1263 if (loader != Py_None) {
1264 /* a loader was found */
1265 *p_loader = loader;
1266 Py_DECREF(meta_path);
1267 return &importhookdescr;
1268 }
1269 Py_DECREF(loader);
1270 }
1271 Py_DECREF(meta_path);
1272 }
Guido van Rossum0506a431998-08-11 15:07:39 +00001273
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001274 if (path != NULL && PyString_Check(path)) {
1275 /* The only type of submodule allowed inside a "frozen"
1276 package are other frozen modules or packages. */
1277 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
1278 PyErr_SetString(PyExc_ImportError,
1279 "full frozen module name too long");
1280 return NULL;
1281 }
1282 strcpy(buf, PyString_AsString(path));
1283 strcat(buf, ".");
1284 strcat(buf, name);
1285 strcpy(name, buf);
1286 if (find_frozen(name) != NULL) {
1287 strcpy(buf, name);
1288 return &fd_frozen;
1289 }
1290 PyErr_Format(PyExc_ImportError,
1291 "No frozen submodule named %.200s", name);
1292 return NULL;
1293 }
1294 if (path == NULL) {
1295 if (is_builtin(name)) {
1296 strcpy(buf, name);
1297 return &fd_builtin;
1298 }
1299 if ((find_frozen(name)) != NULL) {
1300 strcpy(buf, name);
1301 return &fd_frozen;
1302 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001303
Guido van Rossumac279101996-08-22 23:10:58 +00001304#ifdef MS_COREDLL
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001305 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
1306 if (fp != NULL) {
1307 *p_fp = fp;
1308 return fdp;
1309 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +00001310#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001311 path = PySys_GetObject("path");
1312 }
1313 if (path == NULL || !PyList_Check(path)) {
Victor Stinnered36c062011-09-15 19:45:53 +02001314 PyErr_SetString(PyExc_RuntimeError,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001315 "sys.path must be a list of directory names");
1316 return NULL;
1317 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001318
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001319 path_hooks = PySys_GetObject("path_hooks");
1320 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
Victor Stinnered36c062011-09-15 19:45:53 +02001321 PyErr_SetString(PyExc_RuntimeError,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001322 "sys.path_hooks must be a list of "
1323 "import hooks");
1324 return NULL;
1325 }
1326 path_importer_cache = PySys_GetObject("path_importer_cache");
1327 if (path_importer_cache == NULL ||
1328 !PyDict_Check(path_importer_cache)) {
Victor Stinnered36c062011-09-15 19:45:53 +02001329 PyErr_SetString(PyExc_RuntimeError,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001330 "sys.path_importer_cache must be a dict");
1331 return NULL;
1332 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001333
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001334 npath = PyList_Size(path);
1335 namelen = strlen(name);
1336 for (i = 0; i < npath; i++) {
1337 PyObject *copy = NULL;
1338 PyObject *v = PyList_GetItem(path, i);
1339 if (!v)
1340 return NULL;
Walter Dörwald3430d702002-06-17 10:43:59 +00001341#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001342 if (PyUnicode_Check(v)) {
1343 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
1344 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
1345 if (copy == NULL)
1346 return NULL;
1347 v = copy;
1348 }
1349 else
Walter Dörwald3430d702002-06-17 10:43:59 +00001350#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001351 if (!PyString_Check(v))
1352 continue;
1353 len = PyString_GET_SIZE(v);
1354 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1355 Py_XDECREF(copy);
1356 continue; /* Too long */
1357 }
1358 strcpy(buf, PyString_AS_STRING(v));
1359 if (strlen(buf) != len) {
1360 Py_XDECREF(copy);
1361 continue; /* v contains '\0' */
1362 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001363
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001364 /* sys.path_hooks import hook */
1365 if (p_loader != NULL) {
1366 PyObject *importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001367
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001368 importer = get_path_importer(path_importer_cache,
1369 path_hooks, v);
1370 if (importer == NULL) {
1371 Py_XDECREF(copy);
1372 return NULL;
1373 }
1374 /* Note: importer is a borrowed reference */
1375 if (importer != Py_None) {
1376 PyObject *loader;
1377 loader = PyObject_CallMethod(importer,
1378 "find_module",
1379 "s", fullname);
1380 Py_XDECREF(copy);
1381 if (loader == NULL)
1382 return NULL; /* error */
1383 if (loader != Py_None) {
1384 /* a loader was found */
1385 *p_loader = loader;
1386 return &importhookdescr;
1387 }
1388 Py_DECREF(loader);
1389 continue;
1390 }
1391 }
1392 /* no hook was found, use builtin import */
Just van Rossum52e14d62002-12-30 22:08:05 +00001393
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001394 if (len > 0 && buf[len-1] != SEP
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001395#ifdef ALTSEP
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001396 && buf[len-1] != ALTSEP
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001397#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001398 )
1399 buf[len++] = SEP;
1400 strcpy(buf+len, name);
1401 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001402
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001403 /* Check for package import (buf holds a directory name,
1404 and there's an __init__ module in that directory */
Jason R. Coombs925ff742012-01-13 17:12:25 -05001405 if (isdir(buf) && /* it's an existing directory */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001406 case_ok(buf, len, namelen, name)) { /* case matches */
1407 if (find_init_module(buf)) { /* and has __init__.py */
1408 Py_XDECREF(copy);
1409 return &fd_package;
1410 }
1411 else {
1412 char warnstr[MAXPATHLEN+80];
1413 sprintf(warnstr, "Not importing directory "
1414 "'%.*s': missing __init__.py",
1415 MAXPATHLEN, buf);
1416 if (PyErr_Warn(PyExc_ImportWarning,
1417 warnstr)) {
1418 Py_XDECREF(copy);
1419 return NULL;
1420 }
1421 }
1422 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001423#if defined(PYOS_OS2)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001424 /* take a snapshot of the module spec for restoration
1425 * after the 8 character DLL hackery
1426 */
1427 saved_buf = strdup(buf);
1428 saved_len = len;
1429 saved_namelen = namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001430#endif /* PYOS_OS2 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001431 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Georg Brandladd36e52007-08-23 18:08:06 +00001432#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001433 /* OS/2 limits DLLs to 8 character names (w/o
1434 extension)
1435 * so if the name is longer than that and its a
1436 * dynamically loaded module we're going to try,
1437 * truncate the name before trying
1438 */
1439 if (strlen(subname) > 8) {
1440 /* is this an attempt to load a C extension? */
1441 const struct filedescr *scan;
1442 scan = _PyImport_DynLoadFiletab;
1443 while (scan->suffix != NULL) {
1444 if (!strcmp(scan->suffix, fdp->suffix))
1445 break;
1446 else
1447 scan++;
1448 }
1449 if (scan->suffix != NULL) {
1450 /* yes, so truncate the name */
1451 namelen = 8;
1452 len -= strlen(subname) - namelen;
1453 buf[len] = '\0';
1454 }
1455 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001456#endif /* PYOS_OS2 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001457 strcpy(buf+len, fdp->suffix);
1458 if (Py_VerboseFlag > 1)
1459 PySys_WriteStderr("# trying %s\n", buf);
1460 filemode = fdp->mode;
1461 if (filemode[0] == 'U')
1462 filemode = "r" PY_STDIOTEXTMODE;
1463 fp = fopen(buf, filemode);
1464 if (fp != NULL) {
1465 if (case_ok(buf, len, namelen, name))
1466 break;
1467 else { /* continue search */
1468 fclose(fp);
1469 fp = NULL;
1470 }
1471 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001472#if defined(PYOS_OS2)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001473 /* restore the saved snapshot */
1474 strcpy(buf, saved_buf);
1475 len = saved_len;
1476 namelen = saved_namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001477#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001478 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001479#if defined(PYOS_OS2)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001480 /* don't need/want the module name snapshot anymore */
1481 if (saved_buf)
1482 {
1483 free(saved_buf);
1484 saved_buf = NULL;
1485 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001486#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001487 Py_XDECREF(copy);
1488 if (fp != NULL)
1489 break;
1490 }
1491 if (fp == NULL) {
1492 PyErr_Format(PyExc_ImportError,
1493 "No module named %.200s", name);
1494 return NULL;
1495 }
1496 *p_fp = fp;
1497 return fdp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001498}
1499
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001500/* Helpers for main.c
1501 * Find the source file corresponding to a named module
1502 */
1503struct filedescr *
1504_PyImport_FindModule(const char *name, PyObject *path, char *buf,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001505 size_t buflen, FILE **p_fp, PyObject **p_loader)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001506{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001507 return find_module((char *) name, (char *) name, path,
1508 buf, buflen, p_fp, p_loader);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001509}
1510
1511PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
1512{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001513 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00001514}
1515
Martin v. Löwis18e16552006-02-15 17:27:45 +00001516/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00001517 * The arguments here are tricky, best shown by example:
1518 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1519 * ^ ^ ^ ^
1520 * |--------------------- buf ---------------------|
1521 * |------------------- len ------------------|
1522 * |------ name -------|
1523 * |----- namelen -----|
1524 * buf is the full path, but len only counts up to (& exclusive of) the
1525 * extension. name is the module name, also exclusive of extension.
1526 *
1527 * We've already done a successful stat() or fopen() on buf, so know that
1528 * there's some match, possibly case-insensitive.
1529 *
Tim Peters50d8d372001-02-28 05:34:27 +00001530 * case_ok() is to return 1 if there's a case-sensitive match for
1531 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1532 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001533 *
Tim Peters50d8d372001-02-28 05:34:27 +00001534 * case_ok() is used to implement case-sensitive import semantics even
1535 * on platforms with case-insensitive filesystems. It's trivial to implement
1536 * for case-sensitive filesystems. It's pretty much a cross-platform
1537 * nightmare for systems with case-insensitive filesystems.
1538 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001539
Tim Peters50d8d372001-02-28 05:34:27 +00001540/* First we may need a pile of platform-specific header files; the sequence
1541 * of #if's here should match the sequence in the body of case_ok().
1542 */
Jason Tishler7961aa62005-05-20 00:56:54 +00001543#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001544#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001545
Tim Peters50d8d372001-02-28 05:34:27 +00001546#elif defined(DJGPP)
1547#include <dir.h>
1548
Jason Tishler7961aa62005-05-20 00:56:54 +00001549#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001550#include <sys/types.h>
1551#include <dirent.h>
1552
Andrew MacIntyred9400542002-02-26 11:41:34 +00001553#elif defined(PYOS_OS2)
1554#define INCL_DOS
1555#define INCL_DOSERRORS
1556#define INCL_NOPMAPI
1557#include <os2.h>
1558
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001559#elif defined(RISCOS)
1560#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001561#endif
1562
Guido van Rossum0980bd91998-02-13 17:18:36 +00001563static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001564case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001565{
Tim Peters50d8d372001-02-28 05:34:27 +00001566/* Pick a platform-specific implementation; the sequence of #if's here should
1567 * match the sequence just above.
1568 */
1569
Jason Tishler7961aa62005-05-20 00:56:54 +00001570/* MS_WINDOWS */
1571#if defined(MS_WINDOWS)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001572 WIN32_FIND_DATA data;
1573 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00001574
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001575 if (Py_GETENV("PYTHONCASEOK") != NULL)
1576 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001577
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001578 h = FindFirstFile(buf, &data);
1579 if (h == INVALID_HANDLE_VALUE) {
1580 PyErr_Format(PyExc_NameError,
1581 "Can't find file for module %.100s\n(filename %.300s)",
1582 name, buf);
1583 return 0;
1584 }
1585 FindClose(h);
1586 return strncmp(data.cFileName, name, namelen) == 0;
Tim Peters50d8d372001-02-28 05:34:27 +00001587
1588/* DJGPP */
1589#elif defined(DJGPP)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001590 struct ffblk ffblk;
1591 int done;
Tim Peters50d8d372001-02-28 05:34:27 +00001592
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001593 if (Py_GETENV("PYTHONCASEOK") != NULL)
1594 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001595
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001596 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1597 if (done) {
1598 PyErr_Format(PyExc_NameError,
1599 "Can't find file for module %.100s\n(filename %.300s)",
1600 name, buf);
1601 return 0;
1602 }
1603 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001604
Jason Tishler7961aa62005-05-20 00:56:54 +00001605/* new-fangled macintosh (macosx) or Cygwin */
1606#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001607 DIR *dirp;
1608 struct dirent *dp;
1609 char dirname[MAXPATHLEN + 1];
1610 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001611
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001612 if (Py_GETENV("PYTHONCASEOK") != NULL)
1613 return 1;
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001614
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001615 /* Copy the dir component into dirname; substitute "." if empty */
1616 if (dirlen <= 0) {
1617 dirname[0] = '.';
1618 dirname[1] = '\0';
1619 }
1620 else {
1621 assert(dirlen <= MAXPATHLEN);
1622 memcpy(dirname, buf, dirlen);
1623 dirname[dirlen] = '\0';
1624 }
1625 /* Open the directory and search the entries for an exact match. */
1626 dirp = opendir(dirname);
1627 if (dirp) {
1628 char *nameWithExt = buf + len - namelen;
1629 while ((dp = readdir(dirp)) != NULL) {
1630 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001631#ifdef _DIRENT_HAVE_D_NAMELEN
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001632 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001633#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001634 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001635#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001636 if (thislen >= namelen &&
1637 strcmp(dp->d_name, nameWithExt) == 0) {
1638 (void)closedir(dirp);
1639 return 1; /* Found */
1640 }
1641 }
1642 (void)closedir(dirp);
1643 }
1644 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001645
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001646/* RISC OS */
1647#elif defined(RISCOS)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001648 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1649 char buf2[MAXPATHLEN+2];
1650 char *nameWithExt = buf+len-namelen;
1651 int canonlen;
1652 os_error *e;
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001653
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001654 if (Py_GETENV("PYTHONCASEOK") != NULL)
1655 return 1;
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001656
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001657 /* workaround:
1658 append wildcard, otherwise case of filename wouldn't be touched */
1659 strcpy(buf2, buf);
1660 strcat(buf2, "*");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001661
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001662 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1663 canonlen = MAXPATHLEN+1-canonlen;
1664 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1665 return 0;
1666 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1667 return 1; /* match */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001668
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001669 return 0;
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001670
Andrew MacIntyred9400542002-02-26 11:41:34 +00001671/* OS/2 */
1672#elif defined(PYOS_OS2)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001673 HDIR hdir = 1;
1674 ULONG srchcnt = 1;
1675 FILEFINDBUF3 ffbuf;
1676 APIRET rc;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001677
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001678 if (Py_GETENV("PYTHONCASEOK") != NULL)
1679 return 1;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001680
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001681 rc = DosFindFirst(buf,
1682 &hdir,
1683 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1684 &ffbuf, sizeof(ffbuf),
1685 &srchcnt,
1686 FIL_STANDARD);
1687 if (rc != NO_ERROR)
1688 return 0;
1689 return strncmp(ffbuf.achName, name, namelen) == 0;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001690
Tim Peters50d8d372001-02-28 05:34:27 +00001691/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1692#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001693 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001694
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001695#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001696}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001697
Guido van Rossum0980bd91998-02-13 17:18:36 +00001698
Guido van Rossum197346f1997-10-31 18:38:52 +00001699#ifdef HAVE_STAT
1700/* Helper to look for __init__.py or __init__.py[co] in potential package */
1701static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001702find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001703{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001704 const size_t save_len = strlen(buf);
1705 size_t i = save_len;
1706 char *pname; /* pointer to start of __init__ */
1707 struct stat statbuf;
Guido van Rossum197346f1997-10-31 18:38:52 +00001708
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001709/* For calling case_ok(buf, len, namelen, name):
1710 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1711 * ^ ^ ^ ^
1712 * |--------------------- buf ---------------------|
1713 * |------------------- len ------------------|
1714 * |------ name -------|
1715 * |----- namelen -----|
Tim Peters0f9431f2001-07-05 03:47:53 +00001716 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001717 if (save_len + 13 >= MAXPATHLEN)
1718 return 0;
1719 buf[i++] = SEP;
1720 pname = buf + i;
1721 strcpy(pname, "__init__.py");
1722 if (stat(buf, &statbuf) == 0) {
1723 if (case_ok(buf,
1724 save_len + 9, /* len("/__init__") */
1725 8, /* len("__init__") */
1726 pname)) {
1727 buf[save_len] = '\0';
1728 return 1;
1729 }
1730 }
1731 i += strlen(pname);
1732 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
1733 if (stat(buf, &statbuf) == 0) {
1734 if (case_ok(buf,
1735 save_len + 9, /* len("/__init__") */
1736 8, /* len("__init__") */
1737 pname)) {
1738 buf[save_len] = '\0';
1739 return 1;
1740 }
1741 }
1742 buf[save_len] = '\0';
1743 return 0;
Guido van Rossum197346f1997-10-31 18:38:52 +00001744}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001745
1746#else
1747
1748#ifdef RISCOS
1749static int
1750find_init_module(buf)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001751 char *buf;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001752{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001753 int save_len = strlen(buf);
1754 int i = save_len;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001755
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001756 if (save_len + 13 >= MAXPATHLEN)
1757 return 0;
1758 buf[i++] = SEP;
1759 strcpy(buf+i, "__init__/py");
1760 if (isfile(buf)) {
1761 buf[save_len] = '\0';
1762 return 1;
1763 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001764
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001765 if (Py_OptimizeFlag)
1766 strcpy(buf+i, "o");
1767 else
1768 strcpy(buf+i, "c");
1769 if (isfile(buf)) {
1770 buf[save_len] = '\0';
1771 return 1;
1772 }
1773 buf[save_len] = '\0';
1774 return 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001775}
1776#endif /*RISCOS*/
1777
Guido van Rossum197346f1997-10-31 18:38:52 +00001778#endif /* HAVE_STAT */
1779
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001780
Tim Petersdbd9ba62000-07-09 03:09:57 +00001781static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001782
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001783/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001784 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001785
Guido van Rossum79f25d91997-04-29 20:08:16 +00001786static PyObject *
Amaury Forgeot d'Arc982b2fa2009-07-25 16:22:06 +00001787load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001788{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001789 PyObject *modules;
1790 PyObject *m;
1791 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001792
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001793 /* First check that there's an open file (if we need one) */
1794 switch (type) {
1795 case PY_SOURCE:
1796 case PY_COMPILED:
1797 if (fp == NULL) {
1798 PyErr_Format(PyExc_ValueError,
1799 "file object required for import (type code %d)",
1800 type);
1801 return NULL;
1802 }
1803 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001804
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001805 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001806
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001807 case PY_SOURCE:
1808 m = load_source_module(name, pathname, fp);
1809 break;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001810
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001811 case PY_COMPILED:
1812 m = load_compiled_module(name, pathname, fp);
1813 break;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001814
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001815#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001816 case C_EXTENSION:
1817 m = _PyImport_LoadDynamicModule(name, pathname, fp);
1818 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001819#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001820
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001821 case PKG_DIRECTORY:
1822 m = load_package(name, pathname);
1823 break;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001824
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001825 case C_BUILTIN:
1826 case PY_FROZEN:
1827 if (pathname != NULL && pathname[0] != '\0')
1828 name = pathname;
1829 if (type == C_BUILTIN)
1830 err = init_builtin(name);
1831 else
1832 err = PyImport_ImportFrozenModule(name);
1833 if (err < 0)
1834 return NULL;
1835 if (err == 0) {
1836 PyErr_Format(PyExc_ImportError,
1837 "Purported %s module %.200s not found",
1838 type == C_BUILTIN ?
1839 "builtin" : "frozen",
1840 name);
1841 return NULL;
1842 }
1843 modules = PyImport_GetModuleDict();
1844 m = PyDict_GetItemString(modules, name);
1845 if (m == NULL) {
1846 PyErr_Format(
1847 PyExc_ImportError,
1848 "%s module %.200s not properly initialized",
1849 type == C_BUILTIN ?
1850 "builtin" : "frozen",
1851 name);
1852 return NULL;
1853 }
1854 Py_INCREF(m);
1855 break;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001856
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001857 case IMP_HOOK: {
1858 if (loader == NULL) {
1859 PyErr_SetString(PyExc_ImportError,
1860 "import hook without loader");
1861 return NULL;
1862 }
1863 m = PyObject_CallMethod(loader, "load_module", "s", name);
1864 break;
1865 }
Just van Rossum52e14d62002-12-30 22:08:05 +00001866
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001867 default:
1868 PyErr_Format(PyExc_ImportError,
1869 "Don't know how to import %.200s (type code %d)",
1870 name, type);
1871 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001872
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001873 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001874
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001875 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001876}
1877
1878
1879/* Initialize a built-in module.
Brett Cannon5a9aa4f2006-10-03 21:58:55 +00001880 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001881 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001882
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001883static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001884init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001885{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001886 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001887
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001888 if (_PyImport_FindExtension(name, name) != NULL)
1889 return 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001890
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001891 for (p = PyImport_Inittab; p->name != NULL; p++) {
1892 if (strcmp(name, p->name) == 0) {
1893 if (p->initfunc == NULL) {
1894 PyErr_Format(PyExc_ImportError,
1895 "Cannot re-init internal module %.200s",
1896 name);
1897 return -1;
1898 }
1899 if (Py_VerboseFlag)
1900 PySys_WriteStderr("import %s # builtin\n", name);
1901 (*p->initfunc)();
1902 if (PyErr_Occurred())
1903 return -1;
1904 if (_PyImport_FixupExtension(name, name) == NULL)
1905 return -1;
1906 return 1;
1907 }
1908 }
1909 return 0;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001910}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001911
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001912
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001913/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001914
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001915static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001916find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001917{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001918 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001919
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001920 for (p = PyImport_FrozenModules; ; p++) {
1921 if (p->name == NULL)
1922 return NULL;
1923 if (strcmp(p->name, name) == 0)
1924 break;
1925 }
1926 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001927}
1928
Guido van Rossum79f25d91997-04-29 20:08:16 +00001929static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001930get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001931{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001932 struct _frozen *p = find_frozen(name);
1933 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001934
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001935 if (p == NULL) {
1936 PyErr_Format(PyExc_ImportError,
1937 "No such frozen object named %.200s",
1938 name);
1939 return NULL;
1940 }
1941 if (p->code == NULL) {
1942 PyErr_Format(PyExc_ImportError,
1943 "Excluded frozen object named %.200s",
1944 name);
1945 return NULL;
1946 }
1947 size = p->size;
1948 if (size < 0)
1949 size = -size;
1950 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001951}
1952
1953/* Initialize a frozen module.
1954 Return 1 for succes, 0 if the module is not found, and -1 with
1955 an exception set if the initialization failed.
1956 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001957
1958int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001959PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001960{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001961 struct _frozen *p = find_frozen(name);
1962 PyObject *co;
1963 PyObject *m;
1964 int ispackage;
1965 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001966
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001967 if (p == NULL)
1968 return 0;
1969 if (p->code == NULL) {
1970 PyErr_Format(PyExc_ImportError,
1971 "Excluded frozen object named %.200s",
1972 name);
1973 return -1;
1974 }
1975 size = p->size;
1976 ispackage = (size < 0);
1977 if (ispackage)
1978 size = -size;
1979 if (Py_VerboseFlag)
1980 PySys_WriteStderr("import %s # frozen%s\n",
1981 name, ispackage ? " package" : "");
1982 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1983 if (co == NULL)
1984 return -1;
1985 if (!PyCode_Check(co)) {
1986 PyErr_Format(PyExc_TypeError,
1987 "frozen object %.200s is not a code object",
1988 name);
1989 goto err_return;
1990 }
1991 if (ispackage) {
1992 /* Set __path__ to the package name */
1993 PyObject *d, *s;
1994 int err;
1995 m = PyImport_AddModule(name);
1996 if (m == NULL)
1997 goto err_return;
1998 d = PyModule_GetDict(m);
1999 s = PyString_InternFromString(name);
2000 if (s == NULL)
2001 goto err_return;
2002 err = PyDict_SetItemString(d, "__path__", s);
2003 Py_DECREF(s);
2004 if (err != 0)
2005 goto err_return;
2006 }
2007 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
2008 if (m == NULL)
2009 goto err_return;
2010 Py_DECREF(co);
2011 Py_DECREF(m);
2012 return 1;
Neal Norwitzc0cde4d2006-07-16 02:17:36 +00002013err_return:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002014 Py_DECREF(co);
2015 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002016}
Guido van Rossum74e6a111994-08-29 12:54:38 +00002017
2018
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002019/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002020 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00002021
Guido van Rossum79f25d91997-04-29 20:08:16 +00002022PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002023PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00002024{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002025 PyObject *pname;
2026 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002027
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002028 pname = PyString_FromString(name);
2029 if (pname == NULL)
2030 return NULL;
2031 result = PyImport_Import(pname);
2032 Py_DECREF(pname);
2033 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002034}
2035
Christian Heimes000a0742008-01-03 22:16:32 +00002036/* Import a module without blocking
2037 *
2038 * At first it tries to fetch the module from sys.modules. If the module was
2039 * never loaded before it loads it with PyImport_ImportModule() unless another
2040 * thread holds the import lock. In the latter case the function raises an
2041 * ImportError instead of blocking.
2042 *
2043 * Returns the module object with incremented ref count.
2044 */
2045PyObject *
2046PyImport_ImportModuleNoBlock(const char *name)
2047{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002048 PyObject *result;
2049 PyObject *modules;
Victor Stinner871a0fb2011-09-02 00:21:36 +02002050#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002051 long me;
Victor Stinner871a0fb2011-09-02 00:21:36 +02002052#endif
Christian Heimes000a0742008-01-03 22:16:32 +00002053
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002054 /* Try to get the module from sys.modules[name] */
2055 modules = PyImport_GetModuleDict();
2056 if (modules == NULL)
2057 return NULL;
Christian Heimes000a0742008-01-03 22:16:32 +00002058
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002059 result = PyDict_GetItemString(modules, name);
2060 if (result != NULL) {
2061 Py_INCREF(result);
2062 return result;
2063 }
2064 else {
2065 PyErr_Clear();
2066 }
Benjamin Peterson17f03ca2008-09-01 14:18:30 +00002067#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002068 /* check the import lock
2069 * me might be -1 but I ignore the error here, the lock function
2070 * takes care of the problem */
2071 me = PyThread_get_thread_ident();
2072 if (import_lock_thread == -1 || import_lock_thread == me) {
2073 /* no thread or me is holding the lock */
2074 return PyImport_ImportModule(name);
2075 }
2076 else {
2077 PyErr_Format(PyExc_ImportError,
2078 "Failed to import %.200s because the import lock"
2079 "is held by another thread.",
2080 name);
2081 return NULL;
2082 }
Benjamin Peterson17f03ca2008-09-01 14:18:30 +00002083#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002084 return PyImport_ImportModule(name);
Benjamin Peterson17f03ca2008-09-01 14:18:30 +00002085#endif
Christian Heimes000a0742008-01-03 22:16:32 +00002086}
2087
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002088/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002089static PyObject *get_parent(PyObject *globals, char *buf,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002090 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002091static PyObject *load_next(PyObject *mod, PyObject *altmod,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002092 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002093static int mark_miss(char *name);
2094static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002095 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002096static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002097
2098/* The Magnum Opus of dotted-name import :-) */
2099
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002100static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002101import_module_level(char *name, PyObject *globals, PyObject *locals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002102 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002103{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002104 char buf[MAXPATHLEN+1];
2105 Py_ssize_t buflen = 0;
2106 PyObject *parent, *head, *next, *tail;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002107
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002108 if (strchr(name, '/') != NULL
Christian Heimes3403f152008-01-09 19:56:33 +00002109#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002110 || strchr(name, '\\') != NULL
Christian Heimes3403f152008-01-09 19:56:33 +00002111#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002112 ) {
2113 PyErr_SetString(PyExc_ImportError,
2114 "Import by filename is not supported.");
2115 return NULL;
2116 }
Christian Heimes3403f152008-01-09 19:56:33 +00002117
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002118 parent = get_parent(globals, buf, &buflen, level);
2119 if (parent == NULL)
2120 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002121
Brett Cannoneb3cd302010-05-20 18:37:55 +00002122 head = load_next(parent, level < 0 ? Py_None : parent, &name, buf,
2123 &buflen);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002124 if (head == NULL)
2125 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002126
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002127 tail = head;
2128 Py_INCREF(tail);
2129 while (name) {
2130 next = load_next(tail, tail, &name, buf, &buflen);
2131 Py_DECREF(tail);
2132 if (next == NULL) {
2133 Py_DECREF(head);
2134 return NULL;
2135 }
2136 tail = next;
2137 }
2138 if (tail == Py_None) {
2139 /* If tail is Py_None, both get_parent and load_next found
2140 an empty module name: someone called __import__("") or
2141 doctored faulty bytecode */
2142 Py_DECREF(tail);
2143 Py_DECREF(head);
2144 PyErr_SetString(PyExc_ValueError,
2145 "Empty module name");
2146 return NULL;
2147 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002148
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002149 if (fromlist != NULL) {
2150 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2151 fromlist = NULL;
2152 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002153
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002154 if (fromlist == NULL) {
2155 Py_DECREF(tail);
2156 return head;
2157 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002158
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002159 Py_DECREF(head);
2160 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
2161 Py_DECREF(tail);
2162 return NULL;
2163 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002164
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002165 return tail;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002166}
2167
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002168PyObject *
2169PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002170 PyObject *fromlist, int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002171{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002172 PyObject *result;
2173 _PyImport_AcquireLock();
2174 result = import_module_level(name, globals, locals, fromlist, level);
2175 if (_PyImport_ReleaseLock() < 0) {
2176 Py_XDECREF(result);
2177 PyErr_SetString(PyExc_RuntimeError,
2178 "not holding the import lock");
2179 return NULL;
2180 }
2181 return result;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002182}
2183
Fred Drake87590902004-05-28 20:21:36 +00002184/* Return the package that an import is being performed in. If globals comes
2185 from the module foo.bar.bat (not itself a package), this returns the
2186 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Georg Brandl5f6861d2006-05-28 21:57:35 +00002187 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002188
2189 The *name* of the returned package is returned in buf, with the length of
2190 the name in *p_buflen.
2191
2192 If globals doesn't come from a package or a module in a package, or a
2193 corresponding entry is not found in sys.modules, Py_None is returned.
2194*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002195static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002196get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002197{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002198 static PyObject *namestr = NULL;
2199 static PyObject *pathstr = NULL;
2200 static PyObject *pkgstr = NULL;
2201 PyObject *pkgname, *modname, *modpath, *modules, *parent;
2202 int orig_level = level;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002203
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002204 if (globals == NULL || !PyDict_Check(globals) || !level)
2205 return Py_None;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002206
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002207 if (namestr == NULL) {
2208 namestr = PyString_InternFromString("__name__");
2209 if (namestr == NULL)
2210 return NULL;
2211 }
2212 if (pathstr == NULL) {
2213 pathstr = PyString_InternFromString("__path__");
2214 if (pathstr == NULL)
2215 return NULL;
2216 }
2217 if (pkgstr == NULL) {
2218 pkgstr = PyString_InternFromString("__package__");
2219 if (pkgstr == NULL)
2220 return NULL;
2221 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002222
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002223 *buf = '\0';
2224 *p_buflen = 0;
2225 pkgname = PyDict_GetItem(globals, pkgstr);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002226
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002227 if ((pkgname != NULL) && (pkgname != Py_None)) {
2228 /* __package__ is set, so use it */
2229 Py_ssize_t len;
2230 if (!PyString_Check(pkgname)) {
2231 PyErr_SetString(PyExc_ValueError,
2232 "__package__ set to non-string");
2233 return NULL;
2234 }
2235 len = PyString_GET_SIZE(pkgname);
2236 if (len == 0) {
2237 if (level > 0) {
2238 PyErr_SetString(PyExc_ValueError,
2239 "Attempted relative import in non-package");
2240 return NULL;
2241 }
2242 return Py_None;
2243 }
2244 if (len > MAXPATHLEN) {
2245 PyErr_SetString(PyExc_ValueError,
2246 "Package name too long");
2247 return NULL;
2248 }
2249 strcpy(buf, PyString_AS_STRING(pkgname));
2250 } else {
2251 /* __package__ not set, so figure it out and set it */
2252 modname = PyDict_GetItem(globals, namestr);
2253 if (modname == NULL || !PyString_Check(modname))
2254 return Py_None;
Brett Cannonb166afc2010-05-05 20:25:47 +00002255
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002256 modpath = PyDict_GetItem(globals, pathstr);
2257 if (modpath != NULL) {
2258 /* __path__ is set, so modname is already the package name */
2259 Py_ssize_t len = PyString_GET_SIZE(modname);
2260 int error;
2261 if (len > MAXPATHLEN) {
2262 PyErr_SetString(PyExc_ValueError,
2263 "Module name too long");
2264 return NULL;
2265 }
2266 strcpy(buf, PyString_AS_STRING(modname));
2267 error = PyDict_SetItem(globals, pkgstr, modname);
2268 if (error) {
2269 PyErr_SetString(PyExc_ValueError,
2270 "Could not set __package__");
2271 return NULL;
2272 }
2273 } else {
2274 /* Normal module, so work out the package name if any */
2275 char *start = PyString_AS_STRING(modname);
2276 char *lastdot = strrchr(start, '.');
2277 size_t len;
2278 int error;
2279 if (lastdot == NULL && level > 0) {
2280 PyErr_SetString(PyExc_ValueError,
2281 "Attempted relative import in non-package");
2282 return NULL;
2283 }
2284 if (lastdot == NULL) {
2285 error = PyDict_SetItem(globals, pkgstr, Py_None);
2286 if (error) {
2287 PyErr_SetString(PyExc_ValueError,
2288 "Could not set __package__");
2289 return NULL;
2290 }
2291 return Py_None;
2292 }
2293 len = lastdot - start;
2294 if (len >= MAXPATHLEN) {
2295 PyErr_SetString(PyExc_ValueError,
2296 "Module name too long");
2297 return NULL;
2298 }
2299 strncpy(buf, start, len);
2300 buf[len] = '\0';
2301 pkgname = PyString_FromString(buf);
2302 if (pkgname == NULL) {
2303 return NULL;
2304 }
2305 error = PyDict_SetItem(globals, pkgstr, pkgname);
2306 Py_DECREF(pkgname);
2307 if (error) {
2308 PyErr_SetString(PyExc_ValueError,
2309 "Could not set __package__");
2310 return NULL;
2311 }
2312 }
2313 }
2314 while (--level > 0) {
2315 char *dot = strrchr(buf, '.');
2316 if (dot == NULL) {
2317 PyErr_SetString(PyExc_ValueError,
2318 "Attempted relative import beyond "
2319 "toplevel package");
2320 return NULL;
2321 }
2322 *dot = '\0';
2323 }
2324 *p_buflen = strlen(buf);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002325
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002326 modules = PyImport_GetModuleDict();
2327 parent = PyDict_GetItemString(modules, buf);
2328 if (parent == NULL) {
2329 if (orig_level < 1) {
2330 PyObject *err_msg = PyString_FromFormat(
2331 "Parent module '%.200s' not found "
2332 "while handling absolute import", buf);
2333 if (err_msg == NULL) {
2334 return NULL;
2335 }
2336 if (!PyErr_WarnEx(PyExc_RuntimeWarning,
2337 PyString_AsString(err_msg), 1)) {
2338 *buf = '\0';
2339 *p_buflen = 0;
2340 parent = Py_None;
2341 }
2342 Py_DECREF(err_msg);
2343 } else {
2344 PyErr_Format(PyExc_SystemError,
2345 "Parent module '%.200s' not loaded, "
2346 "cannot perform relative import", buf);
2347 }
2348 }
2349 return parent;
2350 /* We expect, but can't guarantee, if parent != None, that:
2351 - parent.__name__ == buf
2352 - parent.__dict__ is globals
2353 If this is violated... Who cares? */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002354}
2355
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002356/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002357static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002358load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002359 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002360{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002361 char *name = *p_name;
2362 char *dot = strchr(name, '.');
2363 size_t len;
2364 char *p;
2365 PyObject *result;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002366
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002367 if (strlen(name) == 0) {
2368 /* completely empty module name should only happen in
2369 'from . import' (or '__import__("")')*/
2370 Py_INCREF(mod);
2371 *p_name = NULL;
2372 return mod;
2373 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002374
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002375 if (dot == NULL) {
2376 *p_name = NULL;
2377 len = strlen(name);
2378 }
2379 else {
2380 *p_name = dot+1;
2381 len = dot-name;
2382 }
2383 if (len == 0) {
2384 PyErr_SetString(PyExc_ValueError,
2385 "Empty module name");
2386 return NULL;
2387 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002388
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002389 p = buf + *p_buflen;
2390 if (p != buf)
2391 *p++ = '.';
2392 if (p+len-buf >= MAXPATHLEN) {
2393 PyErr_SetString(PyExc_ValueError,
2394 "Module name too long");
2395 return NULL;
2396 }
2397 strncpy(p, name, len);
2398 p[len] = '\0';
2399 *p_buflen = p+len-buf;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002400
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002401 result = import_submodule(mod, p, buf);
2402 if (result == Py_None && altmod != mod) {
2403 Py_DECREF(result);
2404 /* Here, altmod must be None and mod must not be None */
2405 result = import_submodule(altmod, p, p);
2406 if (result != NULL && result != Py_None) {
2407 if (mark_miss(buf) != 0) {
2408 Py_DECREF(result);
2409 return NULL;
2410 }
2411 strncpy(buf, name, len);
2412 buf[len] = '\0';
2413 *p_buflen = len;
2414 }
2415 }
2416 if (result == NULL)
2417 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002418
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002419 if (result == Py_None) {
2420 Py_DECREF(result);
2421 PyErr_Format(PyExc_ImportError,
2422 "No module named %.200s", name);
2423 return NULL;
2424 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002425
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002426 return result;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002427}
2428
2429static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002430mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002431{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002432 PyObject *modules = PyImport_GetModuleDict();
2433 return PyDict_SetItemString(modules, name, Py_None);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002434}
2435
2436static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002437ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002438 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002439{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002440 int i;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002441
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002442 if (!PyObject_HasAttrString(mod, "__path__"))
2443 return 1;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002444
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002445 for (i = 0; ; i++) {
2446 PyObject *item = PySequence_GetItem(fromlist, i);
2447 int hasit;
2448 if (item == NULL) {
2449 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2450 PyErr_Clear();
2451 return 1;
2452 }
2453 return 0;
2454 }
2455 if (!PyString_Check(item)) {
2456 PyErr_SetString(PyExc_TypeError,
2457 "Item in ``from list'' not a string");
2458 Py_DECREF(item);
2459 return 0;
2460 }
2461 if (PyString_AS_STRING(item)[0] == '*') {
2462 PyObject *all;
2463 Py_DECREF(item);
2464 /* See if the package defines __all__ */
2465 if (recursive)
2466 continue; /* Avoid endless recursion */
2467 all = PyObject_GetAttrString(mod, "__all__");
2468 if (all == NULL)
2469 PyErr_Clear();
2470 else {
2471 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
2472 Py_DECREF(all);
2473 if (!ret)
2474 return 0;
2475 }
2476 continue;
2477 }
2478 hasit = PyObject_HasAttr(mod, item);
2479 if (!hasit) {
2480 char *subname = PyString_AS_STRING(item);
2481 PyObject *submod;
2482 char *p;
2483 if (buflen + strlen(subname) >= MAXPATHLEN) {
2484 PyErr_SetString(PyExc_ValueError,
2485 "Module name too long");
2486 Py_DECREF(item);
2487 return 0;
2488 }
2489 p = buf + buflen;
2490 *p++ = '.';
2491 strcpy(p, subname);
2492 submod = import_submodule(mod, subname, buf);
2493 Py_XDECREF(submod);
2494 if (submod == NULL) {
2495 Py_DECREF(item);
2496 return 0;
2497 }
2498 }
2499 Py_DECREF(item);
2500 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002501
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002502 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002503}
2504
Neil Schemenauer00b09662003-06-16 21:03:07 +00002505static int
2506add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002507 PyObject *modules)
Neil Schemenauer00b09662003-06-16 21:03:07 +00002508{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002509 if (mod == Py_None)
2510 return 1;
2511 /* Irrespective of the success of this load, make a
2512 reference to it in the parent package module. A copy gets
2513 saved in the modules dictionary under the full name, so get a
2514 reference from there, if need be. (The exception is when the
2515 load failed with a SyntaxError -- then there's no trace in
2516 sys.modules. In that case, of course, do nothing extra.) */
2517 if (submod == NULL) {
2518 submod = PyDict_GetItemString(modules, fullname);
2519 if (submod == NULL)
2520 return 1;
2521 }
2522 if (PyModule_Check(mod)) {
2523 /* We can't use setattr here since it can give a
2524 * spurious warning if the submodule name shadows a
2525 * builtin name */
2526 PyObject *dict = PyModule_GetDict(mod);
2527 if (!dict)
2528 return 0;
2529 if (PyDict_SetItemString(dict, subname, submod) < 0)
2530 return 0;
2531 }
2532 else {
2533 if (PyObject_SetAttrString(mod, subname, submod) < 0)
2534 return 0;
2535 }
2536 return 1;
Neil Schemenauer00b09662003-06-16 21:03:07 +00002537}
2538
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002539static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002540import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002541{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002542 PyObject *modules = PyImport_GetModuleDict();
2543 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002544
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002545 /* Require:
2546 if mod == None: subname == fullname
2547 else: mod.__name__ + "." + subname == fullname
2548 */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002549
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002550 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
2551 Py_INCREF(m);
2552 }
2553 else {
2554 PyObject *path, *loader = NULL;
2555 char buf[MAXPATHLEN+1];
2556 struct filedescr *fdp;
2557 FILE *fp = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002558
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002559 if (mod == Py_None)
2560 path = NULL;
2561 else {
2562 path = PyObject_GetAttrString(mod, "__path__");
2563 if (path == NULL) {
2564 PyErr_Clear();
2565 Py_INCREF(Py_None);
2566 return Py_None;
2567 }
2568 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002569
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002570 buf[0] = '\0';
2571 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
2572 &fp, &loader);
2573 Py_XDECREF(path);
2574 if (fdp == NULL) {
2575 if (!PyErr_ExceptionMatches(PyExc_ImportError))
2576 return NULL;
2577 PyErr_Clear();
2578 Py_INCREF(Py_None);
2579 return Py_None;
2580 }
2581 m = load_module(fullname, fp, buf, fdp->type, loader);
2582 Py_XDECREF(loader);
2583 if (fp)
2584 fclose(fp);
2585 if (!add_submodule(mod, m, fullname, subname, modules)) {
2586 Py_XDECREF(m);
2587 m = NULL;
2588 }
2589 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002590
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002591 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00002592}
2593
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002594
2595/* Re-import a module of any kind and return its module object, WITH
2596 INCREMENTED REFERENCE COUNT */
2597
Guido van Rossum79f25d91997-04-29 20:08:16 +00002598PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002599PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002600{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002601 PyInterpreterState *interp = PyThreadState_Get()->interp;
2602 PyObject *modules_reloading = interp->modules_reloading;
2603 PyObject *modules = PyImport_GetModuleDict();
2604 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
2605 char *name, *subname;
2606 char buf[MAXPATHLEN+1];
2607 struct filedescr *fdp;
2608 FILE *fp = NULL;
2609 PyObject *newm;
Brett Cannonb166afc2010-05-05 20:25:47 +00002610
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002611 if (modules_reloading == NULL) {
2612 Py_FatalError("PyImport_ReloadModule: "
2613 "no modules_reloading dictionary!");
2614 return NULL;
2615 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002616
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002617 if (m == NULL || !PyModule_Check(m)) {
2618 PyErr_SetString(PyExc_TypeError,
2619 "reload() argument must be module");
2620 return NULL;
2621 }
2622 name = PyModule_GetName(m);
2623 if (name == NULL)
2624 return NULL;
2625 if (m != PyDict_GetItemString(modules, name)) {
2626 PyErr_Format(PyExc_ImportError,
2627 "reload(): module %.200s not in sys.modules",
2628 name);
2629 return NULL;
2630 }
2631 existing_m = PyDict_GetItemString(modules_reloading, name);
2632 if (existing_m != NULL) {
2633 /* Due to a recursive reload, this module is already
2634 being reloaded. */
2635 Py_INCREF(existing_m);
2636 return existing_m;
2637 }
2638 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
2639 return NULL;
Collin Winter276887b2007-03-12 16:11:39 +00002640
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002641 subname = strrchr(name, '.');
2642 if (subname == NULL)
2643 subname = name;
2644 else {
2645 PyObject *parentname, *parent;
2646 parentname = PyString_FromStringAndSize(name, (subname-name));
2647 if (parentname == NULL) {
2648 imp_modules_reloading_clear();
2649 return NULL;
2650 }
2651 parent = PyDict_GetItem(modules, parentname);
2652 if (parent == NULL) {
2653 PyErr_Format(PyExc_ImportError,
2654 "reload(): parent %.200s not in sys.modules",
2655 PyString_AS_STRING(parentname));
2656 Py_DECREF(parentname);
2657 imp_modules_reloading_clear();
2658 return NULL;
2659 }
2660 Py_DECREF(parentname);
2661 subname++;
2662 path = PyObject_GetAttrString(parent, "__path__");
2663 if (path == NULL)
2664 PyErr_Clear();
2665 }
2666 buf[0] = '\0';
2667 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
2668 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002669
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002670 if (fdp == NULL) {
2671 Py_XDECREF(loader);
2672 imp_modules_reloading_clear();
2673 return NULL;
2674 }
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002675
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002676 newm = load_module(name, fp, buf, fdp->type, loader);
2677 Py_XDECREF(loader);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00002678
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002679 if (fp)
2680 fclose(fp);
2681 if (newm == NULL) {
2682 /* load_module probably removed name from modules because of
2683 * the error. Put back the original module object. We're
2684 * going to return NULL in this case regardless of whether
2685 * replacing name succeeds, so the return value is ignored.
2686 */
2687 PyDict_SetItemString(modules, name, m);
2688 }
2689 imp_modules_reloading_clear();
2690 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002691}
2692
2693
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002694/* Higher-level import emulator which emulates the "import" statement
2695 more accurately -- it invokes the __import__() function from the
2696 builtins of the current globals. This means that the import is
2697 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002698 environment, e.g. by "rexec".
2699 A dummy list ["__doc__"] is passed as the 4th argument so that
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002700 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00002701 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002702
2703PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002704PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002705{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002706 static PyObject *silly_list = NULL;
2707 static PyObject *builtins_str = NULL;
2708 static PyObject *import_str = NULL;
2709 PyObject *globals = NULL;
2710 PyObject *import = NULL;
2711 PyObject *builtins = NULL;
2712 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002713
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002714 /* Initialize constant string objects */
2715 if (silly_list == NULL) {
2716 import_str = PyString_InternFromString("__import__");
2717 if (import_str == NULL)
2718 return NULL;
2719 builtins_str = PyString_InternFromString("__builtins__");
2720 if (builtins_str == NULL)
2721 return NULL;
2722 silly_list = Py_BuildValue("[s]", "__doc__");
2723 if (silly_list == NULL)
2724 return NULL;
2725 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002726
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002727 /* Get the builtins from current globals */
2728 globals = PyEval_GetGlobals();
2729 if (globals != NULL) {
2730 Py_INCREF(globals);
2731 builtins = PyObject_GetItem(globals, builtins_str);
2732 if (builtins == NULL)
2733 goto err;
2734 }
2735 else {
2736 /* No globals -- use standard builtins, and fake globals */
2737 builtins = PyImport_ImportModuleLevel("__builtin__",
2738 NULL, NULL, NULL, 0);
2739 if (builtins == NULL)
2740 return NULL;
2741 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2742 if (globals == NULL)
2743 goto err;
2744 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002745
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002746 /* Get the __import__ function from the builtins */
2747 if (PyDict_Check(builtins)) {
2748 import = PyObject_GetItem(builtins, import_str);
2749 if (import == NULL)
2750 PyErr_SetObject(PyExc_KeyError, import_str);
2751 }
2752 else
2753 import = PyObject_GetAttr(builtins, import_str);
2754 if (import == NULL)
2755 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002756
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002757 /* Call the __import__ function with the proper argument list
2758 * Always use absolute import here. */
2759 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
2760 globals, silly_list, 0, NULL);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002761
2762 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002763 Py_XDECREF(globals);
2764 Py_XDECREF(builtins);
2765 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002766
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002767 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002768}
2769
2770
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002771/* Module 'imp' provides Python access to the primitives used for
2772 importing modules.
2773*/
2774
Guido van Rossum79f25d91997-04-29 20:08:16 +00002775static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002776imp_get_magic(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002777{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002778 char buf[4];
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002779
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002780 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2781 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2782 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2783 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002784
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002785 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002786}
2787
Guido van Rossum79f25d91997-04-29 20:08:16 +00002788static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00002789imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002790{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002791 PyObject *list;
2792 struct filedescr *fdp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002793
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002794 list = PyList_New(0);
2795 if (list == NULL)
2796 return NULL;
2797 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2798 PyObject *item = Py_BuildValue("ssi",
2799 fdp->suffix, fdp->mode, fdp->type);
2800 if (item == NULL) {
2801 Py_DECREF(list);
2802 return NULL;
2803 }
2804 if (PyList_Append(list, item) < 0) {
2805 Py_DECREF(list);
2806 Py_DECREF(item);
2807 return NULL;
2808 }
2809 Py_DECREF(item);
2810 }
2811 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002812}
2813
Guido van Rossum79f25d91997-04-29 20:08:16 +00002814static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002815call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002816{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002817 extern int fclose(FILE *);
2818 PyObject *fob, *ret;
2819 struct filedescr *fdp;
2820 char pathname[MAXPATHLEN+1];
2821 FILE *fp = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002822
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002823 pathname[0] = '\0';
2824 if (path == Py_None)
2825 path = NULL;
2826 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
2827 if (fdp == NULL)
2828 return NULL;
2829 if (fp != NULL) {
2830 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
Victor Stinner63c22fa2011-09-23 19:37:03 +02002831 if (fob == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002832 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002833 }
2834 else {
2835 fob = Py_None;
2836 Py_INCREF(fob);
2837 }
2838 ret = Py_BuildValue("Os(ssi)",
2839 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
2840 Py_DECREF(fob);
2841 return ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002842}
2843
Guido van Rossum79f25d91997-04-29 20:08:16 +00002844static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002845imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002846{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002847 char *name;
2848 PyObject *path = NULL;
2849 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
2850 return NULL;
2851 return call_find_module(name, path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002852}
2853
2854static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002855imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002856{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002857 char *name;
2858 int ret;
2859 PyObject *m;
2860 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
2861 return NULL;
2862 ret = init_builtin(name);
2863 if (ret < 0)
2864 return NULL;
2865 if (ret == 0) {
2866 Py_INCREF(Py_None);
2867 return Py_None;
2868 }
2869 m = PyImport_AddModule(name);
2870 Py_XINCREF(m);
2871 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002872}
2873
Guido van Rossum79f25d91997-04-29 20:08:16 +00002874static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002875imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002876{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002877 char *name;
2878 int ret;
2879 PyObject *m;
2880 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
2881 return NULL;
2882 ret = PyImport_ImportFrozenModule(name);
2883 if (ret < 0)
2884 return NULL;
2885 if (ret == 0) {
2886 Py_INCREF(Py_None);
2887 return Py_None;
2888 }
2889 m = PyImport_AddModule(name);
2890 Py_XINCREF(m);
2891 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002892}
2893
Guido van Rossum79f25d91997-04-29 20:08:16 +00002894static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002895imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002896{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002897 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002898
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002899 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
2900 return NULL;
2901 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002902}
2903
Guido van Rossum79f25d91997-04-29 20:08:16 +00002904static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002905imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002906{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002907 char *name;
2908 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
2909 return NULL;
2910 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002911}
2912
Guido van Rossum79f25d91997-04-29 20:08:16 +00002913static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002914imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002915{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002916 char *name;
2917 struct _frozen *p;
2918 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
2919 return NULL;
2920 p = find_frozen(name);
2921 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002922}
2923
2924static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002925get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002926{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002927 FILE *fp;
2928 if (fob == NULL) {
2929 if (mode[0] == 'U')
2930 mode = "r" PY_STDIOTEXTMODE;
2931 fp = fopen(pathname, mode);
2932 if (fp == NULL)
2933 PyErr_SetFromErrno(PyExc_IOError);
2934 }
2935 else {
2936 fp = PyFile_AsFile(fob);
2937 if (fp == NULL)
2938 PyErr_SetString(PyExc_ValueError,
2939 "bad/closed file object");
2940 }
2941 return fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002942}
2943
Guido van Rossum79f25d91997-04-29 20:08:16 +00002944static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002945imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002946{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002947 char *name;
2948 char *pathname;
2949 PyObject *fob = NULL;
2950 PyObject *m;
2951 FILE *fp;
2952 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
2953 &PyFile_Type, &fob))
2954 return NULL;
2955 fp = get_file(pathname, fob, "rb");
2956 if (fp == NULL)
2957 return NULL;
2958 m = load_compiled_module(name, pathname, fp);
2959 if (fob == NULL)
2960 fclose(fp);
2961 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002962}
2963
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002964#ifdef HAVE_DYNAMIC_LOADING
2965
Guido van Rossum79f25d91997-04-29 20:08:16 +00002966static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002967imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002968{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002969 char *name;
2970 char *pathname;
2971 PyObject *fob = NULL;
2972 PyObject *m;
2973 FILE *fp = NULL;
2974 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
2975 &PyFile_Type, &fob))
2976 return NULL;
2977 if (fob) {
2978 fp = get_file(pathname, fob, "r");
2979 if (fp == NULL)
2980 return NULL;
2981 }
2982 m = _PyImport_LoadDynamicModule(name, pathname, fp);
2983 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002984}
2985
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002986#endif /* HAVE_DYNAMIC_LOADING */
2987
Guido van Rossum79f25d91997-04-29 20:08:16 +00002988static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002989imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002990{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002991 char *name;
2992 char *pathname;
2993 PyObject *fob = NULL;
2994 PyObject *m;
2995 FILE *fp;
2996 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
2997 &PyFile_Type, &fob))
2998 return NULL;
2999 fp = get_file(pathname, fob, "r");
3000 if (fp == NULL)
3001 return NULL;
3002 m = load_source_module(name, pathname, fp);
3003 if (fob == NULL)
3004 fclose(fp);
3005 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003006}
3007
Guido van Rossum79f25d91997-04-29 20:08:16 +00003008static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003009imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003010{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003011 char *name;
3012 PyObject *fob;
3013 char *pathname;
3014 char *suffix; /* Unused */
3015 char *mode;
3016 int type;
3017 FILE *fp;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003018
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003019 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
3020 &name, &fob, &pathname,
3021 &suffix, &mode, &type))
3022 return NULL;
3023 if (*mode) {
3024 /* Mode must start with 'r' or 'U' and must not contain '+'.
3025 Implicit in this test is the assumption that the mode
3026 may contain other modifiers like 'b' or 't'. */
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003027
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003028 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
3029 PyErr_Format(PyExc_ValueError,
3030 "invalid file open mode %.200s", mode);
3031 return NULL;
3032 }
3033 }
3034 if (fob == Py_None)
3035 fp = NULL;
3036 else {
3037 if (!PyFile_Check(fob)) {
3038 PyErr_SetString(PyExc_ValueError,
3039 "load_module arg#2 should be a file or None");
3040 return NULL;
3041 }
3042 fp = get_file(pathname, fob, mode);
3043 if (fp == NULL)
3044 return NULL;
3045 }
3046 return load_module(name, fp, pathname, type, NULL);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003047}
3048
3049static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003050imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003051{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003052 char *name;
3053 char *pathname;
3054 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
3055 return NULL;
3056 return load_package(name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003057}
3058
3059static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003060imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003061{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003062 char *name;
3063 if (!PyArg_ParseTuple(args, "s:new_module", &name))
3064 return NULL;
3065 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003066}
3067
Brett Cannon3aa2a492008-08-06 22:28:09 +00003068static PyObject *
3069imp_reload(PyObject *self, PyObject *v)
3070{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003071 return PyImport_ReloadModule(v);
Brett Cannon3aa2a492008-08-06 22:28:09 +00003072}
3073
3074
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003075/* Doc strings */
3076
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003077PyDoc_STRVAR(doc_imp,
3078"This module provides the components needed to build your own\n\
3079__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003080
Brett Cannon3aa2a492008-08-06 22:28:09 +00003081PyDoc_STRVAR(doc_reload,
3082"reload(module) -> module\n\
3083\n\
3084Reload the module. The module must have been successfully imported before.");
3085
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003086PyDoc_STRVAR(doc_find_module,
3087"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003088Search for a module. If path is omitted or None, search for a\n\
3089built-in, frozen or special module and continue search in sys.path.\n\
3090The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003091package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003093PyDoc_STRVAR(doc_load_module,
3094"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003095Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003096The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003097
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003098PyDoc_STRVAR(doc_get_magic,
3099"get_magic() -> string\n\
3100Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003101
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003102PyDoc_STRVAR(doc_get_suffixes,
3103"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003104Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003105that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003106
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003107PyDoc_STRVAR(doc_new_module,
3108"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003109Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003110The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003112PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00003113"lock_held() -> boolean\n\
3114Return True if the import lock is currently held, else False.\n\
3115On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00003116
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003117PyDoc_STRVAR(doc_acquire_lock,
3118"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00003119Acquires the interpreter's import lock for the current thread.\n\
3120This lock should be used by import hooks to ensure thread-safety\n\
3121when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003122On platforms without threads, this function does nothing.");
3123
3124PyDoc_STRVAR(doc_release_lock,
3125"release_lock() -> None\n\
3126Release the interpreter's import lock.\n\
3127On platforms without threads, this function does nothing.");
3128
Guido van Rossum79f25d91997-04-29 20:08:16 +00003129static PyMethodDef imp_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003130 {"reload", imp_reload, METH_O, doc_reload},
3131 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3132 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3133 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3134 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3135 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3136 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3137 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3138 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
3139 /* The rest are obsolete */
3140 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
3141 {"init_builtin", imp_init_builtin, METH_VARARGS},
3142 {"init_frozen", imp_init_frozen, METH_VARARGS},
3143 {"is_builtin", imp_is_builtin, METH_VARARGS},
3144 {"is_frozen", imp_is_frozen, METH_VARARGS},
3145 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003146#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003147 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003148#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003149 {"load_package", imp_load_package, METH_VARARGS},
3150 {"load_source", imp_load_source, METH_VARARGS},
3151 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003152};
3153
Guido van Rossum1a8791e1998-08-04 22:46:29 +00003154static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003155setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003156{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003157 PyObject *v;
3158 int err;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003159
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003160 v = PyInt_FromLong((long)value);
3161 err = PyDict_SetItemString(d, name, v);
3162 Py_XDECREF(v);
3163 return err;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003164}
3165
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003166typedef struct {
3167 PyObject_HEAD
3168} NullImporter;
3169
3170static int
3171NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3172{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003173 char *path;
3174 Py_ssize_t pathlen;
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003175
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003176 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3177 return -1;
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003178
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003179 if (!PyArg_ParseTuple(args, "s:NullImporter",
3180 &path))
3181 return -1;
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003182
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003183 pathlen = strlen(path);
3184 if (pathlen == 0) {
3185 PyErr_SetString(PyExc_ImportError, "empty pathname");
3186 return -1;
3187 } else {
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003188#ifndef RISCOS
Kristján Valur Jónsson3b2a6b82009-01-09 20:10:59 +00003189#ifndef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003190 struct stat statbuf;
3191 int rv;
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003192
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003193 rv = stat(path, &statbuf);
3194 if (rv == 0) {
3195 /* it exists */
3196 if (S_ISDIR(statbuf.st_mode)) {
3197 /* it's a directory */
3198 PyErr_SetString(PyExc_ImportError,
3199 "existing directory");
3200 return -1;
3201 }
3202 }
Kristján Valur Jónsson3b2a6b82009-01-09 20:10:59 +00003203#else /* MS_WINDOWS */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003204 DWORD rv;
3205 /* see issue1293 and issue3677:
3206 * stat() on Windows doesn't recognise paths like
3207 * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
3208 */
3209 rv = GetFileAttributesA(path);
3210 if (rv != INVALID_FILE_ATTRIBUTES) {
3211 /* it exists */
3212 if (rv & FILE_ATTRIBUTE_DIRECTORY) {
3213 /* it's a directory */
3214 PyErr_SetString(PyExc_ImportError,
3215 "existing directory");
3216 return -1;
3217 }
3218 }
Kristján Valur Jónsson3b2a6b82009-01-09 20:10:59 +00003219#endif
3220#else /* RISCOS */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003221 if (object_exists(path)) {
3222 /* it exists */
3223 if (isdir(path)) {
3224 /* it's a directory */
3225 PyErr_SetString(PyExc_ImportError,
3226 "existing directory");
3227 return -1;
3228 }
3229 }
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003230#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003231 }
3232 return 0;
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003233}
3234
3235static PyObject *
3236NullImporter_find_module(NullImporter *self, PyObject *args)
3237{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003238 Py_RETURN_NONE;
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003239}
3240
3241static PyMethodDef NullImporter_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003242 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3243 "Always return None"
3244 },
3245 {NULL} /* Sentinel */
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003246};
3247
3248
Nick Coghlan327a39b2007-11-18 11:56:28 +00003249PyTypeObject PyNullImporter_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003250 PyVarObject_HEAD_INIT(NULL, 0)
3251 "imp.NullImporter", /*tp_name*/
3252 sizeof(NullImporter), /*tp_basicsize*/
3253 0, /*tp_itemsize*/
3254 0, /*tp_dealloc*/
3255 0, /*tp_print*/
3256 0, /*tp_getattr*/
3257 0, /*tp_setattr*/
3258 0, /*tp_compare*/
3259 0, /*tp_repr*/
3260 0, /*tp_as_number*/
3261 0, /*tp_as_sequence*/
3262 0, /*tp_as_mapping*/
3263 0, /*tp_hash */
3264 0, /*tp_call*/
3265 0, /*tp_str*/
3266 0, /*tp_getattro*/
3267 0, /*tp_setattro*/
3268 0, /*tp_as_buffer*/
3269 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3270 "Null importer object", /* tp_doc */
3271 0, /* tp_traverse */
3272 0, /* tp_clear */
3273 0, /* tp_richcompare */
3274 0, /* tp_weaklistoffset */
3275 0, /* tp_iter */
3276 0, /* tp_iternext */
3277 NullImporter_methods, /* tp_methods */
3278 0, /* tp_members */
3279 0, /* tp_getset */
3280 0, /* tp_base */
3281 0, /* tp_dict */
3282 0, /* tp_descr_get */
3283 0, /* tp_descr_set */
3284 0, /* tp_dictoffset */
3285 (initproc)NullImporter_init, /* tp_init */
3286 0, /* tp_alloc */
3287 PyType_GenericNew /* tp_new */
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003288};
3289
3290
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003291PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003292initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003293{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003294 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003295
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003296 if (PyType_Ready(&PyNullImporter_Type) < 0)
3297 goto failure;
Phillip J. Ebyf7575d02006-07-28 21:12:07 +00003298
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003299 m = Py_InitModule4("imp", imp_methods, doc_imp,
3300 NULL, PYTHON_API_VERSION);
3301 if (m == NULL)
3302 goto failure;
3303 d = PyModule_GetDict(m);
3304 if (d == NULL)
3305 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003306
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003307 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
3308 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
3309 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
3310 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
3311 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
3312 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
3313 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
3314 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
3315 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
3316 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003317
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003318 Py_INCREF(&PyNullImporter_Type);
3319 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003320 failure:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003321 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003322}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003323
3324
Guido van Rossumb18618d2000-05-03 23:44:39 +00003325/* API for embedding applications that want to add their own entries
3326 to the table of built-in modules. This should normally be called
3327 *before* Py_Initialize(). When the table resize fails, -1 is
3328 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003329
3330 After a similar function by Just van Rossum. */
3331
3332int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003333PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003334{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003335 static struct _inittab *our_copy = NULL;
3336 struct _inittab *p;
3337 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003338
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003339 /* Count the number of entries in both tables */
3340 for (n = 0; newtab[n].name != NULL; n++)
3341 ;
3342 if (n == 0)
3343 return 0; /* Nothing to do */
3344 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
3345 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003346
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003347 /* Allocate new memory for the combined table */
3348 p = our_copy;
3349 PyMem_RESIZE(p, struct _inittab, i+n+1);
3350 if (p == NULL)
3351 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003352
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003353 /* Copy the tables into the new memory */
3354 if (our_copy != PyImport_Inittab)
3355 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
3356 PyImport_Inittab = our_copy = p;
3357 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003358
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003359 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003360}
3361
3362/* Shorthand to add a single entry given a name and a function */
3363
3364int
Brett Cannonc4f90eb2009-04-02 03:17:39 +00003365PyImport_AppendInittab(const char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003366{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003367 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003368
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003369 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003370
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003371 newtab[0].name = (char *)name;
3372 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003373
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003374 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00003375}
Anthony Baxterac6bd462006-04-13 02:06:09 +00003376
3377#ifdef __cplusplus
3378}
3379#endif