blob: 5c53813462dbb470d78a58f91e1a3b35f98760c8 [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
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include "token.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +00009#include "marshal.h"
10#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000011#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000012#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000013#include "importdl.h"
Jack Jansen9c96a921995-02-15 22:57:06 +000014#ifdef macintosh
15#include "macglue.h"
16#endif
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
21
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000022extern time_t PyOS_GetLastModificationTime(char *, FILE *);
23 /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000024
Guido van Rossum6c849691994-09-26 15:47:17 +000025/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000026/* Change for each incompatible change */
27/* The value of CR and LF is incorporated so if you ever read or write
28 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000029 Apple MPW compiler swaps their values, botching string constants.
30 XXX That probably isn't important anymore.
31*/
Guido van Rossum7faeab31995-07-07 22:50:36 +000032/* XXX Perhaps the magic number should be frozen and a version field
33 added to the .pyc file header? */
Tim Peters36515e22001-11-18 04:06:29 +000034/* New way to come up with the low 16 bits of the magic number:
35 (YEAR-1995) * 10000 + MONTH * 100 + DAY
36 where MONTH and DAY are 1-based.
37 XXX Whatever the "old way" may have been isn't documented.
38 XXX This scheme breaks in 2002, as (2002-1995)*10000 = 70000 doesn't
39 fit in 16 bits.
40 XXX Later, sometimes 1 gets added to MAGIC in order to record that
41 the Unicode -U option is in use. IMO (Tim's), that's a Bad Idea
42 (quite apart from that the -U option doesn't work so isn't used
43 anyway).
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000044
45 XXX MAL, 2002-02-07: I had to modify the MAGIC due to a fix of the
46 UTF-8 encoder (it previously produced invalid UTF-8 for unpaired
47 high surrogates), so I simply bumped the month value to 20 (invalid
48 month) and set the day to 1. This should be recognizable by any
49 algorithm relying on the above scheme. Perhaps we should simply
50 start counting in increments of 10 from now on ?!
51
52 Known values:
53 Python 1.5: 20121
54 Python 1.5.1: 20121
55 Python 1.5.2: 20121
56 Python 2.0: 50823
57 Python 2.0.1: 50823
58 Python 2.1: 60202
59 Python 2.1.1: 60202
60 Python 2.1.2: 60202
61 Python 2.2: 60717
62 Python 2.3a0: 62001
Tim Peters36515e22001-11-18 04:06:29 +000063*/
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000064#define MAGIC (62001 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000065
Guido van Rossum96774c12000-05-01 20:19:08 +000066/* Magic word as global; note that _PyImport_Init() can change the
Jeremy Hylton9262b8a2000-06-30 04:59:17 +000067 value of this global to accommodate for alterations of how the
Guido van Rossum96774c12000-05-01 20:19:08 +000068 compiler works which are enabled by command line switches. */
69static long pyc_magic = MAGIC;
70
Guido van Rossum25ce5661997-08-02 03:10:38 +000071/* See _PyImport_FixupExtension() below */
72static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000073
Guido van Rossum771c6c81997-10-31 18:37:24 +000074/* This table is defined in config.c: */
75extern struct _inittab _PyImport_Inittab[];
76
77struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000078
Guido van Rossumed1170e1999-12-20 21:23:41 +000079/* these tables define the module suffixes that Python recognizes */
80struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +000081
82#ifdef RISCOS
83static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000084 {"/py", "U", PY_SOURCE},
Guido van Rossum48a680c2001-03-02 06:34:14 +000085 {"/pyc", "rb", PY_COMPILED},
86 {0, 0}
87};
88#else
Guido van Rossumed1170e1999-12-20 21:23:41 +000089static const struct filedescr _PyImport_StandardFiletab[] = {
Jack Jansenc88da1f2002-05-28 10:58:19 +000090 {".py", "U", PY_SOURCE},
Tim Petersc1731372001-08-04 08:12:36 +000091#ifdef MS_WIN32
Jack Jansenc88da1f2002-05-28 10:58:19 +000092 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +000093#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000094 {".pyc", "rb", PY_COMPILED},
95 {0, 0}
96};
Guido van Rossum48a680c2001-03-02 06:34:14 +000097#endif
Guido van Rossumed1170e1999-12-20 21:23:41 +000098
Guido van Rossum1ae940a1995-01-02 19:04:15 +000099/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100
101void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000102_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103{
Guido van Rossumed1170e1999-12-20 21:23:41 +0000104 const struct filedescr *scan;
105 struct filedescr *filetab;
106 int countD = 0;
107 int countS = 0;
108
109 /* prepare _PyImport_Filetab: copy entries from
110 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
111 */
112 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
113 ++countD;
114 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
115 ++countS;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000116 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
Guido van Rossumed1170e1999-12-20 21:23:41 +0000117 memcpy(filetab, _PyImport_DynLoadFiletab,
118 countD * sizeof(struct filedescr));
119 memcpy(filetab + countD, _PyImport_StandardFiletab,
120 countS * sizeof(struct filedescr));
121 filetab[countD + countS].suffix = NULL;
122
123 _PyImport_Filetab = filetab;
124
Guido van Rossum0824f631997-03-11 18:37:35 +0000125 if (Py_OptimizeFlag) {
Guido van Rossumed1170e1999-12-20 21:23:41 +0000126 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
127 for (; filetab->suffix != NULL; filetab++) {
Guido van Rossum48a680c2001-03-02 06:34:14 +0000128#ifndef RISCOS
Guido van Rossumed1170e1999-12-20 21:23:41 +0000129 if (strcmp(filetab->suffix, ".pyc") == 0)
130 filetab->suffix = ".pyo";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000131#else
132 if (strcmp(filetab->suffix, "/pyc") == 0)
133 filetab->suffix = "/pyo";
134#endif
Guido van Rossum0824f631997-03-11 18:37:35 +0000135 }
136 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000137
138 if (Py_UnicodeFlag) {
139 /* Fix the pyc_magic so that byte compiled code created
140 using the all-Unicode method doesn't interfere with
141 code created in normal operation mode. */
142 pyc_magic = MAGIC + 1;
143 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144}
145
Guido van Rossum25ce5661997-08-02 03:10:38 +0000146void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000147_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000148{
149 Py_XDECREF(extensions);
150 extensions = NULL;
Barry Warsaw84294482000-10-03 16:02:05 +0000151 PyMem_DEL(_PyImport_Filetab);
152 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000153}
154
155
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000156/* Locking primitives to prevent parallel imports of the same module
157 in different threads to return with a partially loaded module.
158 These calls are serialized by the global interpreter lock. */
159
160#ifdef WITH_THREAD
161
Guido van Rossum49b56061998-10-01 20:42:43 +0000162#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000163
Guido van Rossum65d5b571998-12-21 19:32:43 +0000164static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000165static long import_lock_thread = -1;
166static int import_lock_level = 0;
167
168static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000169lock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000170{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000171 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000172 if (me == -1)
173 return; /* Too bad */
174 if (import_lock == NULL)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000175 import_lock = PyThread_allocate_lock();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000176 if (import_lock_thread == me) {
177 import_lock_level++;
178 return;
179 }
Guido van Rossum65d5b571998-12-21 19:32:43 +0000180 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) {
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000181 PyThreadState *tstate = PyEval_SaveThread();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000182 PyThread_acquire_lock(import_lock, 1);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000183 PyEval_RestoreThread(tstate);
184 }
185 import_lock_thread = me;
186 import_lock_level = 1;
187}
188
189static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000190unlock_import(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000191{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000192 long me = PyThread_get_thread_ident();
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000193 if (me == -1)
194 return; /* Too bad */
195 if (import_lock_thread != me)
196 Py_FatalError("unlock_import: not holding the import lock");
197 import_lock_level--;
198 if (import_lock_level == 0) {
199 import_lock_thread = -1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000200 PyThread_release_lock(import_lock);
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000201 }
202}
203
204#else
205
206#define lock_import()
207#define unlock_import()
208
209#endif
210
Tim Peters69232342001-08-30 05:16:13 +0000211static PyObject *
212imp_lock_held(PyObject *self, PyObject *args)
213{
214 if (!PyArg_ParseTuple(args, ":lock_held"))
215 return NULL;
216#ifdef WITH_THREAD
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000217 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000218#else
Guido van Rossumb8bff3f2002-04-07 06:34:38 +0000219 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000220#endif
221}
222
Guido van Rossum25ce5661997-08-02 03:10:38 +0000223/* Helper for sys */
224
225PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000227{
228 PyInterpreterState *interp = PyThreadState_Get()->interp;
229 if (interp->modules == NULL)
230 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
231 return interp->modules;
232}
233
Guido van Rossum3f5da241990-12-20 15:06:42 +0000234
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000235/* List of names to clear in sys */
236static char* sys_deletes[] = {
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000237 "path", "argv", "ps1", "ps2", "exitfunc",
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000238 "exc_type", "exc_value", "exc_traceback",
239 "last_type", "last_value", "last_traceback",
240 NULL
241};
242
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000243static char* sys_files[] = {
244 "stdin", "__stdin__",
245 "stdout", "__stdout__",
246 "stderr", "__stderr__",
247 NULL
248};
249
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000250
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000251/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000252
Guido van Rossum3f5da241990-12-20 15:06:42 +0000253void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000254PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000255{
Guido van Rossum758eec01998-01-19 21:58:26 +0000256 int pos, ndone;
257 char *name;
258 PyObject *key, *value, *dict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000259 PyInterpreterState *interp = PyThreadState_Get()->interp;
Guido van Rossum758eec01998-01-19 21:58:26 +0000260 PyObject *modules = interp->modules;
261
262 if (modules == NULL)
263 return; /* Already done */
264
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000265 /* Delete some special variables first. These are common
266 places where user values hide and people complain when their
267 destructors fail. Since the modules containing them are
268 deleted *last* of all, they would come too late in the normal
269 destruction order. Sigh. */
270
271 value = PyDict_GetItemString(modules, "__builtin__");
272 if (value != NULL && PyModule_Check(value)) {
273 dict = PyModule_GetDict(value);
274 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000275 PySys_WriteStderr("# clear __builtin__._\n");
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000276 PyDict_SetItemString(dict, "_", Py_None);
277 }
278 value = PyDict_GetItemString(modules, "sys");
279 if (value != NULL && PyModule_Check(value)) {
280 char **p;
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000281 PyObject *v;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000282 dict = PyModule_GetDict(value);
283 for (p = sys_deletes; *p != NULL; p++) {
284 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000285 PySys_WriteStderr("# clear sys.%s\n", *p);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000286 PyDict_SetItemString(dict, *p, Py_None);
287 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000288 for (p = sys_files; *p != NULL; p+=2) {
289 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000290 PySys_WriteStderr("# restore sys.%s\n", *p);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000291 v = PyDict_GetItemString(dict, *(p+1));
292 if (v == NULL)
293 v = Py_None;
294 PyDict_SetItemString(dict, *p, v);
295 }
296 }
297
298 /* First, delete __main__ */
299 value = PyDict_GetItemString(modules, "__main__");
300 if (value != NULL && PyModule_Check(value)) {
301 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000302 PySys_WriteStderr("# cleanup __main__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000303 _PyModule_Clear(value);
304 PyDict_SetItemString(modules, "__main__", Py_None);
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000305 }
306
Guido van Rossum758eec01998-01-19 21:58:26 +0000307 /* The special treatment of __builtin__ here is because even
308 when it's not referenced as a module, its dictionary is
309 referenced by almost every module's __builtins__. Since
310 deleting a module clears its dictionary (even if there are
311 references left to it), we need to delete the __builtin__
312 module last. Likewise, we don't delete sys until the very
313 end because it is implicitly referenced (e.g. by print).
314
315 Also note that we 'delete' modules by replacing their entry
316 in the modules dict with None, rather than really deleting
317 them; this avoids a rehash of the modules dictionary and
318 also marks them as "non existent" so they won't be
319 re-imported. */
320
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000321 /* Next, repeatedly delete modules with a reference count of
Guido van Rossum758eec01998-01-19 21:58:26 +0000322 one (skipping __builtin__ and sys) and delete them */
323 do {
324 ndone = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000325 pos = 0;
Guido van Rossum758eec01998-01-19 21:58:26 +0000326 while (PyDict_Next(modules, &pos, &key, &value)) {
327 if (value->ob_refcnt != 1)
328 continue;
Guido van Rossum566373e1998-10-01 15:24:50 +0000329 if (PyString_Check(key) && PyModule_Check(value)) {
330 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000331 if (strcmp(name, "__builtin__") == 0)
332 continue;
333 if (strcmp(name, "sys") == 0)
334 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000335 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000336 PySys_WriteStderr(
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000337 "# cleanup[1] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000338 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000339 PyDict_SetItem(modules, key, Py_None);
340 ndone++;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000341 }
342 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000343 } while (ndone > 0);
344
Guido van Rossum758eec01998-01-19 21:58:26 +0000345 /* Next, delete all modules (still skipping __builtin__ and sys) */
346 pos = 0;
347 while (PyDict_Next(modules, &pos, &key, &value)) {
Guido van Rossum566373e1998-10-01 15:24:50 +0000348 if (PyString_Check(key) && PyModule_Check(value)) {
349 name = PyString_AS_STRING(key);
Guido van Rossum758eec01998-01-19 21:58:26 +0000350 if (strcmp(name, "__builtin__") == 0)
351 continue;
352 if (strcmp(name, "sys") == 0)
353 continue;
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000354 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000355 PySys_WriteStderr("# cleanup[2] %s\n", name);
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000356 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000357 PyDict_SetItem(modules, key, Py_None);
358 }
359 }
360
361 /* Next, delete sys and __builtin__ (in that order) */
362 value = PyDict_GetItemString(modules, "sys");
363 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000364 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000365 PySys_WriteStderr("# cleanup sys\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000366 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000367 PyDict_SetItemString(modules, "sys", Py_None);
368 }
369 value = PyDict_GetItemString(modules, "__builtin__");
370 if (value != NULL && PyModule_Check(value)) {
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000371 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000372 PySys_WriteStderr("# cleanup __builtin__\n");
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000373 _PyModule_Clear(value);
Guido van Rossum758eec01998-01-19 21:58:26 +0000374 PyDict_SetItemString(modules, "__builtin__", Py_None);
375 }
376
377 /* Finally, clear and delete the modules directory */
378 PyDict_Clear(modules);
379 interp->modules = NULL;
380 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000381}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000382
383
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000384/* Helper for pythonrun.c -- return magic number */
385
386long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000387PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000388{
Guido van Rossum96774c12000-05-01 20:19:08 +0000389 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000390}
391
392
Guido van Rossum25ce5661997-08-02 03:10:38 +0000393/* Magic for extension modules (built-in as well as dynamically
394 loaded). To prevent initializing an extension module more than
395 once, we keep a static dictionary 'extensions' keyed by module name
396 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000397 modules), containing these modules. A copy of the module's
Guido van Rossum25ce5661997-08-02 03:10:38 +0000398 dictionary is stored by calling _PyImport_FixupExtension()
399 immediately after the module initialization function succeeds. A
400 copy can be retrieved from there by calling
401 _PyImport_FindExtension(). */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000402
Guido van Rossum79f25d91997-04-29 20:08:16 +0000403PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000404_PyImport_FixupExtension(char *name, char *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000405{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000406 PyObject *modules, *mod, *dict, *copy;
407 if (extensions == NULL) {
408 extensions = PyDict_New();
409 if (extensions == NULL)
410 return NULL;
411 }
412 modules = PyImport_GetModuleDict();
413 mod = PyDict_GetItemString(modules, name);
414 if (mod == NULL || !PyModule_Check(mod)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000415 PyErr_Format(PyExc_SystemError,
416 "_PyImport_FixupExtension: module %.200s not loaded", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000417 return NULL;
418 }
419 dict = PyModule_GetDict(mod);
420 if (dict == NULL)
421 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000422 copy = PyDict_Copy(dict);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000423 if (copy == NULL)
424 return NULL;
425 PyDict_SetItemString(extensions, filename, copy);
426 Py_DECREF(copy);
427 return copy;
428}
429
430PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000431_PyImport_FindExtension(char *name, char *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000432{
Fred Drake9cd0efc2001-10-25 21:38:59 +0000433 PyObject *dict, *mod, *mdict;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000434 if (extensions == NULL)
435 return NULL;
436 dict = PyDict_GetItemString(extensions, filename);
437 if (dict == NULL)
438 return NULL;
439 mod = PyImport_AddModule(name);
440 if (mod == NULL)
441 return NULL;
442 mdict = PyModule_GetDict(mod);
443 if (mdict == NULL)
444 return NULL;
Fred Drake9cd0efc2001-10-25 21:38:59 +0000445 if (PyDict_Update(mdict, dict))
Guido van Rossum25ce5661997-08-02 03:10:38 +0000446 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000447 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000448 PySys_WriteStderr("import %s # previously loaded (%s)\n",
Guido van Rossum25ce5661997-08-02 03:10:38 +0000449 name, filename);
450 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000451}
452
453
454/* Get the module object corresponding to a module name.
455 First check the modules dictionary if there's one there,
456 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000457 Because the former action is most common, THIS DOES NOT RETURN A
458 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000459
Guido van Rossum79f25d91997-04-29 20:08:16 +0000460PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000461PyImport_AddModule(char *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000462{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000463 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000464 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000465
Guido van Rossum25ce5661997-08-02 03:10:38 +0000466 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
Guido van Rossum79f25d91997-04-29 20:08:16 +0000467 PyModule_Check(m))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000468 return m;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000469 m = PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000470 if (m == NULL)
471 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000472 if (PyDict_SetItemString(modules, name, m) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000473 Py_DECREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000474 return NULL;
475 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000476 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000477
478 return m;
479}
480
481
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000482/* Execute a code object in a module and return the module object
483 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000484
Guido van Rossum79f25d91997-04-29 20:08:16 +0000485PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000486PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000487{
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000488 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
489}
490
491PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000492PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000493{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000494 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum79f25d91997-04-29 20:08:16 +0000495 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000496
Guido van Rossum79f25d91997-04-29 20:08:16 +0000497 m = PyImport_AddModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000498 if (m == NULL)
499 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000500 d = PyModule_GetDict(m);
501 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
502 if (PyDict_SetItemString(d, "__builtins__",
503 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000504 return NULL;
505 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000506 /* Remember the filename as the __file__ attribute */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000507 v = NULL;
508 if (pathname != NULL) {
509 v = PyString_FromString(pathname);
510 if (v == NULL)
511 PyErr_Clear();
512 }
513 if (v == NULL) {
514 v = ((PyCodeObject *)co)->co_filename;
515 Py_INCREF(v);
516 }
517 if (PyDict_SetItemString(d, "__file__", v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000518 PyErr_Clear(); /* Not important enough to report */
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000519 Py_DECREF(v);
520
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000521 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000522 if (v == NULL)
523 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000524 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000525
Guido van Rossum25ce5661997-08-02 03:10:38 +0000526 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000527 PyErr_Format(PyExc_ImportError,
528 "Loaded module %.200s not found in sys.modules",
529 name);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000530 return NULL;
531 }
532
Guido van Rossum79f25d91997-04-29 20:08:16 +0000533 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000534
535 return m;
536}
537
538
539/* Given a pathname for a Python source file, fill a buffer with the
540 pathname for the corresponding compiled file. Return the pathname
541 for the compiled file, or NULL if there's no space in the buffer.
542 Doesn't set an exception. */
543
544static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000545make_compiled_pathname(char *pathname, char *buf, size_t buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000546{
Tim Petersc1731372001-08-04 08:12:36 +0000547 size_t len = strlen(pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000548 if (len+2 > buflen)
549 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000550
551#ifdef MS_WIN32
552 /* Treat .pyw as if it were .py. The case of ".pyw" must match
553 that used in _PyImport_StandardFiletab. */
554 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
555 --len; /* pretend 'w' isn't there */
556#endif
557 memcpy(buf, pathname, len);
558 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
559 buf[len+1] = '\0';
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000560
561 return buf;
562}
563
564
565/* Given a pathname for a Python source file, its time of last
566 modification, and a pathname for a compiled file, check whether the
567 compiled file represents the same version of the source. If so,
568 return a FILE pointer for the compiled file, positioned just after
569 the header; if not, return NULL.
570 Doesn't set an exception. */
571
572static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000573check_compiled_module(char *pathname, long mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574{
575 FILE *fp;
576 long magic;
577 long pyc_mtime;
578
579 fp = fopen(cpathname, "rb");
580 if (fp == NULL)
581 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000582 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000583 if (magic != pyc_magic) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000584 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000585 PySys_WriteStderr("# %s has bad magic\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586 fclose(fp);
587 return NULL;
588 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000589 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000590 if (pyc_mtime != mtime) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000592 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000593 fclose(fp);
594 return NULL;
595 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000596 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000597 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000598 return fp;
599}
600
601
602/* Read a code object from a file and check it for validity */
603
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000605read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000606{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000607 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000608
Tim Petersd9b9ac82001-01-28 00:27:39 +0000609 co = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000610 /* Ugly: rd_object() may return NULL with or without error */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 if (co == NULL || !PyCode_Check(co)) {
612 if (!PyErr_Occurred())
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000613 PyErr_Format(PyExc_ImportError,
614 "Non-code object in %.200s", cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000615 Py_XDECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000616 return NULL;
617 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000618 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000619}
620
621
622/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000623 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000624
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000626load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000627{
628 long magic;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 PyCodeObject *co;
630 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000631
Guido van Rossum79f25d91997-04-29 20:08:16 +0000632 magic = PyMarshal_ReadLongFromFile(fp);
Guido van Rossum96774c12000-05-01 20:19:08 +0000633 if (magic != pyc_magic) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000634 PyErr_Format(PyExc_ImportError,
635 "Bad magic number in %.200s", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000636 return NULL;
637 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000638 (void) PyMarshal_ReadLongFromFile(fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000639 co = read_compiled_module(cpathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000640 if (co == NULL)
641 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000643 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000644 name, cpathname);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000645 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000646 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647
648 return m;
649}
650
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651/* Parse a source file and return the corresponding code object */
652
Guido van Rossum79f25d91997-04-29 20:08:16 +0000653static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000654parse_source_module(char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000656 PyCodeObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000657 node *n;
658
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000659 n = PyParser_SimpleParseFile(fp, pathname, Py_file_input);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000660 if (n == NULL)
661 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 co = PyNode_Compile(n, pathname);
663 PyNode_Free(n);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000664
665 return co;
666}
667
668
Guido van Rossum55a83382000-09-20 20:31:38 +0000669/* Helper to open a bytecode file for writing in exclusive mode */
670
671static FILE *
672open_exclusive(char *filename)
673{
674#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
675 /* Use O_EXCL to avoid a race condition when another process tries to
676 write the same file. When that happens, our open() call fails,
677 which is just fine (since it's only a cache).
678 XXX If the file exists and is writable but the directory is not
679 writable, the file will never be written. Oh well.
680 */
681 int fd;
682 (void) unlink(filename);
Tim Peters42c83af2000-09-29 04:03:10 +0000683 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
684#ifdef O_BINARY
685 |O_BINARY /* necessary for Windows */
686#endif
Tim Peters50d8d372001-02-28 05:34:27 +0000687
Tim Peters42c83af2000-09-29 04:03:10 +0000688 , 0666);
Guido van Rossum55a83382000-09-20 20:31:38 +0000689 if (fd < 0)
690 return NULL;
691 return fdopen(fd, "wb");
692#else
693 /* Best we can do -- on Windows this can't happen anyway */
694 return fopen(filename, "wb");
695#endif
696}
697
698
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000699/* Write a compiled module to a file, placing the time of last
700 modification of its source into the header.
701 Errors are ignored, if a write error occurs an attempt is made to
702 remove the file. */
703
704static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000705write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000706{
707 FILE *fp;
708
Guido van Rossum55a83382000-09-20 20:31:38 +0000709 fp = open_exclusive(cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000710 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000711 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000712 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000713 "# can't create %s\n", cpathname);
714 return;
715 }
Guido van Rossum96774c12000-05-01 20:19:08 +0000716 PyMarshal_WriteLongToFile(pyc_magic, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000717 /* First write a 0 for mtime */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000718 PyMarshal_WriteLongToFile(0L, fp);
719 PyMarshal_WriteObjectToFile((PyObject *)co, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000720 if (ferror(fp)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000721 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000722 PySys_WriteStderr("# can't write %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000723 /* Don't keep partial file */
724 fclose(fp);
725 (void) unlink(cpathname);
726 return;
727 }
728 /* Now write the true mtime */
729 fseek(fp, 4L, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000730 PyMarshal_WriteLongToFile(mtime, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000731 fflush(fp);
732 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000734 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000735#ifdef macintosh
Jack Jansencbf630f2000-07-11 21:59:16 +0000736 PyMac_setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000737#endif
738}
739
740
741/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000742 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
743 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000744
Guido van Rossum79f25d91997-04-29 20:08:16 +0000745static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000746load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000747{
Fred Drake4c82b232000-06-30 16:18:57 +0000748 time_t mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000749 FILE *fpc;
750 char buf[MAXPATHLEN+1];
751 char *cpathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752 PyCodeObject *co;
753 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000754
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000755 mtime = PyOS_GetLastModificationTime(pathname, fp);
Fred Drakec63d3e92001-03-01 06:33:32 +0000756 if (mtime == (time_t)(-1))
Fred Drake4c82b232000-06-30 16:18:57 +0000757 return NULL;
758#if SIZEOF_TIME_T > 4
759 /* Python's .pyc timestamp handling presumes that the timestamp fits
760 in 4 bytes. This will be fine until sometime in the year 2038,
761 when a 4-byte signed time_t will overflow.
762 */
763 if (mtime >> 32) {
764 PyErr_SetString(PyExc_OverflowError,
Fred Drakec63d3e92001-03-01 06:33:32 +0000765 "modification time overflows a 4 byte field");
Fred Drake4c82b232000-06-30 16:18:57 +0000766 return NULL;
767 }
768#endif
Tim Peters36515e22001-11-18 04:06:29 +0000769 cpathname = make_compiled_pathname(pathname, buf,
Jeremy Hylton37832f02001-04-13 17:50:20 +0000770 (size_t)MAXPATHLEN + 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000771 if (cpathname != NULL &&
772 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000773 co = read_compiled_module(cpathname, fpc);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000774 fclose(fpc);
775 if (co == NULL)
776 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000778 PySys_WriteStderr("import %s # precompiled from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000779 name, cpathname);
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000780 pathname = cpathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000781 }
782 else {
783 co = parse_source_module(pathname, fp);
784 if (co == NULL)
785 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000786 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000787 PySys_WriteStderr("import %s # from %s\n",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000788 name, pathname);
789 write_compiled_module(co, cpathname, mtime);
790 }
Guido van Rossumafd3dae1998-08-25 18:44:34 +0000791 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000792 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000793
794 return m;
795}
796
797
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000798/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000799static PyObject *load_module(char *, FILE *, char *, int);
800static struct filedescr *find_module(char *, PyObject *,
801 char *, size_t, FILE **);
802static struct _frozen *find_frozen(char *name);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000803
804/* Load a package and return its module object WITH INCREMENTED
805 REFERENCE COUNT */
806
807static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000808load_package(char *name, char *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000809{
810 PyObject *m, *d, *file, *path;
811 int err;
812 char buf[MAXPATHLEN+1];
813 FILE *fp = NULL;
814 struct filedescr *fdp;
815
816 m = PyImport_AddModule(name);
817 if (m == NULL)
818 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000819 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000820 PySys_WriteStderr("import %s # directory %s\n",
Guido van Rossum17fc85f1997-09-06 18:52:03 +0000821 name, pathname);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000822 d = PyModule_GetDict(m);
823 file = PyString_FromString(pathname);
824 if (file == NULL)
825 return NULL;
826 path = Py_BuildValue("[O]", file);
827 if (path == NULL) {
828 Py_DECREF(file);
829 return NULL;
830 }
831 err = PyDict_SetItemString(d, "__file__", file);
832 if (err == 0)
833 err = PyDict_SetItemString(d, "__path__", path);
834 if (err != 0) {
835 m = NULL;
836 goto cleanup;
837 }
838 buf[0] = '\0';
839 fdp = find_module("__init__", path, buf, sizeof(buf), &fp);
840 if (fdp == NULL) {
841 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
842 PyErr_Clear();
843 }
844 else
845 m = NULL;
846 goto cleanup;
847 }
848 m = load_module(name, fp, buf, fdp->type);
849 if (fp != NULL)
850 fclose(fp);
851 cleanup:
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000852 Py_XDECREF(path);
853 Py_XDECREF(file);
854 return m;
855}
856
857
858/* Helper to test for built-in module */
859
860static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000861is_builtin(char *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000862{
863 int i;
Guido van Rossum771c6c81997-10-31 18:37:24 +0000864 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
865 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
866 if (PyImport_Inittab[i].initfunc == NULL)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000867 return -1;
868 else
869 return 1;
870 }
871 }
872 return 0;
873}
874
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000875
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000876/* Search the path (default sys.path) for a module. Return the
877 corresponding filedescr struct, and (via return arguments) the
878 pathname and an open file. Return NULL if the module is not found. */
879
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000880#ifdef MS_COREDLL
Thomas Woutersb4bd21c2000-07-22 23:38:01 +0000881extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
882 char *, int);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000883#endif
884
Tim Peters50d8d372001-02-28 05:34:27 +0000885static int case_ok(char *, int, int, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000886static int find_init_module(char *); /* Forward */
Guido van Rossum197346f1997-10-31 18:38:52 +0000887
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000888static struct filedescr *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000889find_module(char *realname, PyObject *path, char *buf, size_t buflen,
890 FILE **p_fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000891{
Fred Drake4c82b232000-06-30 16:18:57 +0000892 int i, npath;
893 size_t len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +0000894 struct filedescr *fdp = NULL;
Jack Jansenc88da1f2002-05-28 10:58:19 +0000895 char *filemode;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000896 FILE *fp = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000897#ifndef RISCOS
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000898 struct stat statbuf;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000899#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +0000900 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
901 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
902 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Guido van Rossum0506a431998-08-11 15:07:39 +0000903 char name[MAXPATHLEN+1];
Andrew MacIntyred9400542002-02-26 11:41:34 +0000904#if defined(PYOS_OS2)
905 size_t saved_len;
906 size_t saved_namelen;
907 char *saved_buf = NULL;
908#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000909
Fred Drake4c82b232000-06-30 16:18:57 +0000910 if (strlen(realname) > MAXPATHLEN) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +0000911 PyErr_SetString(PyExc_OverflowError,
912 "module name is too long");
Fred Drake4c82b232000-06-30 16:18:57 +0000913 return NULL;
914 }
Guido van Rossum0506a431998-08-11 15:07:39 +0000915 strcpy(name, realname);
916
917 if (path != NULL && PyString_Check(path)) {
Guido van Rossum8f4d3312001-10-18 18:54:11 +0000918 /* The only type of submodule allowed inside a "frozen"
919 package are other frozen modules or packages. */
Guido van Rossum0506a431998-08-11 15:07:39 +0000920 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
921 PyErr_SetString(PyExc_ImportError,
922 "full frozen module name too long");
923 return NULL;
924 }
925 strcpy(buf, PyString_AsString(path));
926 strcat(buf, ".");
927 strcat(buf, name);
928 strcpy(name, buf);
Jack Jansen550fdae2001-10-30 13:08:39 +0000929#ifdef macintosh
930 /* Freezing on the mac works different, and the modules are
931 ** actually on sys.path. So we don't take the quick exit but
932 ** continue with the normal flow.
933 */
934 path = NULL;
935#else
Guido van Rossum8f4d3312001-10-18 18:54:11 +0000936 if (find_frozen(name) != NULL) {
937 strcpy(buf, name);
938 return &fd_frozen;
939 }
940 PyErr_Format(PyExc_ImportError,
941 "No frozen submodule named %.200s", name);
942 return NULL;
Jack Jansen550fdae2001-10-30 13:08:39 +0000943#endif
Guido van Rossum0506a431998-08-11 15:07:39 +0000944 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000945 if (path == NULL) {
946 if (is_builtin(name)) {
Guido van Rossuma5568d31998-03-05 03:45:08 +0000947 strcpy(buf, name);
948 return &fd_builtin;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000949 }
Greg Ward201baee2001-10-04 14:52:06 +0000950 if ((find_frozen(name)) != NULL) {
Guido van Rossuma5568d31998-03-05 03:45:08 +0000951 strcpy(buf, name);
952 return &fd_frozen;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000953 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000954
Guido van Rossumac279101996-08-22 23:10:58 +0000955#ifdef MS_COREDLL
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000956 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
957 if (fp != NULL) {
958 *p_fp = fp;
959 return fdp;
960 }
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000961#endif
Guido van Rossuma5568d31998-03-05 03:45:08 +0000962 path = PySys_GetObject("path");
963 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000964 if (path == NULL || !PyList_Check(path)) {
965 PyErr_SetString(PyExc_ImportError,
Guido van Rossuma5568d31998-03-05 03:45:08 +0000966 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000967 return NULL;
968 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000969 npath = PyList_Size(path);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000970 namelen = strlen(name);
971 for (i = 0; i < npath; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000972 PyObject *v = PyList_GetItem(path, i);
973 if (!PyString_Check(v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000974 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000975 len = PyString_Size(v);
Guido van Rossumef3d02e1997-07-21 14:54:36 +0000976 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000977 continue; /* Too long */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000978 strcpy(buf, PyString_AsString(v));
Fred Drake4c82b232000-06-30 16:18:57 +0000979 if (strlen(buf) != len)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000980 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000981#ifdef macintosh
Tim Peters50d8d372001-02-28 05:34:27 +0000982 /*
Guido van Rossum741689d1997-08-12 14:53:39 +0000983 ** Speedup: each sys.path item is interned, and
984 ** FindResourceModule remembers which items refer to
985 ** folders (so we don't have to bother trying to look
Tim Peters50d8d372001-02-28 05:34:27 +0000986 ** into them for resources).
Guido van Rossum741689d1997-08-12 14:53:39 +0000987 */
988 PyString_InternInPlace(&PyList_GET_ITEM(path, i));
989 v = PyList_GET_ITEM(path, i);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000990 if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000991 static struct filedescr resfiledescr =
992 {"", "", PY_RESOURCE};
Tim Peters50d8d372001-02-28 05:34:27 +0000993
Jack Jansen9c96a921995-02-15 22:57:06 +0000994 return &resfiledescr;
995 }
Guido van Rossum0f84a341998-08-06 13:36:01 +0000996 if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) {
997 static struct filedescr resfiledescr =
998 {"", "", PY_CODERESOURCE};
Tim Peters50d8d372001-02-28 05:34:27 +0000999
Guido van Rossum0f84a341998-08-06 13:36:01 +00001000 return &resfiledescr;
1001 }
Jack Jansen9c96a921995-02-15 22:57:06 +00001002#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001003 if (len > 0 && buf[len-1] != SEP
1004#ifdef ALTSEP
1005 && buf[len-1] != ALTSEP
1006#endif
1007 )
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001008 buf[len++] = SEP;
Guido van Rossum215c3402000-11-13 17:26:32 +00001009 strcpy(buf+len, name);
1010 len += namelen;
Tim Peters50d8d372001-02-28 05:34:27 +00001011
1012 /* Check for package import (buf holds a directory name,
1013 and there's an __init__ module in that directory */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001014#ifdef HAVE_STAT
Tim Peterscab3f682001-04-29 22:21:25 +00001015 if (stat(buf, &statbuf) == 0 && /* it exists */
1016 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1017 find_init_module(buf) && /* it has __init__.py */
1018 case_ok(buf, len, namelen, name)) /* and case matches */
1019 return &fd_package;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001020#else
1021 /* XXX How are you going to test for directories? */
Guido van Rossum48a680c2001-03-02 06:34:14 +00001022#ifdef RISCOS
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001023 if (isdir(buf) &&
1024 find_init_module(buf) &&
1025 case_ok(buf, len, namelen, name))
1026 return &fd_package;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001027#endif
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001028#endif
Guido van Rossum9a61dc91997-10-08 15:25:08 +00001029#ifdef macintosh
1030 fdp = PyMac_FindModuleExtension(buf, &len, name);
Jack Jansen4df3c522001-03-20 23:09:54 +00001031 if (fdp) {
Guido van Rossum9a61dc91997-10-08 15:25:08 +00001032#else
Andrew MacIntyred9400542002-02-26 11:41:34 +00001033#if defined(PYOS_OS2)
1034 /* take a snapshot of the module spec for restoration
1035 * after the 8 character DLL hackery
1036 */
1037 saved_buf = strdup(buf);
1038 saved_len = len;
1039 saved_namelen = namelen;
1040#endif /* PYOS_OS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001041 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Andrew MacIntyred9400542002-02-26 11:41:34 +00001042#if defined(PYOS_OS2)
1043 /* OS/2 limits DLLs to 8 character names (w/o extension)
1044 * so if the name is longer than that and its a
1045 * dynamically loaded module we're going to try,
1046 * truncate the name before trying
1047 */
1048 if (strlen(realname) > 8) {
1049 /* is this an attempt to load a C extension? */
1050 const struct filedescr *scan = _PyImport_DynLoadFiletab;
1051 while (scan->suffix != NULL) {
1052 if (strcmp(scan->suffix, fdp->suffix) == 0)
1053 break;
1054 else
1055 scan++;
1056 }
1057 if (scan->suffix != NULL) {
1058 /* yes, so truncate the name */
1059 namelen = 8;
1060 len -= strlen(realname) - namelen;
1061 buf[len] = '\0';
1062 }
1063 }
1064#endif /* PYOS_OS2 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001065 strcpy(buf+len, fdp->suffix);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001066 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001067 PySys_WriteStderr("# trying %s\n", buf);
Jack Jansen4df3c522001-03-20 23:09:54 +00001068#endif /* !macintosh */
Jack Jansenc88da1f2002-05-28 10:58:19 +00001069 filemode = fdp->mode;
1070 if (filemode[0] == 'U') filemode = "r" PY_STDIOTEXTMODE;
1071 fp = fopen(buf, filemode);
Tim Peters50d8d372001-02-28 05:34:27 +00001072 if (fp != NULL) {
1073 if (case_ok(buf, len, namelen, name))
1074 break;
1075 else { /* continue search */
1076 fclose(fp);
1077 fp = NULL;
Barry Warsaw914a0b12001-02-02 19:12:16 +00001078 }
Barry Warsaw914a0b12001-02-02 19:12:16 +00001079 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001080#if defined(PYOS_OS2)
1081 /* restore the saved snapshot */
1082 strcpy(buf, saved_buf);
1083 len = saved_len;
1084 namelen = saved_namelen;
1085#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001086 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001087#if defined(PYOS_OS2)
1088 /* don't need/want the module name snapshot anymore */
1089 if (saved_buf)
1090 {
1091 free(saved_buf);
1092 saved_buf = NULL;
1093 }
1094#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001095 if (fp != NULL)
1096 break;
1097 }
1098 if (fp == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001099 PyErr_Format(PyExc_ImportError,
1100 "No module named %.200s", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001101 return NULL;
1102 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001103 *p_fp = fp;
1104 return fdp;
1105}
1106
Tim Petersd1e87a82001-03-01 18:12:00 +00001107/* case_ok(char* buf, int len, int namelen, char* name)
1108 * The arguments here are tricky, best shown by example:
1109 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1110 * ^ ^ ^ ^
1111 * |--------------------- buf ---------------------|
1112 * |------------------- len ------------------|
1113 * |------ name -------|
1114 * |----- namelen -----|
1115 * buf is the full path, but len only counts up to (& exclusive of) the
1116 * extension. name is the module name, also exclusive of extension.
1117 *
1118 * We've already done a successful stat() or fopen() on buf, so know that
1119 * there's some match, possibly case-insensitive.
1120 *
Tim Peters50d8d372001-02-28 05:34:27 +00001121 * case_ok() is to return 1 if there's a case-sensitive match for
1122 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
1123 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00001124 *
Tim Peters50d8d372001-02-28 05:34:27 +00001125 * case_ok() is used to implement case-sensitive import semantics even
1126 * on platforms with case-insensitive filesystems. It's trivial to implement
1127 * for case-sensitive filesystems. It's pretty much a cross-platform
1128 * nightmare for systems with case-insensitive filesystems.
1129 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00001130
Tim Peters50d8d372001-02-28 05:34:27 +00001131/* First we may need a pile of platform-specific header files; the sequence
1132 * of #if's here should match the sequence in the body of case_ok().
1133 */
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001134#if defined(MS_WIN32) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001135#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001136#ifdef __CYGWIN__
1137#include <sys/cygwin.h>
1138#endif
1139
Tim Peters50d8d372001-02-28 05:34:27 +00001140#elif defined(DJGPP)
1141#include <dir.h>
1142
1143#elif defined(macintosh)
1144#include <TextUtils.h>
Tim Peters50d8d372001-02-28 05:34:27 +00001145
Tim Petersd1e87a82001-03-01 18:12:00 +00001146#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001147#include <sys/types.h>
1148#include <dirent.h>
1149
Andrew MacIntyred9400542002-02-26 11:41:34 +00001150#elif defined(PYOS_OS2)
1151#define INCL_DOS
1152#define INCL_DOSERRORS
1153#define INCL_NOPMAPI
1154#include <os2.h>
1155
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001156#elif defined(RISCOS)
1157#include "oslib/osfscontrol.h"
Tim Peters50d8d372001-02-28 05:34:27 +00001158#endif
1159
Guido van Rossum0980bd91998-02-13 17:18:36 +00001160static int
Tim Peters50d8d372001-02-28 05:34:27 +00001161case_ok(char *buf, int len, int namelen, char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001162{
Tim Peters50d8d372001-02-28 05:34:27 +00001163/* Pick a platform-specific implementation; the sequence of #if's here should
1164 * match the sequence just above.
1165 */
1166
1167/* MS_WIN32 || __CYGWIN__ */
1168#if defined(MS_WIN32) || defined(__CYGWIN__)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001169 WIN32_FIND_DATA data;
1170 HANDLE h;
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001171#ifdef __CYGWIN__
1172 char tempbuf[MAX_PATH];
1173#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001174
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001175 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001176 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001177
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001178#ifdef __CYGWIN__
1179 cygwin32_conv_to_win32_path(buf, tempbuf);
1180 h = FindFirstFile(tempbuf, &data);
1181#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001182 h = FindFirstFile(buf, &data);
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00001183#endif
Guido van Rossum0980bd91998-02-13 17:18:36 +00001184 if (h == INVALID_HANDLE_VALUE) {
1185 PyErr_Format(PyExc_NameError,
1186 "Can't find file for module %.100s\n(filename %.300s)",
1187 name, buf);
1188 return 0;
1189 }
1190 FindClose(h);
Tim Peters50d8d372001-02-28 05:34:27 +00001191 return strncmp(data.cFileName, name, namelen) == 0;
1192
1193/* DJGPP */
1194#elif defined(DJGPP)
1195 struct ffblk ffblk;
1196 int done;
1197
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001198 if (Py_GETENV("PYTHONCASEOK") != NULL)
Guido van Rossum323bf5e1998-06-24 03:54:06 +00001199 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00001200
1201 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
1202 if (done) {
Guido van Rossum0980bd91998-02-13 17:18:36 +00001203 PyErr_Format(PyExc_NameError,
Tim Peters50d8d372001-02-28 05:34:27 +00001204 "Can't find file for module %.100s\n(filename %.300s)",
Guido van Rossum0980bd91998-02-13 17:18:36 +00001205 name, buf);
1206 return 0;
1207 }
Tim Peters50d8d372001-02-28 05:34:27 +00001208 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001209
Tim Peters50d8d372001-02-28 05:34:27 +00001210/* macintosh */
1211#elif defined(macintosh)
Guido van Rossum0980bd91998-02-13 17:18:36 +00001212 FSSpec fss;
1213 OSErr err;
Tim Peters50d8d372001-02-28 05:34:27 +00001214
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001215 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Peters50d8d372001-02-28 05:34:27 +00001216 return 1;
1217
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001218 err = FSMakeFSSpec(0, 0, Pstring(buf), &fss);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001219 if (err) {
1220 PyErr_Format(PyExc_NameError,
Guido van Rossuma0f0a331998-09-14 13:40:53 +00001221 "Can't find file for module %.100s\n(filename %.300s)",
1222 name, buf);
Guido van Rossum0980bd91998-02-13 17:18:36 +00001223 return 0;
1224 }
Tim Peters50d8d372001-02-28 05:34:27 +00001225 return fss.name[0] >= namelen &&
1226 strncmp(name, (char *)fss.name+1, namelen) == 0;
1227
Tim Peters677898a2001-03-02 03:28:03 +00001228/* new-fangled macintosh (macosx) */
Tim Petersd1e87a82001-03-01 18:12:00 +00001229#elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00001230 DIR *dirp;
1231 struct dirent *dp;
Tim Petersd1e87a82001-03-01 18:12:00 +00001232 char dirname[MAXPATHLEN + 1];
1233 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00001234
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +00001235 if (Py_GETENV("PYTHONCASEOK") != NULL)
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001236 return 1;
1237
Tim Petersd1e87a82001-03-01 18:12:00 +00001238 /* Copy the dir component into dirname; substitute "." if empty */
1239 if (dirlen <= 0) {
1240 dirname[0] = '.';
1241 dirname[1] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001242 }
1243 else {
Tim Petersd1e87a82001-03-01 18:12:00 +00001244 assert(dirlen <= MAXPATHLEN);
1245 memcpy(dirname, buf, dirlen);
1246 dirname[dirlen] = '\0';
Tim Peters430f5d42001-03-01 01:30:56 +00001247 }
1248 /* Open the directory and search the entries for an exact match. */
Tim Petersd1e87a82001-03-01 18:12:00 +00001249 dirp = opendir(dirname);
Tim Peters430f5d42001-03-01 01:30:56 +00001250 if (dirp) {
Tim Peters677898a2001-03-02 03:28:03 +00001251 char *nameWithExt = buf + len - namelen;
Tim Peters430f5d42001-03-01 01:30:56 +00001252 while ((dp = readdir(dirp)) != NULL) {
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001253 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00001254#ifdef _DIRENT_HAVE_D_NAMELEN
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001255 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00001256#else
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001257 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00001258#endif
Tim Petersd1e87a82001-03-01 18:12:00 +00001259 if (thislen >= namelen &&
Tim Peters677898a2001-03-02 03:28:03 +00001260 strcmp(dp->d_name, nameWithExt) == 0) {
Tim Peters430f5d42001-03-01 01:30:56 +00001261 (void)closedir(dirp);
1262 return 1; /* Found */
1263 }
1264 }
Tim Petersdbe6ebb2001-03-01 08:47:29 +00001265 (void)closedir(dirp);
Tim Peters430f5d42001-03-01 01:30:56 +00001266 }
Tim Peters430f5d42001-03-01 01:30:56 +00001267 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00001268
Guido van Rossume2ae77b2001-10-24 20:42:55 +00001269/* RISC OS */
1270#elif defined(RISCOS)
1271 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
1272 char buf2[MAXPATHLEN+2];
1273 char *nameWithExt = buf+len-namelen;
1274 int canonlen;
1275 os_error *e;
1276
1277 if (Py_GETENV("PYTHONCASEOK") != NULL)
1278 return 1;
1279
1280 /* workaround:
1281 append wildcard, otherwise case of filename wouldn't be touched */
1282 strcpy(buf2, buf);
1283 strcat(buf2, "*");
1284
1285 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
1286 canonlen = MAXPATHLEN+1-canonlen;
1287 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
1288 return 0;
1289 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
1290 return 1; /* match */
1291
1292 return 0;
1293
Andrew MacIntyred9400542002-02-26 11:41:34 +00001294/* OS/2 */
1295#elif defined(PYOS_OS2)
1296 HDIR hdir = 1;
1297 ULONG srchcnt = 1;
1298 FILEFINDBUF3 ffbuf;
1299 APIRET rc;
1300
1301 if (getenv("PYTHONCASEOK") != NULL)
1302 return 1;
1303
1304 rc = DosFindFirst(buf,
1305 &hdir,
1306 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
1307 &ffbuf, sizeof(ffbuf),
1308 &srchcnt,
1309 FIL_STANDARD);
1310 if (rc != NO_ERROR)
1311 return 0;
1312 return strncmp(ffbuf.achName, name, namelen) == 0;
1313
Tim Peters50d8d372001-02-28 05:34:27 +00001314/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
1315#else
Guido van Rossum0980bd91998-02-13 17:18:36 +00001316 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00001317
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001318#endif
Tim Peters50d8d372001-02-28 05:34:27 +00001319}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00001320
Guido van Rossum0980bd91998-02-13 17:18:36 +00001321
Guido van Rossum197346f1997-10-31 18:38:52 +00001322#ifdef HAVE_STAT
1323/* Helper to look for __init__.py or __init__.py[co] in potential package */
1324static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001325find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00001326{
Tim Peters0f9431f2001-07-05 03:47:53 +00001327 const size_t save_len = strlen(buf);
Fred Drake4c82b232000-06-30 16:18:57 +00001328 size_t i = save_len;
Tim Peters0f9431f2001-07-05 03:47:53 +00001329 char *pname; /* pointer to start of __init__ */
Guido van Rossum197346f1997-10-31 18:38:52 +00001330 struct stat statbuf;
1331
Tim Peters0f9431f2001-07-05 03:47:53 +00001332/* For calling case_ok(buf, len, namelen, name):
1333 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
1334 * ^ ^ ^ ^
1335 * |--------------------- buf ---------------------|
1336 * |------------------- len ------------------|
1337 * |------ name -------|
1338 * |----- namelen -----|
1339 */
Guido van Rossum197346f1997-10-31 18:38:52 +00001340 if (save_len + 13 >= MAXPATHLEN)
1341 return 0;
1342 buf[i++] = SEP;
Tim Peters0f9431f2001-07-05 03:47:53 +00001343 pname = buf + i;
1344 strcpy(pname, "__init__.py");
Guido van Rossum197346f1997-10-31 18:38:52 +00001345 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001346 if (case_ok(buf,
1347 save_len + 9, /* len("/__init__") */
1348 8, /* len("__init__") */
1349 pname)) {
1350 buf[save_len] = '\0';
1351 return 1;
1352 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001353 }
Tim Peters0f9431f2001-07-05 03:47:53 +00001354 i += strlen(pname);
1355 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum197346f1997-10-31 18:38:52 +00001356 if (stat(buf, &statbuf) == 0) {
Tim Peters0f9431f2001-07-05 03:47:53 +00001357 if (case_ok(buf,
1358 save_len + 9, /* len("/__init__") */
1359 8, /* len("__init__") */
1360 pname)) {
1361 buf[save_len] = '\0';
1362 return 1;
1363 }
Guido van Rossum197346f1997-10-31 18:38:52 +00001364 }
1365 buf[save_len] = '\0';
1366 return 0;
1367}
Guido van Rossum48a680c2001-03-02 06:34:14 +00001368
1369#else
1370
1371#ifdef RISCOS
1372static int
1373find_init_module(buf)
1374 char *buf;
1375{
1376 int save_len = strlen(buf);
1377 int i = save_len;
1378
1379 if (save_len + 13 >= MAXPATHLEN)
1380 return 0;
1381 buf[i++] = SEP;
1382 strcpy(buf+i, "__init__/py");
1383 if (isfile(buf)) {
1384 buf[save_len] = '\0';
1385 return 1;
1386 }
1387
1388 if (Py_OptimizeFlag)
1389 strcpy(buf+i, "o");
1390 else
1391 strcpy(buf+i, "c");
1392 if (isfile(buf)) {
1393 buf[save_len] = '\0';
1394 return 1;
1395 }
1396 buf[save_len] = '\0';
1397 return 0;
1398}
1399#endif /*RISCOS*/
1400
Guido van Rossum197346f1997-10-31 18:38:52 +00001401#endif /* HAVE_STAT */
1402
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001403
Tim Petersdbd9ba62000-07-09 03:09:57 +00001404static int init_builtin(char *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001405
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001406/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001407 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001408
Guido van Rossum79f25d91997-04-29 20:08:16 +00001409static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001410load_module(char *name, FILE *fp, char *buf, int type)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001411{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001412 PyObject *modules;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001413 PyObject *m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001414 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001415
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001416 /* First check that there's an open file (if we need one) */
1417 switch (type) {
1418 case PY_SOURCE:
1419 case PY_COMPILED:
1420 if (fp == NULL) {
1421 PyErr_Format(PyExc_ValueError,
1422 "file object required for import (type code %d)",
1423 type);
1424 return NULL;
1425 }
1426 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001427
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001428 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001429
1430 case PY_SOURCE:
1431 m = load_source_module(name, buf, fp);
1432 break;
1433
1434 case PY_COMPILED:
1435 m = load_compiled_module(name, buf, fp);
1436 break;
1437
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001438#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001439 case C_EXTENSION:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001440 m = _PyImport_LoadDynamicModule(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001441 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001442#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001443
Jack Jansen9c96a921995-02-15 22:57:06 +00001444#ifdef macintosh
1445 case PY_RESOURCE:
1446 m = PyMac_LoadResourceModule(name, buf);
1447 break;
Guido van Rossum0f84a341998-08-06 13:36:01 +00001448 case PY_CODERESOURCE:
1449 m = PyMac_LoadCodeResourceModule(name, buf);
1450 break;
Jack Jansen9c96a921995-02-15 22:57:06 +00001451#endif
1452
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001453 case PKG_DIRECTORY:
1454 m = load_package(name, buf);
1455 break;
1456
1457 case C_BUILTIN:
1458 case PY_FROZEN:
Guido van Rossuma5568d31998-03-05 03:45:08 +00001459 if (buf != NULL && buf[0] != '\0')
1460 name = buf;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001461 if (type == C_BUILTIN)
1462 err = init_builtin(name);
1463 else
1464 err = PyImport_ImportFrozenModule(name);
1465 if (err < 0)
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001466 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001467 if (err == 0) {
1468 PyErr_Format(PyExc_ImportError,
1469 "Purported %s module %.200s not found",
1470 type == C_BUILTIN ?
1471 "builtin" : "frozen",
1472 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001473 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001474 }
1475 modules = PyImport_GetModuleDict();
1476 m = PyDict_GetItemString(modules, name);
1477 if (m == NULL) {
1478 PyErr_Format(
1479 PyExc_ImportError,
1480 "%s module %.200s not properly initialized",
1481 type == C_BUILTIN ?
1482 "builtin" : "frozen",
1483 name);
Guido van Rossuma86f77d1997-09-09 18:53:47 +00001484 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001485 }
1486 Py_INCREF(m);
1487 break;
1488
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001489 default:
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001490 PyErr_Format(PyExc_ImportError,
1491 "Don't know how to import %.200s (type code %d)",
1492 name, type);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001493 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001494
1495 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001496
1497 return m;
1498}
1499
1500
1501/* Initialize a built-in module.
1502 Return 1 for succes, 0 if the module is not found, and -1 with
1503 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001504
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001505static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001506init_builtin(char *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001507{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001508 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001509
Greg Ward201baee2001-10-04 14:52:06 +00001510 if (_PyImport_FindExtension(name, name) != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001511 return 1;
1512
Guido van Rossum771c6c81997-10-31 18:37:24 +00001513 for (p = PyImport_Inittab; p->name != NULL; p++) {
Guido van Rossum25ce5661997-08-02 03:10:38 +00001514 if (strcmp(name, p->name) == 0) {
1515 if (p->initfunc == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001516 PyErr_Format(PyExc_ImportError,
1517 "Cannot re-init internal module %.200s",
1518 name);
Guido van Rossum74e6a111994-08-29 12:54:38 +00001519 return -1;
1520 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001521 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001522 PySys_WriteStderr("import %s # builtin\n", name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001523 (*p->initfunc)();
Guido van Rossum79f25d91997-04-29 20:08:16 +00001524 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001525 return -1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001526 if (_PyImport_FixupExtension(name, name) == NULL)
1527 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001528 return 1;
1529 }
1530 }
1531 return 0;
1532}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001533
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001534
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001535/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001536
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001537static struct _frozen *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001538find_frozen(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001539{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001540 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001541
Guido van Rossum79f25d91997-04-29 20:08:16 +00001542 for (p = PyImport_FrozenModules; ; p++) {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001543 if (p->name == NULL)
1544 return NULL;
1545 if (strcmp(p->name, name) == 0)
1546 break;
1547 }
1548 return p;
1549}
1550
Guido van Rossum79f25d91997-04-29 20:08:16 +00001551static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001552get_frozen_object(char *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001553{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001554 struct _frozen *p = find_frozen(name);
Guido van Rossuma5568d31998-03-05 03:45:08 +00001555 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001556
1557 if (p == NULL) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001558 PyErr_Format(PyExc_ImportError,
1559 "No such frozen object named %.200s",
1560 name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001561 return NULL;
1562 }
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001563 if (p->code == NULL) {
1564 PyErr_Format(PyExc_ImportError,
1565 "Excluded frozen object named %.200s",
1566 name);
1567 return NULL;
1568 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001569 size = p->size;
1570 if (size < 0)
1571 size = -size;
1572 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001573}
1574
1575/* Initialize a frozen module.
1576 Return 1 for succes, 0 if the module is not found, and -1 with
1577 an exception set if the initialization failed.
1578 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001579
1580int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001581PyImport_ImportFrozenModule(char *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001582{
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001583 struct _frozen *p = find_frozen(name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001584 PyObject *co;
1585 PyObject *m;
Guido van Rossuma5568d31998-03-05 03:45:08 +00001586 int ispackage;
1587 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001588
1589 if (p == NULL)
1590 return 0;
Guido van Rossum8f4d3312001-10-18 18:54:11 +00001591 if (p->code == NULL) {
1592 PyErr_Format(PyExc_ImportError,
1593 "Excluded frozen object named %.200s",
1594 name);
1595 return -1;
1596 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001597 size = p->size;
1598 ispackage = (size < 0);
1599 if (ispackage)
1600 size = -size;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001601 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +00001602 PySys_WriteStderr("import %s # frozen%s\n",
Guido van Rossuma5568d31998-03-05 03:45:08 +00001603 name, ispackage ? " package" : "");
1604 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001605 if (co == NULL)
1606 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001607 if (!PyCode_Check(co)) {
1608 Py_DECREF(co);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001609 PyErr_Format(PyExc_TypeError,
1610 "frozen object %.200s is not a code object",
1611 name);
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001612 return -1;
1613 }
Guido van Rossuma5568d31998-03-05 03:45:08 +00001614 if (ispackage) {
1615 /* Set __path__ to the package name */
1616 PyObject *d, *s;
1617 int err;
1618 m = PyImport_AddModule(name);
1619 if (m == NULL)
1620 return -1;
1621 d = PyModule_GetDict(m);
1622 s = PyString_InternFromString(name);
1623 if (s == NULL)
1624 return -1;
1625 err = PyDict_SetItemString(d, "__path__", s);
1626 Py_DECREF(s);
1627 if (err != 0)
1628 return err;
1629 }
Guido van Rossume32bf6e1998-02-11 05:53:02 +00001630 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
Guido van Rossum79f25d91997-04-29 20:08:16 +00001631 Py_DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001632 if (m == NULL)
1633 return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001634 Py_DECREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001635 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001636}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001637
1638
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001639/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001640 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001641
Guido van Rossum79f25d91997-04-29 20:08:16 +00001642PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001643PyImport_ImportModule(char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001644{
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001645 PyObject *pname;
1646 PyObject *result;
1647
1648 pname = PyString_FromString(name);
1649 result = PyImport_Import(pname);
1650 Py_DECREF(pname);
1651 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001652}
1653
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001654/* Forward declarations for helper routines */
Tim Petersdbd9ba62000-07-09 03:09:57 +00001655static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen);
1656static PyObject *load_next(PyObject *mod, PyObject *altmod,
1657 char **p_name, char *buf, int *p_buflen);
1658static int mark_miss(char *name);
1659static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
1660 char *buf, int buflen, int recursive);
1661static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001662
1663/* The Magnum Opus of dotted-name import :-) */
1664
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001665static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001666import_module_ex(char *name, PyObject *globals, PyObject *locals,
1667 PyObject *fromlist)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001668{
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001669 char buf[MAXPATHLEN+1];
1670 int buflen = 0;
1671 PyObject *parent, *head, *next, *tail;
1672
1673 parent = get_parent(globals, buf, &buflen);
1674 if (parent == NULL)
1675 return NULL;
1676
1677 head = load_next(parent, Py_None, &name, buf, &buflen);
1678 if (head == NULL)
1679 return NULL;
1680
1681 tail = head;
1682 Py_INCREF(tail);
1683 while (name) {
1684 next = load_next(tail, tail, &name, buf, &buflen);
1685 Py_DECREF(tail);
1686 if (next == NULL) {
1687 Py_DECREF(head);
1688 return NULL;
1689 }
1690 tail = next;
1691 }
1692
1693 if (fromlist != NULL) {
1694 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
1695 fromlist = NULL;
1696 }
1697
1698 if (fromlist == NULL) {
1699 Py_DECREF(tail);
1700 return head;
1701 }
1702
1703 Py_DECREF(head);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001704 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001705 Py_DECREF(tail);
1706 return NULL;
1707 }
1708
1709 return tail;
1710}
1711
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001712PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001713PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals,
1714 PyObject *fromlist)
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001715{
1716 PyObject *result;
1717 lock_import();
Guido van Rossumd65911b1998-03-03 22:33:27 +00001718 result = import_module_ex(name, globals, locals, fromlist);
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001719 unlock_import();
1720 return result;
1721}
1722
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001723static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001724get_parent(PyObject *globals, char *buf, int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001725{
1726 static PyObject *namestr = NULL;
1727 static PyObject *pathstr = NULL;
1728 PyObject *modname, *modpath, *modules, *parent;
1729
1730 if (globals == NULL || !PyDict_Check(globals))
1731 return Py_None;
1732
1733 if (namestr == NULL) {
1734 namestr = PyString_InternFromString("__name__");
1735 if (namestr == NULL)
1736 return NULL;
1737 }
1738 if (pathstr == NULL) {
1739 pathstr = PyString_InternFromString("__path__");
1740 if (pathstr == NULL)
1741 return NULL;
1742 }
1743
1744 *buf = '\0';
1745 *p_buflen = 0;
1746 modname = PyDict_GetItem(globals, namestr);
1747 if (modname == NULL || !PyString_Check(modname))
1748 return Py_None;
1749
1750 modpath = PyDict_GetItem(globals, pathstr);
1751 if (modpath != NULL) {
1752 int len = PyString_GET_SIZE(modname);
1753 if (len > MAXPATHLEN) {
1754 PyErr_SetString(PyExc_ValueError,
1755 "Module name too long");
1756 return NULL;
1757 }
1758 strcpy(buf, PyString_AS_STRING(modname));
1759 *p_buflen = len;
1760 }
1761 else {
1762 char *start = PyString_AS_STRING(modname);
1763 char *lastdot = strrchr(start, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001764 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001765 if (lastdot == NULL)
1766 return Py_None;
1767 len = lastdot - start;
1768 if (len >= MAXPATHLEN) {
1769 PyErr_SetString(PyExc_ValueError,
1770 "Module name too long");
1771 return NULL;
1772 }
1773 strncpy(buf, start, len);
1774 buf[len] = '\0';
1775 *p_buflen = len;
1776 }
1777
1778 modules = PyImport_GetModuleDict();
1779 parent = PyDict_GetItemString(modules, buf);
1780 if (parent == NULL)
1781 parent = Py_None;
1782 return parent;
1783 /* We expect, but can't guarantee, if parent != None, that:
1784 - parent.__name__ == buf
1785 - parent.__dict__ is globals
1786 If this is violated... Who cares? */
1787}
1788
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001789/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001790static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001791load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
1792 int *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001793{
1794 char *name = *p_name;
1795 char *dot = strchr(name, '.');
Fred Drake4c82b232000-06-30 16:18:57 +00001796 size_t len;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001797 char *p;
1798 PyObject *result;
1799
1800 if (dot == NULL) {
1801 *p_name = NULL;
1802 len = strlen(name);
1803 }
1804 else {
1805 *p_name = dot+1;
1806 len = dot-name;
1807 }
Guido van Rossum111c20b1998-04-11 17:38:22 +00001808 if (len == 0) {
1809 PyErr_SetString(PyExc_ValueError,
1810 "Empty module name");
1811 return NULL;
1812 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001813
1814 p = buf + *p_buflen;
1815 if (p != buf)
1816 *p++ = '.';
1817 if (p+len-buf >= MAXPATHLEN) {
1818 PyErr_SetString(PyExc_ValueError,
1819 "Module name too long");
1820 return NULL;
1821 }
1822 strncpy(p, name, len);
1823 p[len] = '\0';
1824 *p_buflen = p+len-buf;
1825
1826 result = import_submodule(mod, p, buf);
1827 if (result == Py_None && altmod != mod) {
1828 Py_DECREF(result);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001829 /* Here, altmod must be None and mod must not be None */
Guido van Rossum0c819451997-09-07 06:16:57 +00001830 result = import_submodule(altmod, p, p);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001831 if (result != NULL && result != Py_None) {
1832 if (mark_miss(buf) != 0) {
1833 Py_DECREF(result);
1834 return NULL;
1835 }
1836 strncpy(buf, name, len);
1837 buf[len] = '\0';
1838 *p_buflen = len;
1839 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001840 }
1841 if (result == NULL)
1842 return NULL;
1843
1844 if (result == Py_None) {
1845 Py_DECREF(result);
1846 PyErr_Format(PyExc_ImportError,
1847 "No module named %.200s", name);
1848 return NULL;
1849 }
1850
1851 return result;
1852}
1853
1854static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001855mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00001856{
1857 PyObject *modules = PyImport_GetModuleDict();
1858 return PyDict_SetItemString(modules, name, Py_None);
1859}
1860
1861static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001862ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen,
1863 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001864{
1865 int i;
1866
1867 if (!PyObject_HasAttrString(mod, "__path__"))
1868 return 1;
1869
1870 for (i = 0; ; i++) {
1871 PyObject *item = PySequence_GetItem(fromlist, i);
1872 int hasit;
1873 if (item == NULL) {
1874 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
1875 PyErr_Clear();
1876 return 1;
1877 }
1878 return 0;
1879 }
1880 if (!PyString_Check(item)) {
1881 PyErr_SetString(PyExc_TypeError,
1882 "Item in ``from list'' not a string");
1883 Py_DECREF(item);
1884 return 0;
1885 }
1886 if (PyString_AS_STRING(item)[0] == '*') {
Guido van Rossum9905ef91997-09-08 16:07:11 +00001887 PyObject *all;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001888 Py_DECREF(item);
Guido van Rossum9905ef91997-09-08 16:07:11 +00001889 /* See if the package defines __all__ */
1890 if (recursive)
1891 continue; /* Avoid endless recursion */
1892 all = PyObject_GetAttrString(mod, "__all__");
1893 if (all == NULL)
1894 PyErr_Clear();
1895 else {
1896 if (!ensure_fromlist(mod, all, buf, buflen, 1))
1897 return 0;
1898 Py_DECREF(all);
1899 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001900 continue;
1901 }
1902 hasit = PyObject_HasAttr(mod, item);
1903 if (!hasit) {
1904 char *subname = PyString_AS_STRING(item);
1905 PyObject *submod;
1906 char *p;
1907 if (buflen + strlen(subname) >= MAXPATHLEN) {
1908 PyErr_SetString(PyExc_ValueError,
1909 "Module name too long");
1910 Py_DECREF(item);
1911 return 0;
1912 }
1913 p = buf + buflen;
1914 *p++ = '.';
1915 strcpy(p, subname);
1916 submod = import_submodule(mod, subname, buf);
1917 Py_XDECREF(submod);
1918 if (submod == NULL) {
1919 Py_DECREF(item);
1920 return 0;
1921 }
1922 }
1923 Py_DECREF(item);
1924 }
1925
Guido van Rossuma7f2e811997-10-03 15:33:32 +00001926 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001927}
1928
1929static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001930import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001931{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001932 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossumf48f11c2001-07-23 13:27:49 +00001933 PyObject *m, *res = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00001934
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001935 /* Require:
1936 if mod == None: subname == fullname
1937 else: mod.__name__ + "." + subname == fullname
1938 */
1939
Tim Peters50d8d372001-02-28 05:34:27 +00001940 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001941 Py_INCREF(m);
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001942 }
1943 else {
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001944 PyObject *path;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001945 char buf[MAXPATHLEN+1];
1946 struct filedescr *fdp;
1947 FILE *fp = NULL;
1948
Guido van Rossum9c0afe51998-05-19 15:09:05 +00001949 if (mod == Py_None)
1950 path = NULL;
1951 else {
1952 path = PyObject_GetAttrString(mod, "__path__");
1953 if (path == NULL) {
1954 PyErr_Clear();
1955 Py_INCREF(Py_None);
1956 return Py_None;
1957 }
1958 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001959
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001960 buf[0] = '\0';
Guido van Rossumb68cd421998-07-01 17:36:26 +00001961 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1962 Py_XDECREF(path);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001963 if (fdp == NULL) {
1964 if (!PyErr_ExceptionMatches(PyExc_ImportError))
1965 return NULL;
1966 PyErr_Clear();
1967 Py_INCREF(Py_None);
1968 return Py_None;
1969 }
1970 m = load_module(fullname, fp, buf, fdp->type);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001971 if (fp)
1972 fclose(fp);
Guido van Rossumf48f11c2001-07-23 13:27:49 +00001973 if (mod != Py_None) {
1974 /* Irrespective of the success of this load, make a
1975 reference to it in the parent package module.
1976 A copy gets saved in the modules dictionary
1977 under the full name, so get a reference from
1978 there, if need be. (The exception is when
1979 the load failed with a SyntaxError -- then
1980 there's no trace in sys.modules. In that case,
1981 of course, do nothing extra.) */
1982 res = m;
1983 if (res == NULL)
1984 res = PyDict_GetItemString(modules, fullname);
1985 if (res != NULL &&
1986 PyObject_SetAttrString(mod, subname, res) < 0) {
1987 Py_XDECREF(m);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001988 m = NULL;
1989 }
1990 }
Guido van Rossum74e6a111994-08-29 12:54:38 +00001991 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001992
1993 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00001994}
1995
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001996
1997/* Re-import a module of any kind and return its module object, WITH
1998 INCREMENTED REFERENCE COUNT */
1999
Guido van Rossum79f25d91997-04-29 20:08:16 +00002000PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002001PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002002{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002003 PyObject *modules = PyImport_GetModuleDict();
Guido van Rossum222ef561997-09-06 19:41:09 +00002004 PyObject *path = NULL;
2005 char *name, *subname;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002006 char buf[MAXPATHLEN+1];
2007 struct filedescr *fdp;
2008 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002009
Guido van Rossum79f25d91997-04-29 20:08:16 +00002010 if (m == NULL || !PyModule_Check(m)) {
2011 PyErr_SetString(PyExc_TypeError,
2012 "reload() argument must be module");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002013 return NULL;
2014 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002015 name = PyModule_GetName(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002016 if (name == NULL)
2017 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002018 if (m != PyDict_GetItemString(modules, name)) {
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002019 PyErr_Format(PyExc_ImportError,
2020 "reload(): module %.200s not in sys.modules",
2021 name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002022 return NULL;
2023 }
Guido van Rossum222ef561997-09-06 19:41:09 +00002024 subname = strrchr(name, '.');
2025 if (subname == NULL)
2026 subname = name;
2027 else {
2028 PyObject *parentname, *parent;
2029 parentname = PyString_FromStringAndSize(name, (subname-name));
2030 if (parentname == NULL)
2031 return NULL;
2032 parent = PyDict_GetItem(modules, parentname);
Barry Warsaw38793331999-01-27 17:54:20 +00002033 Py_DECREF(parentname);
Guido van Rossum222ef561997-09-06 19:41:09 +00002034 if (parent == NULL) {
2035 PyErr_Format(PyExc_ImportError,
2036 "reload(): parent %.200s not in sys.modules",
2037 name);
2038 return NULL;
2039 }
2040 subname++;
2041 path = PyObject_GetAttrString(parent, "__path__");
2042 if (path == NULL)
2043 PyErr_Clear();
2044 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002045 buf[0] = '\0';
Guido van Rossum222ef561997-09-06 19:41:09 +00002046 fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
2047 Py_XDECREF(path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002048 if (fdp == NULL)
2049 return NULL;
2050 m = load_module(name, fp, buf, fdp->type);
2051 if (fp)
2052 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002053 return m;
2054}
2055
2056
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002057/* Higher-level import emulator which emulates the "import" statement
2058 more accurately -- it invokes the __import__() function from the
2059 builtins of the current globals. This means that the import is
2060 done using whatever import hooks are installed in the current
Guido van Rossum6058eb41998-12-21 19:51:00 +00002061 environment, e.g. by "rexec".
2062 A dummy list ["__doc__"] is passed as the 4th argument so that
2063 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2064 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002065
2066PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002067PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002068{
2069 static PyObject *silly_list = NULL;
2070 static PyObject *builtins_str = NULL;
2071 static PyObject *import_str = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002072 PyObject *globals = NULL;
2073 PyObject *import = NULL;
2074 PyObject *builtins = NULL;
2075 PyObject *r = NULL;
2076
2077 /* Initialize constant string objects */
2078 if (silly_list == NULL) {
2079 import_str = PyString_InternFromString("__import__");
2080 if (import_str == NULL)
2081 return NULL;
2082 builtins_str = PyString_InternFromString("__builtins__");
2083 if (builtins_str == NULL)
2084 return NULL;
2085 silly_list = Py_BuildValue("[s]", "__doc__");
2086 if (silly_list == NULL)
2087 return NULL;
2088 }
2089
2090 /* Get the builtins from current globals */
2091 globals = PyEval_GetGlobals();
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002092 if (globals != NULL) {
Guido van Rossum66468561998-10-22 15:46:50 +00002093 Py_INCREF(globals);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002094 builtins = PyObject_GetItem(globals, builtins_str);
2095 if (builtins == NULL)
2096 goto err;
2097 }
2098 else {
2099 /* No globals -- use standard builtins, and fake globals */
2100 PyErr_Clear();
2101
Guido van Rossum85cd1d62001-02-20 21:43:24 +00002102 builtins = PyImport_ImportModuleEx("__builtin__",
2103 NULL, NULL, NULL);
2104 if (builtins == NULL)
2105 return NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002106 globals = Py_BuildValue("{OO}", builtins_str, builtins);
2107 if (globals == NULL)
2108 goto err;
2109 }
2110
2111 /* Get the __import__ function from the builtins */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002112 if (PyDict_Check(builtins)) {
Fred Drakea76ba6e2001-03-06 06:31:15 +00002113 import = PyObject_GetItem(builtins, import_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002114 if (import == NULL)
2115 PyErr_SetObject(PyExc_KeyError, import_str);
2116 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002117 else
Fred Drakea76ba6e2001-03-06 06:31:15 +00002118 import = PyObject_GetAttr(builtins, import_str);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002119 if (import == NULL)
2120 goto err;
2121
2122 /* Call the _import__ function with the proper argument list */
2123 r = PyObject_CallFunction(import, "OOOO",
2124 module_name, globals, globals, silly_list);
2125
2126 err:
2127 Py_XDECREF(globals);
2128 Py_XDECREF(builtins);
2129 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00002130
Guido van Rossumd47a0a81997-08-14 20:11:26 +00002131 return r;
2132}
2133
2134
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002135/* Module 'imp' provides Python access to the primitives used for
2136 importing modules.
2137*/
2138
Guido van Rossum79f25d91997-04-29 20:08:16 +00002139static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002140imp_get_magic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002141{
2142 char buf[4];
2143
Guido van Rossum43713e52000-02-29 13:59:29 +00002144 if (!PyArg_ParseTuple(args, ":get_magic"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002145 return NULL;
Guido van Rossum96774c12000-05-01 20:19:08 +00002146 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
2147 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
2148 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
2149 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002150
Guido van Rossum79f25d91997-04-29 20:08:16 +00002151 return PyString_FromStringAndSize(buf, 4);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002152}
2153
Guido van Rossum79f25d91997-04-29 20:08:16 +00002154static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002155imp_get_suffixes(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002156{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002157 PyObject *list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002158 struct filedescr *fdp;
2159
Guido van Rossum43713e52000-02-29 13:59:29 +00002160 if (!PyArg_ParseTuple(args, ":get_suffixes"))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002161 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002162 list = PyList_New(0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002163 if (list == NULL)
2164 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002165 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
2166 PyObject *item = Py_BuildValue("ssi",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002167 fdp->suffix, fdp->mode, fdp->type);
2168 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002169 Py_DECREF(list);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002170 return NULL;
2171 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002172 if (PyList_Append(list, item) < 0) {
2173 Py_DECREF(list);
2174 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002175 return NULL;
2176 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002177 Py_DECREF(item);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002178 }
2179 return list;
2180}
2181
Guido van Rossum79f25d91997-04-29 20:08:16 +00002182static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002183call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002184{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002185 extern int fclose(FILE *);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002186 PyObject *fob, *ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002187 struct filedescr *fdp;
2188 char pathname[MAXPATHLEN+1];
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002189 FILE *fp = NULL;
2190
2191 pathname[0] = '\0';
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002192 if (path == Py_None)
2193 path = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002194 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
2195 if (fdp == NULL)
2196 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002197 if (fp != NULL) {
2198 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
2199 if (fob == NULL) {
2200 fclose(fp);
2201 return NULL;
2202 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002203 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002204 else {
2205 fob = Py_None;
2206 Py_INCREF(fob);
Tim Peters50d8d372001-02-28 05:34:27 +00002207 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002208 ret = Py_BuildValue("Os(ssi)",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002209 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002210 Py_DECREF(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002211 return ret;
2212}
2213
Guido van Rossum79f25d91997-04-29 20:08:16 +00002214static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002215imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002216{
2217 char *name;
2218 PyObject *path = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002219 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002220 return NULL;
2221 return call_find_module(name, path);
2222}
2223
2224static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002225imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002226{
2227 char *name;
2228 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002229 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002230 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002231 return NULL;
2232 ret = init_builtin(name);
2233 if (ret < 0)
2234 return NULL;
2235 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002236 Py_INCREF(Py_None);
2237 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002238 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002239 m = PyImport_AddModule(name);
2240 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002241 return m;
2242}
2243
Guido van Rossum79f25d91997-04-29 20:08:16 +00002244static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002245imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002246{
2247 char *name;
2248 int ret;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002249 PyObject *m;
Guido van Rossum43713e52000-02-29 13:59:29 +00002250 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002251 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002252 ret = PyImport_ImportFrozenModule(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002253 if (ret < 0)
2254 return NULL;
2255 if (ret == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002256 Py_INCREF(Py_None);
2257 return Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002258 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002259 m = PyImport_AddModule(name);
2260 Py_XINCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002261 return m;
2262}
2263
Guido van Rossum79f25d91997-04-29 20:08:16 +00002264static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002265imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002266{
2267 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002268
Guido van Rossum43713e52000-02-29 13:59:29 +00002269 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002270 return NULL;
2271 return get_frozen_object(name);
2272}
2273
Guido van Rossum79f25d91997-04-29 20:08:16 +00002274static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002275imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002276{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002277 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002278 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002279 return NULL;
Guido van Rossum50ee94f2002-04-09 18:00:58 +00002280 return PyInt_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002281}
2282
Guido van Rossum79f25d91997-04-29 20:08:16 +00002283static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002284imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002285{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002286 char *name;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002287 struct _frozen *p;
Guido van Rossum43713e52000-02-29 13:59:29 +00002288 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002289 return NULL;
Guido van Rossum323bf5e1998-06-24 03:54:06 +00002290 p = find_frozen(name);
Guido van Rossumb8bff3f2002-04-07 06:34:38 +00002291 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002292}
2293
2294static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002295get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002296{
2297 FILE *fp;
2298 if (fob == NULL) {
2299 fp = fopen(pathname, mode);
2300 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002301 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002302 }
2303 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002304 fp = PyFile_AsFile(fob);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002305 if (fp == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002306 PyErr_SetString(PyExc_ValueError,
2307 "bad/closed file object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002308 }
2309 return fp;
2310}
2311
Guido van Rossum79f25d91997-04-29 20:08:16 +00002312static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002313imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002314{
2315 char *name;
2316 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002317 PyObject *fob = NULL;
2318 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002319 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002320 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002321 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002322 return NULL;
2323 fp = get_file(pathname, fob, "rb");
2324 if (fp == NULL)
2325 return NULL;
2326 m = load_compiled_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002327 if (fob == NULL)
2328 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002329 return m;
2330}
2331
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002332#ifdef HAVE_DYNAMIC_LOADING
2333
Guido van Rossum79f25d91997-04-29 20:08:16 +00002334static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002335imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002336{
2337 char *name;
2338 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002339 PyObject *fob = NULL;
2340 PyObject *m;
Guido van Rossum7faeab31995-07-07 22:50:36 +00002341 FILE *fp = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +00002342 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002343 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002344 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002345 if (fob) {
Guido van Rossum7faeab31995-07-07 22:50:36 +00002346 fp = get_file(pathname, fob, "r");
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002347 if (fp == NULL)
2348 return NULL;
2349 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002350 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +00002351 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002352}
2353
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002354#endif /* HAVE_DYNAMIC_LOADING */
2355
Guido van Rossum79f25d91997-04-29 20:08:16 +00002356static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002357imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002358{
2359 char *name;
2360 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002361 PyObject *fob = NULL;
2362 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002363 FILE *fp;
Guido van Rossum43713e52000-02-29 13:59:29 +00002364 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002365 &PyFile_Type, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002366 return NULL;
2367 fp = get_file(pathname, fob, "r");
2368 if (fp == NULL)
2369 return NULL;
2370 m = load_source_module(name, pathname, fp);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002371 if (fob == NULL)
2372 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002373 return m;
2374}
2375
Jack Jansen9c96a921995-02-15 22:57:06 +00002376#ifdef macintosh
Guido van Rossum79f25d91997-04-29 20:08:16 +00002377static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002378imp_load_resource(PyObject *self, PyObject *args)
Jack Jansen9c96a921995-02-15 22:57:06 +00002379{
2380 char *name;
2381 char *pathname;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002382 PyObject *m;
Jack Jansen9c96a921995-02-15 22:57:06 +00002383
Guido van Rossum43713e52000-02-29 13:59:29 +00002384 if (!PyArg_ParseTuple(args, "ss:load_resource", &name, &pathname))
Jack Jansen9c96a921995-02-15 22:57:06 +00002385 return NULL;
2386 m = PyMac_LoadResourceModule(name, pathname);
2387 return m;
2388}
2389#endif /* macintosh */
2390
Guido van Rossum79f25d91997-04-29 20:08:16 +00002391static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002392imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002393{
2394 char *name;
2395 PyObject *fob;
2396 char *pathname;
2397 char *suffix; /* Unused */
2398 char *mode;
2399 int type;
2400 FILE *fp;
2401
Guido van Rossum43713e52000-02-29 13:59:29 +00002402 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002403 &name, &fob, &pathname,
2404 &suffix, &mode, &type))
2405 return NULL;
2406 if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) {
2407 PyErr_Format(PyExc_ValueError,
2408 "invalid file open mode %.200s", mode);
2409 return NULL;
2410 }
2411 if (fob == Py_None)
2412 fp = NULL;
2413 else {
2414 if (!PyFile_Check(fob)) {
2415 PyErr_SetString(PyExc_ValueError,
2416 "load_module arg#2 should be a file or None");
2417 return NULL;
2418 }
2419 fp = get_file(pathname, fob, mode);
2420 if (fp == NULL)
2421 return NULL;
2422 }
2423 return load_module(name, fp, pathname, type);
2424}
2425
2426static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002427imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002428{
2429 char *name;
2430 char *pathname;
Guido van Rossum43713e52000-02-29 13:59:29 +00002431 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002432 return NULL;
2433 return load_package(name, pathname);
2434}
2435
2436static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002437imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002438{
2439 char *name;
Guido van Rossum43713e52000-02-29 13:59:29 +00002440 if (!PyArg_ParseTuple(args, "s:new_module", &name))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002441 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002442 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002443}
2444
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002445/* Doc strings */
2446
2447static char doc_imp[] = "\
2448This module provides the components needed to build your own\n\
2449__import__ function. Undocumented functions are obsolete.\n\
2450";
2451
2452static char doc_find_module[] = "\
2453find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
2454Search for a module. If path is omitted or None, search for a\n\
2455built-in, frozen or special module and continue search in sys.path.\n\
2456The module name cannot contain '.'; to search for a submodule of a\n\
2457package, pass the submodule name and the package's __path__.\
2458";
2459
2460static char doc_load_module[] = "\
2461load_module(name, file, filename, (suffix, mode, type)) -> module\n\
2462Load a module, given information returned by find_module().\n\
2463The module name must include the full package name, if any.\
2464";
2465
2466static char doc_get_magic[] = "\
2467get_magic() -> string\n\
2468Return the magic number for .pyc or .pyo files.\
2469";
2470
2471static char doc_get_suffixes[] = "\
2472get_suffixes() -> [(suffix, mode, type), ...]\n\
2473Return a list of (suffix, mode, type) tuples describing the files\n\
2474that find_module() looks for.\
2475";
2476
2477static char doc_new_module[] = "\
2478new_module(name) -> module\n\
2479Create a new module. Do not enter it in sys.modules.\n\
2480The module name must include the full package name, if any.\
2481";
2482
Tim Peters69232342001-08-30 05:16:13 +00002483static char doc_lock_held[] = "\
2484lock_held() -> 0 or 1\n\
2485Return 1 if the import lock is currently held.\n\
2486On platforms without threads, return 0.\
2487";
2488
Guido van Rossum79f25d91997-04-29 20:08:16 +00002489static PyMethodDef imp_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +00002490 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
2491 {"get_magic", imp_get_magic, METH_VARARGS, doc_get_magic},
2492 {"get_suffixes", imp_get_suffixes, METH_VARARGS, doc_get_suffixes},
2493 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
2494 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
2495 {"lock_held", imp_lock_held, METH_VARARGS, doc_lock_held},
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002496 /* The rest are obsolete */
Neal Norwitz031829d2002-03-31 14:37:44 +00002497 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
2498 {"init_builtin", imp_init_builtin, METH_VARARGS},
2499 {"init_frozen", imp_init_frozen, METH_VARARGS},
2500 {"is_builtin", imp_is_builtin, METH_VARARGS},
2501 {"is_frozen", imp_is_frozen, METH_VARARGS},
2502 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002503#ifdef HAVE_DYNAMIC_LOADING
Neal Norwitz031829d2002-03-31 14:37:44 +00002504 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002505#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002506 {"load_package", imp_load_package, METH_VARARGS},
Jack Jansen9c96a921995-02-15 22:57:06 +00002507#ifdef macintosh
Neal Norwitz031829d2002-03-31 14:37:44 +00002508 {"load_resource", imp_load_resource, METH_VARARGS},
Jack Jansen9c96a921995-02-15 22:57:06 +00002509#endif
Neal Norwitz031829d2002-03-31 14:37:44 +00002510 {"load_source", imp_load_source, METH_VARARGS},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002511 {NULL, NULL} /* sentinel */
2512};
2513
Guido van Rossum1a8791e1998-08-04 22:46:29 +00002514static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002515setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002516{
2517 PyObject *v;
2518 int err;
2519
2520 v = PyInt_FromLong((long)value);
2521 err = PyDict_SetItemString(d, name, v);
2522 Py_XDECREF(v);
2523 return err;
2524}
2525
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002526void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002527initimp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002528{
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002529 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002530
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002531 m = Py_InitModule4("imp", imp_methods, doc_imp,
2532 NULL, PYTHON_API_VERSION);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002533 d = PyModule_GetDict(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002534
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002535 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
2536 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
2537 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
2538 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
2539 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
2540 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
2541 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
2542 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
Guido van Rossum0f84a341998-08-06 13:36:01 +00002543 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002544
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002545 failure:
2546 ;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002547}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002548
2549
Guido van Rossumb18618d2000-05-03 23:44:39 +00002550/* API for embedding applications that want to add their own entries
2551 to the table of built-in modules. This should normally be called
2552 *before* Py_Initialize(). When the table resize fails, -1 is
2553 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002554
2555 After a similar function by Just van Rossum. */
2556
2557int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002558PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002559{
2560 static struct _inittab *our_copy = NULL;
2561 struct _inittab *p;
2562 int i, n;
2563
2564 /* Count the number of entries in both tables */
2565 for (n = 0; newtab[n].name != NULL; n++)
2566 ;
2567 if (n == 0)
2568 return 0; /* Nothing to do */
2569 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2570 ;
2571
2572 /* Allocate new memory for the combined table */
Guido van Rossumb18618d2000-05-03 23:44:39 +00002573 p = our_copy;
2574 PyMem_RESIZE(p, struct _inittab, i+n+1);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002575 if (p == NULL)
2576 return -1;
2577
2578 /* Copy the tables into the new memory */
2579 if (our_copy != PyImport_Inittab)
2580 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2581 PyImport_Inittab = our_copy = p;
2582 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
2583
2584 return 0;
2585}
2586
2587/* Shorthand to add a single entry given a name and a function */
2588
2589int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002590PyImport_AppendInittab(char *name, void (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002591{
2592 struct _inittab newtab[2];
2593
2594 memset(newtab, '\0', sizeof newtab);
2595
2596 newtab[0].name = name;
2597 newtab[0].initfunc = initfunc;
2598
2599 return PyImport_ExtendInittab(newtab);
2600}