blob: 56254ac285d69566f7c72502167dc976164e5bd3 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
Guido van Rossum9bfef441993-03-29 10:43:31 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
Guido van Rossum1984f1e1992-08-04 12:41:02 +00004
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
25/* Python interpreter top-level routines, including init/exit */
26
27#include "allobjects.h"
28
29#include "grammar.h"
30#include "node.h"
31#include "parsetok.h"
32#include "graminit.h"
33#include "errcode.h"
34#include "sysmodule.h"
35#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000036#include "eval.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000037#include "ceval.h"
38#include "pythonrun.h"
39#include "import.h"
40
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000041#ifdef unix
42#define HANDLE_SIGNALS
43#endif
44
45#ifdef HANDLE_SIGNALS
46#include <signal.h>
47#include "sigtype.h"
48#endif
49
Guido van Rossum1984f1e1992-08-04 12:41:02 +000050extern char *getpythonpath();
51
52extern grammar gram; /* From graminit.c */
53
Guido van Rossumb73cc041993-11-01 16:28:59 +000054/* Forward */
55static object *run_err_node PROTO((int err, node *n, char *filename,
56 object *globals, object *locals));
57static object *run_node PROTO((node *n, char *filename,
58 object *globals, object *locals));
59static object *eval_node PROTO((node *n, char *filename,
60 object *globals, object *locals));
61void initsigs PROTO(());
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000062
Guido van Rossum1984f1e1992-08-04 12:41:02 +000063int debugging; /* Needed by parser.c */
64int verbose; /* Needed by import.c */
65
66/* Initialize all */
67
68void
69initall()
70{
71 static int inited;
72
73 if (inited)
74 return;
75 inited = 1;
76
77 initimport();
78
79 /* Modules 'builtin' and 'sys' are initialized here,
80 they are needed by random bits of the interpreter.
81 All other modules are optional and are initialized
82 when they are first imported. */
83
84 initbuiltin(); /* Also initializes builtin exceptions */
85 initsys();
86
87 initcalls(); /* Configuration-dependent initializations */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000088
89 setpythonpath(getpythonpath());
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000090
91 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000092}
93
94/* Parse input from a file and execute it */
95
96int
97run(fp, filename)
98 FILE *fp;
99 char *filename;
100{
101 if (filename == NULL)
102 filename = "???";
103 if (isatty((int)fileno(fp)))
104 return run_tty_loop(fp, filename);
105 else
106 return run_script(fp, filename);
107}
108
109int
110run_tty_loop(fp, filename)
111 FILE *fp;
112 char *filename;
113{
114 object *v;
115 int ret;
116 v = sysget("ps1");
117 if (v == NULL) {
118 sysset("ps1", v = newstringobject(">>> "));
119 XDECREF(v);
120 }
121 v = sysget("ps2");
122 if (v == NULL) {
123 sysset("ps2", v = newstringobject("... "));
124 XDECREF(v);
125 }
126 for (;;) {
127 ret = run_tty_1(fp, filename);
128#ifdef REF_DEBUG
129 fprintf(stderr, "[%ld refs]\n", ref_total);
130#endif
131 if (ret == E_EOF)
132 return 0;
133 /*
134 if (ret == E_NOMEM)
135 return -1;
136 */
137 }
138}
139
140int
141run_tty_1(fp, filename)
142 FILE *fp;
143 char *filename;
144{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000145 object *m, *d, *v, *w;
146 node *n;
147 char *ps1, *ps2;
148 int err;
149 v = sysget("ps1");
150 w = sysget("ps2");
151 if (v != NULL && is_stringobject(v)) {
152 INCREF(v);
153 ps1 = getstringvalue(v);
154 }
155 else {
156 v = NULL;
157 ps1 = "";
158 }
159 if (w != NULL && is_stringobject(w)) {
160 INCREF(w);
161 ps2 = getstringvalue(w);
162 }
163 else {
164 w = NULL;
165 ps2 = "";
166 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000167 BGN_SAVE
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000168 err = parsefile(fp, filename, &gram, single_input, ps1, ps2, &n);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000169 END_SAVE
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000170 XDECREF(v);
171 XDECREF(w);
172 if (err == E_EOF)
173 return E_EOF;
174 if (err != E_DONE) {
175 err_input(err);
176 print_error();
177 return err;
178 }
179 m = add_module("__main__");
180 if (m == NULL)
181 return -1;
182 d = getmoduledict(m);
183 v = run_node(n, filename, d, d);
184 flushline();
185 if (v == NULL) {
186 print_error();
187 return -1;
188 }
189 DECREF(v);
190 return 0;
191}
192
193int
194run_script(fp, filename)
195 FILE *fp;
196 char *filename;
197{
198 object *m, *d, *v;
199 m = add_module("__main__");
200 if (m == NULL)
201 return -1;
202 d = getmoduledict(m);
203 v = run_file(fp, filename, file_input, d, d);
204 flushline();
205 if (v == NULL) {
206 print_error();
207 return -1;
208 }
209 DECREF(v);
210 return 0;
211}
212
213int
214run_command(command)
215 char *command;
216{
217 object *m, *d, *v;
218 m = add_module("__main__");
219 if (m == NULL)
220 return -1;
221 d = getmoduledict(m);
222 v = run_string(command, file_input, d, d);
223 flushline();
224 if (v == NULL) {
225 print_error();
226 return -1;
227 }
228 DECREF(v);
229 return 0;
230}
231
232void
233print_error()
234{
Guido van Rossum3165fe61992-09-25 21:59:05 +0000235 object *exception, *v, *f;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000236 err_get(&exception, &v);
237 if (exception == SystemExit) {
238 if (v == NULL || v == None)
239 goaway(0);
240 if (is_intobject(v))
241 goaway((int)getintvalue(v));
242 else {
Guido van Rossum3165fe61992-09-25 21:59:05 +0000243 /* OK to use real stderr here */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000244 printobject(v, stderr, PRINT_RAW);
245 fprintf(stderr, "\n");
246 goaway(1);
247 }
248 }
249 sysset("last_type", exception);
250 sysset("last_value", v);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000251 f = sysget("stderr");
252 if (f == NULL)
253 fprintf(stderr, "lost sys.stderr\n");
254 else {
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000255 printtraceback(f);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000256 if (writeobject(exception, f, PRINT_RAW) != 0)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000257 err_clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +0000258 if (v != NULL && v != None) {
259 writestring(": ", f);
260 if (writeobject(v, f, PRINT_RAW) != 0)
261 err_clear();
262 }
263 writestring("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000264 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000265 XDECREF(exception);
266 XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267}
268
269object *
270run_string(str, start, globals, locals)
271 char *str;
272 int start;
Guido van Rossum5b722181993-03-30 17:46:03 +0000273 object *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000274{
275 node *n;
276 int err;
277 err = parse_string(str, start, &n);
278 return run_err_node(err, n, "<string>", globals, locals);
279}
280
281object *
282run_file(fp, filename, start, globals, locals)
283 FILE *fp;
284 char *filename;
285 int start;
Guido van Rossum5b722181993-03-30 17:46:03 +0000286 object *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000287{
288 node *n;
289 int err;
290 err = parse_file(fp, filename, start, &n);
291 return run_err_node(err, n, filename, globals, locals);
292}
293
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000294static object *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295run_err_node(err, n, filename, globals, locals)
296 int err;
297 node *n;
298 char *filename;
Guido van Rossum5b722181993-03-30 17:46:03 +0000299 object *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000300{
301 if (err != E_DONE) {
302 err_input(err);
303 return NULL;
304 }
305 return run_node(n, filename, globals, locals);
306}
307
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000308static object *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000309run_node(n, filename, globals, locals)
310 node *n;
311 char *filename;
Guido van Rossum5b722181993-03-30 17:46:03 +0000312 object *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313{
Guido van Rossum5b722181993-03-30 17:46:03 +0000314 return eval_node(n, filename, globals, locals);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000315}
316
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000317static object *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318eval_node(n, filename, globals, locals)
319 node *n;
320 char *filename;
321 object *globals;
322 object *locals;
323{
324 codeobject *co;
325 object *v;
326 co = compile(n, filename);
327 freetree(n);
328 if (co == NULL)
329 return NULL;
Guido van Rossum81daa321993-05-20 14:24:46 +0000330 v = eval_code(co, globals, locals, (object *)NULL, (object *)NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000331 DECREF(co);
332 return v;
333}
334
Guido van Rossum5b722181993-03-30 17:46:03 +0000335object *
336compile_string(str, filename, start)
337 char *str;
338 char *filename;
339 int start;
340{
341 node *n;
342 int err;
343 codeobject *co;
344 err = parse_string(str, start, &n);
345 if (err != E_DONE) {
346 err_input(err);
347 return NULL;
348 }
349 co = compile(n, filename);
350 freetree(n);
351 return (object *)co;
352}
353
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000354/* Simplified interface to parsefile */
355
356int
357parse_file(fp, filename, start, n_ret)
358 FILE *fp;
359 char *filename;
360 int start;
361 node **n_ret;
362{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000363 int ret;
364 BGN_SAVE
365 ret = parsefile(fp, filename, &gram, start,
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000366 (char *)0, (char *)0, n_ret);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000367 END_SAVE
368 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000369}
370
371/* Simplified interface to parsestring */
372
373int
374parse_string(str, start, n_ret)
375 char *str;
376 int start;
377 node **n_ret;
378{
379 int err = parsestring(str, &gram, start, n_ret);
380 /* Don't confuse early end of string with early end of input */
381 if (err == E_EOF)
382 err = E_SYNTAX;
383 return err;
384}
385
386/* Print fatal error message and abort */
387
388void
389fatal(msg)
390 char *msg;
391{
392 fprintf(stderr, "Fatal error: %s\n", msg);
393 abort();
394}
395
396/* Clean up and exit */
397
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000398#ifdef USE_THREAD
399extern int threads_started;
400#endif
401
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000402static void
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000403cleanup()
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000404{
Guido van Rossum59bff391992-09-03 20:28:00 +0000405 object *exitfunc = sysget("exitfunc");
406
407 if (exitfunc) {
408 object *arg;
409 object *res;
410 sysset("exitfunc", (object *)NULL);
411 arg = newtupleobject(0);
412 if (arg == NULL)
413 res = NULL;
414 else {
415 res = call_object(exitfunc, arg);
416 DECREF(arg);
417 }
418 if (res == NULL) {
419 fprintf(stderr, "Error in sys.exitfunc:\n");
420 print_error();
421 }
422 }
423
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000424 flushline();
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000425}
426
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000427#ifdef COUNT_ALLOCS
428extern void dump_counts PROTO((void));
429#endif
430
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000431void
432goaway(sts)
433 int sts;
434{
435 cleanup();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000436
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000437#ifdef COUNT_ALLOCS
438 dump_counts();
439#endif
440
Guido van Rossumff4949e1992-08-05 19:58:53 +0000441#ifdef USE_THREAD
442
443 /* Other threads may still be active, so skip most of the
444 cleanup actions usually done (these are mostly for
445 debugging anyway). */
446
Guido van Rossumdf72a651992-08-12 15:27:32 +0000447 (void) save_thread();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000448 donecalls();
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000449 if (threads_started)
450 _exit_prog(sts);
451 else
452 exit_prog(sts);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000453
454#else /* USE_THREAD */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000455
456 /* XXX Call doneimport() before donecalls(), since donecalls()
457 calls wdone(), and doneimport() may close windows */
458 doneimport();
459 donecalls();
460
461 err_clear();
462
463#ifdef REF_DEBUG
464 fprintf(stderr, "[%ld refs]\n", ref_total);
465#endif
466
467#ifdef TRACE_REFS
468 if (askyesno("Print left references?")) {
469 printrefs(stderr);
470 }
471#endif /* TRACE_REFS */
472
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000473 exit(sts);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000474#endif /* USE_THREAD */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000475 /*NOTREACHED*/
476}
477
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000478#ifdef HANDLE_SIGNALS
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000479static SIGTYPE
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000480sighandler(sig)
481 int sig;
482{
483 signal(sig, SIG_DFL); /* Don't catch recursive signals */
484 cleanup(); /* Do essential clean-up */
485 kill(getpid(), sig); /* Pretend the signal killed us */
486 /*NOTREACHED*/
487}
488#endif
489
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000490static void
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000491initsigs()
492{
493 initintr();
494#ifdef HANDLE_SIGNALS
495 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
496 signal(SIGHUP, sighandler);
497 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
498 signal(SIGTERM, sighandler);
499#endif
500}
501
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000502#ifdef TRACE_REFS
503/* Ask a yes/no question */
504
Guido van Rossum59bff391992-09-03 20:28:00 +0000505int
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000506askyesno(prompt)
507 char *prompt;
508{
509 char buf[256];
510
511 printf("%s [ny] ", prompt);
512 if (fgets(buf, sizeof buf, stdin) == NULL)
513 return 0;
514 return buf[0] == 'y' || buf[0] == 'Y';
515}
516#endif
517
518#ifdef applec /* MPW (also usable for Think C 3.0) */
519
520/* Check for file descriptor connected to interactive device.
521 Pretend that stdin is always interactive, other files never. */
522
523int
524isatty(fd)
525 int fd;
526{
527 return fd == fileno(stdin);
528}
529
530#endif