blob: 7fc50c88638bfe6e5c7b54e457bdd0430c8bafe3 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Module definition and import implementation */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030#include "token.h"
31#include "graminit.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032#include "import.h"
33#include "errcode.h"
34#include "sysmodule.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000035#include "pythonrun.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000036#include "marshal.h"
37#include "compile.h"
38#include "ceval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000039#include "osdefs.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000040
Guido van Rossume25c2561992-01-19 16:28:21 +000041#ifdef DEBUG
42#define D(x) x
43#else
44#define D(x)
45#endif
46
47#ifdef USE_DL
48#include "dl.h"
49
Guido van Rossume0513de1992-01-26 18:15:22 +000050extern char *argv0;
Guido van Rossume25c2561992-01-19 16:28:21 +000051#endif
52
Guido van Rossum3ddee711991-12-16 13:06:34 +000053/* Magic word to reject pre-0.9.4 .pyc files */
54
55#define MAGIC 0x949494L
56
Guido van Rossum3f5da241990-12-20 15:06:42 +000057static object *modules;
58
Guido van Rossum66f1fa81991-04-03 19:03:52 +000059/* Forward */
60static int init_builtin PROTO((char *));
61
Guido van Rossum3f5da241990-12-20 15:06:42 +000062/* Initialization */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063
64void
65initimport()
66{
Guido van Rossum3f5da241990-12-20 15:06:42 +000067 if ((modules = newdictobject()) == NULL)
68 fatal("no mem for dictionary of modules");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069}
70
71object *
Guido van Rossum3f5da241990-12-20 15:06:42 +000072get_modules()
73{
74 return modules;
75}
76
77object *
78add_module(name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 char *name;
80{
81 object *m;
Guido van Rossum3f5da241990-12-20 15:06:42 +000082 if ((m = dictlookup(modules, name)) != NULL && is_moduleobject(m))
83 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084 m = newmoduleobject(name);
85 if (m == NULL)
86 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000087 if (dictinsert(modules, name, m) != 0) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 DECREF(m);
89 return NULL;
90 }
Guido van Rossum3f5da241990-12-20 15:06:42 +000091 DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092 return m;
93}
94
Guido van Rossume25c2561992-01-19 16:28:21 +000095/* Suffixes used by open_module: */
96
97#define PY_SUFFIX ".py"
98#ifdef USE_DL
99#define O_SUFFIX "module.o"
100#endif
101
102/* Find and open a module file, using sys.path.
103 Return a NULL pointer if no module file is found.
104 When dynamic loading is enabled, the contents of namebuf
105 is important when NULL is returned: if namebuf[0] != '\0'
106 a dl-able object file was found and namebuf is its pathname. */
107
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108static FILE *
Guido van Rossume25c2561992-01-19 16:28:21 +0000109open_module(name, namebuf)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000111 char *namebuf; /* XXX No buffer overflow checks! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
113 object *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 FILE *fp;
115
116 path = sysget("path");
117 if (path == NULL || !is_listobject(path)) {
Guido van Rossume25c2561992-01-19 16:28:21 +0000118 /* No path -- at least try current directory */
119#ifdef USE_DL
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120 strcpy(namebuf, name);
Guido van Rossume25c2561992-01-19 16:28:21 +0000121 strcat(namebuf, O_SUFFIX);
122 if (getmtime(namebuf) > 0)
123 return NULL;
124#endif
125 strcpy(namebuf, name);
126 strcat(namebuf, PY_SUFFIX);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127 fp = fopen(namebuf, "r");
128 }
129 else {
130 int npath = getlistsize(path);
131 int i;
132 fp = NULL;
133 for (i = 0; i < npath; i++) {
134 object *v = getlistitem(path, i);
135 int len;
136 if (!is_stringobject(v))
137 continue;
138 strcpy(namebuf, getstringvalue(v));
139 len = getstringsize(v);
140 if (len > 0 && namebuf[len-1] != SEP)
141 namebuf[len++] = SEP;
Guido van Rossume25c2561992-01-19 16:28:21 +0000142#ifdef USE_DL
Guido van Rossum3f5da241990-12-20 15:06:42 +0000143 strcpy(namebuf+len, name);
Guido van Rossume25c2561992-01-19 16:28:21 +0000144 strcat(namebuf, O_SUFFIX);
145 if (getmtime(namebuf) > 0)
146 return NULL;
147#endif
148 strcpy(namebuf+len, name);
149 strcat(namebuf, PY_SUFFIX);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 fp = fopen(namebuf, "r");
151 if (fp != NULL)
152 break;
153 }
154 }
Guido van Rossume25c2561992-01-19 16:28:21 +0000155 if (fp == NULL)
156 namebuf[0] = '\0';
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157 return fp;
158}
159
160static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000161get_module(m, name, m_ret)
162 /*module*/object *m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000164 object **m_ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165{
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000166 codeobject *co = NULL;
167 object *v, *d;
168 FILE *fp, *fpc;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000169 node *n;
170 int err;
Guido van Rossumd8bac6d1992-02-26 15:19:13 +0000171 char namebuf[MAXPATHLEN+1];
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000172 int namelen;
173 long mtime;
174 extern long getmtime();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000175
Guido van Rossume25c2561992-01-19 16:28:21 +0000176 fp = open_module(name, namebuf);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000177 if (fp == NULL) {
Guido van Rossume25c2561992-01-19 16:28:21 +0000178#ifdef USE_DL
179 if (namebuf[0] != '\0') {
180 char funcname[258];
181 dl_funcptr p;
182 D(fprintf(stderr, "Found %s\n", namebuf));
183 sprintf(funcname, "init%s", name);
Guido van Rossume0513de1992-01-26 18:15:22 +0000184 p = dl_loadmod(argv0, namebuf, funcname);
Guido van Rossume25c2561992-01-19 16:28:21 +0000185 if (p == NULL) {
186 D(fprintf(stderr, "dl_loadmod failed\n"));
187 }
188 else {
189 (*p)();
190 *m_ret = m = dictlookup(modules, name);
191 if (m == NULL) {
192 err_setstr(SystemError,
193 "dynamic module missing");
194 return NULL;
195 }
196 else {
197 D(fprintf(stderr,
198 "module %s loaded!\n", name));
199 INCREF(None);
200 return None;
201 }
202 }
203 }
204#endif
Guido van Rossum4135e781991-12-24 13:26:56 +0000205 if (m == NULL) {
206 sprintf(namebuf, "no module named %.200s", name);
207 err_setstr(ImportError, namebuf);
208 }
209 else {
210 sprintf(namebuf, "no source for module %.200s", name);
211 err_setstr(ImportError, namebuf);
212 }
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000213 return NULL;
214 }
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000215 /* Get mtime -- always useful */
216 mtime = getmtime(namebuf);
217 /* Check ".pyc" file first */
218 namelen = strlen(namebuf);
219 namebuf[namelen] = 'c';
220 namebuf[namelen+1] = '\0';
221 fpc = fopen(namebuf, "rb");
222 if (fpc != NULL) {
223 long pyc_mtime;
Guido van Rossum3ddee711991-12-16 13:06:34 +0000224 long magic;
225 magic = rd_long(fpc);
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000226 pyc_mtime = rd_long(fpc);
Guido van Rossum3ddee711991-12-16 13:06:34 +0000227 if (magic == MAGIC && pyc_mtime == mtime && mtime != 0 && mtime != -1) {
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000228 v = rd_object(fpc);
229 if (v == NULL || err_occurred() || !is_codeobject(v)) {
230 err_clear();
231 XDECREF(v);
232 }
233 else
234 co = (codeobject *)v;
235 }
236 fclose(fpc);
237 }
238 namebuf[namelen] = '\0';
239 if (co == NULL)
240 err = parse_file(fp, namebuf, file_input, &n);
241 else
242 err = E_DONE;
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000243 fclose(fp);
244 if (err != E_DONE) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000245 err_input(err);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000246 return NULL;
247 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000248 if (m == NULL) {
249 m = add_module(name);
250 if (m == NULL) {
251 freetree(n);
252 return NULL;
253 }
254 *m_ret = m;
255 }
256 d = getmoduledict(m);
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000257 if (co == NULL) {
258 co = compile(n, namebuf);
259 freetree(n);
260 if (co == NULL)
261 return NULL;
262 /* Now write the code object to the ".pyc" file */
263 namebuf[namelen] = 'c';
264 namebuf[namelen+1] = '\0';
265 fpc = fopen(namebuf, "wb");
266 if (fpc != NULL) {
Guido van Rossum3ddee711991-12-16 13:06:34 +0000267 wr_long(MAGIC, fpc);
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000268 /* First write a 0 for mtime */
269 wr_long(0L, fpc);
270 wr_object((object *)co, fpc);
271 if (ferror(fpc)) {
272 /* Don't keep partial file */
273 fclose(fpc);
274 (void) unlink(namebuf);
275 }
276 else {
277 /* Now write the true mtime */
278 fseek(fpc, 4L, 0);
279 wr_long(mtime, fpc);
280 fflush(fpc);
281 fclose(fpc);
282 }
283 }
284 }
285 v = eval_code(co, d, d, (object *)NULL);
286 DECREF(co);
287 return v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000288}
289
290static object *
291load_module(name)
292 char *name;
293{
294 object *m, *v;
295 v = get_module((object *)NULL, name, &m);
296 if (v == NULL)
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000297 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000298 DECREF(v);
299 return m;
300}
301
302object *
303import_module(name)
304 char *name;
305{
306 object *m;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000307 if ((m = dictlookup(modules, name)) == NULL) {
308 if (init_builtin(name)) {
309 if ((m = dictlookup(modules, name)) == NULL)
310 err_setstr(SystemError, "builtin module missing");
311 }
312 else {
313 m = load_module(name);
314 }
315 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000316 return m;
317}
318
319object *
320reload_module(m)
321 object *m;
322{
323 if (m == NULL || !is_moduleobject(m)) {
324 err_setstr(TypeError, "reload() argument must be module");
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000325 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000326 }
327 /* XXX Ought to check for builtin modules -- can't reload these... */
328 return get_module(m, getmodulename(m), (object **)NULL);
329}
330
331static void
332cleardict(d)
333 object *d;
334{
335 int i;
336 for (i = getdictsize(d); --i >= 0; ) {
337 char *k;
338 k = getdictkey(d, i);
339 if (k != NULL)
340 (void) dictremove(d, k);
341 }
342}
343
344void
345doneimport()
346{
347 if (modules != NULL) {
348 int i;
349 /* Explicitly erase all modules; this is the safest way
350 to get rid of at least *some* circular dependencies */
351 for (i = getdictsize(modules); --i >= 0; ) {
Guido van Rossumf0ada4a1991-08-16 09:01:08 +0000352 object *k;
353 k = getdict2key(modules, i);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000354 if (k != NULL) {
355 object *m;
Guido van Rossumf0ada4a1991-08-16 09:01:08 +0000356 m = dict2lookup(modules, k);
357 if (m == NULL)
358 err_clear();
359 else if (is_moduleobject(m)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000360 object *d;
361 d = getmoduledict(m);
362 if (d != NULL && is_dictobject(d)) {
363 cleardict(d);
364 }
365 }
366 }
367 }
368 cleardict(modules);
369 }
370 DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000371}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000372
373
374/* Initialize built-in modules when first imported */
375
376extern struct {
377 char *name;
378 void (*initfunc)();
379} inittab[];
380
381static int
382init_builtin(name)
383 char *name;
384{
385 int i;
386 for (i = 0; inittab[i].name != NULL; i++) {
387 if (strcmp(name, inittab[i].name) == 0) {
388 (*inittab[i].initfunc)();
389 return 1;
390 }
391 }
392 return 0;
393}