blob: 4ee72a91e19e51957ea530620b002e5d7c992078 [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"
39
Guido van Rossume25c2561992-01-19 16:28:21 +000040#ifdef DEBUG
41#define D(x) x
42#else
43#define D(x)
44#endif
45
46#ifdef USE_DL
47#include "dl.h"
48
49static char *getbinaryname();
50#endif
51
Guido van Rossum3ddee711991-12-16 13:06:34 +000052/* Magic word to reject pre-0.9.4 .pyc files */
53
54#define MAGIC 0x949494L
55
Guido van Rossum3f5da241990-12-20 15:06:42 +000056/* Define pathname separator used in file names */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057
Guido van Rossumd6a15ad1991-06-24 22:30:42 +000058#ifdef macintosh
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059#define SEP ':'
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060#endif
61
Guido van Rossum175a9ea1991-05-05 20:07:59 +000062#ifdef MSDOS
63#define SEP '\\'
64#endif
65
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066#ifndef SEP
67#define SEP '/'
68#endif
69
Guido van Rossum3f5da241990-12-20 15:06:42 +000070static object *modules;
71
Guido van Rossum66f1fa81991-04-03 19:03:52 +000072/* Forward */
73static int init_builtin PROTO((char *));
74
Guido van Rossum3f5da241990-12-20 15:06:42 +000075/* Initialization */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076
77void
78initimport()
79{
Guido van Rossum3f5da241990-12-20 15:06:42 +000080 if ((modules = newdictobject()) == NULL)
81 fatal("no mem for dictionary of modules");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082}
83
84object *
Guido van Rossum3f5da241990-12-20 15:06:42 +000085get_modules()
86{
87 return modules;
88}
89
90object *
91add_module(name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092 char *name;
93{
94 object *m;
Guido van Rossum3f5da241990-12-20 15:06:42 +000095 if ((m = dictlookup(modules, name)) != NULL && is_moduleobject(m))
96 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097 m = newmoduleobject(name);
98 if (m == NULL)
99 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000100 if (dictinsert(modules, name, m) != 0) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 DECREF(m);
102 return NULL;
103 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000104 DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105 return m;
106}
107
Guido van Rossume25c2561992-01-19 16:28:21 +0000108/* Suffixes used by open_module: */
109
110#define PY_SUFFIX ".py"
111#ifdef USE_DL
112#define O_SUFFIX "module.o"
113#endif
114
115/* Find and open a module file, using sys.path.
116 Return a NULL pointer if no module file is found.
117 When dynamic loading is enabled, the contents of namebuf
118 is important when NULL is returned: if namebuf[0] != '\0'
119 a dl-able object file was found and namebuf is its pathname. */
120
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121static FILE *
Guido van Rossume25c2561992-01-19 16:28:21 +0000122open_module(name, namebuf)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000124 char *namebuf; /* XXX No buffer overflow checks! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125{
126 object *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127 FILE *fp;
128
129 path = sysget("path");
130 if (path == NULL || !is_listobject(path)) {
Guido van Rossume25c2561992-01-19 16:28:21 +0000131 /* No path -- at least try current directory */
132#ifdef USE_DL
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133 strcpy(namebuf, name);
Guido van Rossume25c2561992-01-19 16:28:21 +0000134 strcat(namebuf, O_SUFFIX);
135 if (getmtime(namebuf) > 0)
136 return NULL;
137#endif
138 strcpy(namebuf, name);
139 strcat(namebuf, PY_SUFFIX);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140 fp = fopen(namebuf, "r");
141 }
142 else {
143 int npath = getlistsize(path);
144 int i;
145 fp = NULL;
146 for (i = 0; i < npath; i++) {
147 object *v = getlistitem(path, i);
148 int len;
149 if (!is_stringobject(v))
150 continue;
151 strcpy(namebuf, getstringvalue(v));
152 len = getstringsize(v);
153 if (len > 0 && namebuf[len-1] != SEP)
154 namebuf[len++] = SEP;
Guido van Rossume25c2561992-01-19 16:28:21 +0000155#ifdef USE_DL
Guido van Rossum3f5da241990-12-20 15:06:42 +0000156 strcpy(namebuf+len, name);
Guido van Rossume25c2561992-01-19 16:28:21 +0000157 strcat(namebuf, O_SUFFIX);
158 if (getmtime(namebuf) > 0)
159 return NULL;
160#endif
161 strcpy(namebuf+len, name);
162 strcat(namebuf, PY_SUFFIX);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163 fp = fopen(namebuf, "r");
164 if (fp != NULL)
165 break;
166 }
167 }
Guido van Rossume25c2561992-01-19 16:28:21 +0000168 if (fp == NULL)
169 namebuf[0] = '\0';
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170 return fp;
171}
172
173static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000174get_module(m, name, m_ret)
175 /*module*/object *m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000177 object **m_ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178{
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000179 codeobject *co = NULL;
180 object *v, *d;
181 FILE *fp, *fpc;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000182 node *n;
183 int err;
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000184 char namebuf[258];
185 int namelen;
186 long mtime;
187 extern long getmtime();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000188
Guido van Rossume25c2561992-01-19 16:28:21 +0000189 fp = open_module(name, namebuf);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000190 if (fp == NULL) {
Guido van Rossume25c2561992-01-19 16:28:21 +0000191#ifdef USE_DL
192 if (namebuf[0] != '\0') {
193 char funcname[258];
194 dl_funcptr p;
195 D(fprintf(stderr, "Found %s\n", namebuf));
196 sprintf(funcname, "init%s", name);
197 p = dl_loadmod(getbinaryname(), namebuf, funcname);
198 if (p == NULL) {
199 D(fprintf(stderr, "dl_loadmod failed\n"));
200 }
201 else {
202 (*p)();
203 *m_ret = m = dictlookup(modules, name);
204 if (m == NULL) {
205 err_setstr(SystemError,
206 "dynamic module missing");
207 return NULL;
208 }
209 else {
210 D(fprintf(stderr,
211 "module %s loaded!\n", name));
212 INCREF(None);
213 return None;
214 }
215 }
216 }
217#endif
Guido van Rossum4135e781991-12-24 13:26:56 +0000218 if (m == NULL) {
219 sprintf(namebuf, "no module named %.200s", name);
220 err_setstr(ImportError, namebuf);
221 }
222 else {
223 sprintf(namebuf, "no source for module %.200s", name);
224 err_setstr(ImportError, namebuf);
225 }
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000226 return NULL;
227 }
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000228 /* Get mtime -- always useful */
229 mtime = getmtime(namebuf);
230 /* Check ".pyc" file first */
231 namelen = strlen(namebuf);
232 namebuf[namelen] = 'c';
233 namebuf[namelen+1] = '\0';
234 fpc = fopen(namebuf, "rb");
235 if (fpc != NULL) {
236 long pyc_mtime;
Guido van Rossum3ddee711991-12-16 13:06:34 +0000237 long magic;
238 magic = rd_long(fpc);
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000239 pyc_mtime = rd_long(fpc);
Guido van Rossum3ddee711991-12-16 13:06:34 +0000240 if (magic == MAGIC && pyc_mtime == mtime && mtime != 0 && mtime != -1) {
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000241 v = rd_object(fpc);
242 if (v == NULL || err_occurred() || !is_codeobject(v)) {
243 err_clear();
244 XDECREF(v);
245 }
246 else
247 co = (codeobject *)v;
248 }
249 fclose(fpc);
250 }
251 namebuf[namelen] = '\0';
252 if (co == NULL)
253 err = parse_file(fp, namebuf, file_input, &n);
254 else
255 err = E_DONE;
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000256 fclose(fp);
257 if (err != E_DONE) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000258 err_input(err);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000259 return NULL;
260 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000261 if (m == NULL) {
262 m = add_module(name);
263 if (m == NULL) {
264 freetree(n);
265 return NULL;
266 }
267 *m_ret = m;
268 }
269 d = getmoduledict(m);
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000270 if (co == NULL) {
271 co = compile(n, namebuf);
272 freetree(n);
273 if (co == NULL)
274 return NULL;
275 /* Now write the code object to the ".pyc" file */
276 namebuf[namelen] = 'c';
277 namebuf[namelen+1] = '\0';
278 fpc = fopen(namebuf, "wb");
279 if (fpc != NULL) {
Guido van Rossum3ddee711991-12-16 13:06:34 +0000280 wr_long(MAGIC, fpc);
Guido van Rossumc405b7b1991-06-04 19:39:42 +0000281 /* First write a 0 for mtime */
282 wr_long(0L, fpc);
283 wr_object((object *)co, fpc);
284 if (ferror(fpc)) {
285 /* Don't keep partial file */
286 fclose(fpc);
287 (void) unlink(namebuf);
288 }
289 else {
290 /* Now write the true mtime */
291 fseek(fpc, 4L, 0);
292 wr_long(mtime, fpc);
293 fflush(fpc);
294 fclose(fpc);
295 }
296 }
297 }
298 v = eval_code(co, d, d, (object *)NULL);
299 DECREF(co);
300 return v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000301}
302
303static object *
304load_module(name)
305 char *name;
306{
307 object *m, *v;
308 v = get_module((object *)NULL, name, &m);
309 if (v == NULL)
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000310 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000311 DECREF(v);
312 return m;
313}
314
315object *
316import_module(name)
317 char *name;
318{
319 object *m;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000320 if ((m = dictlookup(modules, name)) == NULL) {
321 if (init_builtin(name)) {
322 if ((m = dictlookup(modules, name)) == NULL)
323 err_setstr(SystemError, "builtin module missing");
324 }
325 else {
326 m = load_module(name);
327 }
328 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000329 return m;
330}
331
332object *
333reload_module(m)
334 object *m;
335{
336 if (m == NULL || !is_moduleobject(m)) {
337 err_setstr(TypeError, "reload() argument must be module");
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000338 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000339 }
340 /* XXX Ought to check for builtin modules -- can't reload these... */
341 return get_module(m, getmodulename(m), (object **)NULL);
342}
343
344static void
345cleardict(d)
346 object *d;
347{
348 int i;
349 for (i = getdictsize(d); --i >= 0; ) {
350 char *k;
351 k = getdictkey(d, i);
352 if (k != NULL)
353 (void) dictremove(d, k);
354 }
355}
356
357void
358doneimport()
359{
360 if (modules != NULL) {
361 int i;
362 /* Explicitly erase all modules; this is the safest way
363 to get rid of at least *some* circular dependencies */
364 for (i = getdictsize(modules); --i >= 0; ) {
Guido van Rossumf0ada4a1991-08-16 09:01:08 +0000365 object *k;
366 k = getdict2key(modules, i);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000367 if (k != NULL) {
368 object *m;
Guido van Rossumf0ada4a1991-08-16 09:01:08 +0000369 m = dict2lookup(modules, k);
370 if (m == NULL)
371 err_clear();
372 else if (is_moduleobject(m)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000373 object *d;
374 d = getmoduledict(m);
375 if (d != NULL && is_dictobject(d)) {
376 cleardict(d);
377 }
378 }
379 }
380 }
381 cleardict(modules);
382 }
383 DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000384}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000385
386
387/* Initialize built-in modules when first imported */
388
389extern struct {
390 char *name;
391 void (*initfunc)();
392} inittab[];
393
394static int
395init_builtin(name)
396 char *name;
397{
398 int i;
399 for (i = 0; inittab[i].name != NULL; i++) {
400 if (strcmp(name, inittab[i].name) == 0) {
401 (*inittab[i].initfunc)();
402 return 1;
403 }
404 }
405 return 0;
406}
Guido van Rossume25c2561992-01-19 16:28:21 +0000407
408#ifdef USE_DL
409
410/* A function to find a filename for the currently executing binary.
411 Because this is not directly available, we have to search for argv[0]
412 along $PATH. But note that if argv[0] contains a slash anywhere,
413 sh(1) doesn't search $PATH -- so neither do we! */
414
415/* XXX This should be moved to a more system-specific file */
416
417#include <sys/types.h>
418#include <sys/stat.h> /* For stat */
419
420extern char *getenv();
421
422extern char *argv0; /* In config.c */
423
424/* Default path from sh(1) in Irix 4.0.1 */
425#define DEF_PATH ":/usr/sbin:/usr/bsd:/bin:/usr/bin:/usr/bin/X11"
426
427static char *
428getbinaryname()
429{
430 char *p, *q;
431 char *path;
432 static char buf[258];
433 int i;
434 struct stat st;
435
436 if (strchr(argv0, '/') != NULL) {
437 D(fprintf(stderr, "binary includes slash: %s\n", argv0));
438 return argv0;
439 }
440 path = getenv("PATH");
441 if (path == NULL)
442 path = DEF_PATH;
443 p = q = path;
444 for (;;) {
445 while (*q && *q != ':')
446 q++;
447 i = q-p;
448 strncpy(buf, p, i);
449 if (q > p && q[-1] != '/')
450 buf[i++] = '/';
451 strcpy(buf+i, argv0);
452 if (stat(buf, &st) >= 0) {
453 if (S_ISREG(st.st_mode) &&
454 (st.st_mode & 0111)) {
455 D(fprintf(stderr, "found binary: %s\n", buf));
456 return buf;
457 }
458 }
459 if (!*q)
460 break;
461 p = ++q;
462 }
463 D(fprintf(stderr, "can't find binary: %s\n", argv0));
464 return argv0;
465}
466
467#endif