blob: 90a429408c00a93c682db21d2d0069c909403852 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
2Copyright 1991, 1992 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
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 Rossum1984f1e1992-08-04 12:41:02 +000041extern char *getpythonpath();
42
43extern grammar gram; /* From graminit.c */
44
45int debugging; /* Needed by parser.c */
46int verbose; /* Needed by import.c */
47
48/* Initialize all */
49
50void
51initall()
52{
53 static int inited;
54
55 if (inited)
56 return;
57 inited = 1;
58
59 initimport();
60
61 /* Modules 'builtin' and 'sys' are initialized here,
62 they are needed by random bits of the interpreter.
63 All other modules are optional and are initialized
64 when they are first imported. */
65
66 initbuiltin(); /* Also initializes builtin exceptions */
67 initsys();
68
69 initcalls(); /* Configuration-dependent initializations */
70
71 initintr(); /* For intrcheck() */
72
73 setpythonpath(getpythonpath());
74}
75
76/* Parse input from a file and execute it */
77
78int
79run(fp, filename)
80 FILE *fp;
81 char *filename;
82{
83 if (filename == NULL)
84 filename = "???";
85 if (isatty((int)fileno(fp)))
86 return run_tty_loop(fp, filename);
87 else
88 return run_script(fp, filename);
89}
90
91int
92run_tty_loop(fp, filename)
93 FILE *fp;
94 char *filename;
95{
96 object *v;
97 int ret;
98 v = sysget("ps1");
99 if (v == NULL) {
100 sysset("ps1", v = newstringobject(">>> "));
101 XDECREF(v);
102 }
103 v = sysget("ps2");
104 if (v == NULL) {
105 sysset("ps2", v = newstringobject("... "));
106 XDECREF(v);
107 }
108 for (;;) {
109 ret = run_tty_1(fp, filename);
110#ifdef REF_DEBUG
111 fprintf(stderr, "[%ld refs]\n", ref_total);
112#endif
113 if (ret == E_EOF)
114 return 0;
115 /*
116 if (ret == E_NOMEM)
117 return -1;
118 */
119 }
120}
121
122int
123run_tty_1(fp, filename)
124 FILE *fp;
125 char *filename;
126{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000127 object *m, *d, *v, *w;
128 node *n;
129 char *ps1, *ps2;
130 int err;
131 v = sysget("ps1");
132 w = sysget("ps2");
133 if (v != NULL && is_stringobject(v)) {
134 INCREF(v);
135 ps1 = getstringvalue(v);
136 }
137 else {
138 v = NULL;
139 ps1 = "";
140 }
141 if (w != NULL && is_stringobject(w)) {
142 INCREF(w);
143 ps2 = getstringvalue(w);
144 }
145 else {
146 w = NULL;
147 ps2 = "";
148 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000149 BGN_SAVE
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000150 err = parsefile(fp, filename, &gram, single_input, ps1, ps2, &n);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000151 END_SAVE
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000152 XDECREF(v);
153 XDECREF(w);
154 if (err == E_EOF)
155 return E_EOF;
156 if (err != E_DONE) {
157 err_input(err);
158 print_error();
159 return err;
160 }
161 m = add_module("__main__");
162 if (m == NULL)
163 return -1;
164 d = getmoduledict(m);
165 v = run_node(n, filename, d, d);
166 flushline();
167 if (v == NULL) {
168 print_error();
169 return -1;
170 }
171 DECREF(v);
172 return 0;
173}
174
175int
176run_script(fp, filename)
177 FILE *fp;
178 char *filename;
179{
180 object *m, *d, *v;
181 m = add_module("__main__");
182 if (m == NULL)
183 return -1;
184 d = getmoduledict(m);
185 v = run_file(fp, filename, file_input, d, d);
186 flushline();
187 if (v == NULL) {
188 print_error();
189 return -1;
190 }
191 DECREF(v);
192 return 0;
193}
194
195int
196run_command(command)
197 char *command;
198{
199 object *m, *d, *v;
200 m = add_module("__main__");
201 if (m == NULL)
202 return -1;
203 d = getmoduledict(m);
204 v = run_string(command, file_input, d, d);
205 flushline();
206 if (v == NULL) {
207 print_error();
208 return -1;
209 }
210 DECREF(v);
211 return 0;
212}
213
214void
215print_error()
216{
Guido van Rossum3165fe61992-09-25 21:59:05 +0000217 object *exception, *v, *f;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218 err_get(&exception, &v);
219 if (exception == SystemExit) {
220 if (v == NULL || v == None)
221 goaway(0);
222 if (is_intobject(v))
223 goaway((int)getintvalue(v));
224 else {
Guido van Rossum3165fe61992-09-25 21:59:05 +0000225 /* OK to use real stderr here */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226 printobject(v, stderr, PRINT_RAW);
227 fprintf(stderr, "\n");
228 goaway(1);
229 }
230 }
231 sysset("last_type", exception);
232 sysset("last_value", v);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000233 f = sysget("stderr");
234 if (f == NULL)
235 fprintf(stderr, "lost sys.stderr\n");
236 else {
237 if (writeobject(exception, f, PRINT_RAW) != 0)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000238 err_clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +0000239 if (v != NULL && v != None) {
240 writestring(": ", f);
241 if (writeobject(v, f, PRINT_RAW) != 0)
242 err_clear();
243 }
244 writestring("\n", f);
245 printtraceback(f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000246 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000247 XDECREF(exception);
248 XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000249}
250
251object *
252run_string(str, start, globals, locals)
253 char *str;
254 int start;
255 /*dict*/object *globals, *locals;
256{
257 node *n;
258 int err;
259 err = parse_string(str, start, &n);
260 return run_err_node(err, n, "<string>", globals, locals);
261}
262
263object *
264run_file(fp, filename, start, globals, locals)
265 FILE *fp;
266 char *filename;
267 int start;
268 /*dict*/object *globals, *locals;
269{
270 node *n;
271 int err;
272 err = parse_file(fp, filename, start, &n);
273 return run_err_node(err, n, filename, globals, locals);
274}
275
276object *
277run_err_node(err, n, filename, globals, locals)
278 int err;
279 node *n;
280 char *filename;
281 /*dict*/object *globals, *locals;
282{
283 if (err != E_DONE) {
284 err_input(err);
285 return NULL;
286 }
287 return run_node(n, filename, globals, locals);
288}
289
290object *
291run_node(n, filename, globals, locals)
292 node *n;
293 char *filename;
294 /*dict*/object *globals, *locals;
295{
296 if (globals == NULL) {
297 globals = getglobals();
298 if (locals == NULL)
299 locals = getlocals();
300 }
301 else {
302 if (locals == NULL)
303 locals = globals;
304 }
305 return eval_node(n, filename, globals, locals);
306}
307
308object *
309eval_node(n, filename, globals, locals)
310 node *n;
311 char *filename;
312 object *globals;
313 object *locals;
314{
315 codeobject *co;
316 object *v;
317 co = compile(n, filename);
318 freetree(n);
319 if (co == NULL)
320 return NULL;
321 v = eval_code(co, globals, locals, (object *)NULL);
322 DECREF(co);
323 return v;
324}
325
326/* Simplified interface to parsefile */
327
328int
329parse_file(fp, filename, start, n_ret)
330 FILE *fp;
331 char *filename;
332 int start;
333 node **n_ret;
334{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000335 int ret;
336 BGN_SAVE
337 ret = parsefile(fp, filename, &gram, start,
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000338 (char *)0, (char *)0, n_ret);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000339 END_SAVE
340 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000341}
342
343/* Simplified interface to parsestring */
344
345int
346parse_string(str, start, n_ret)
347 char *str;
348 int start;
349 node **n_ret;
350{
351 int err = parsestring(str, &gram, start, n_ret);
352 /* Don't confuse early end of string with early end of input */
353 if (err == E_EOF)
354 err = E_SYNTAX;
355 return err;
356}
357
358/* Print fatal error message and abort */
359
360void
361fatal(msg)
362 char *msg;
363{
364 fprintf(stderr, "Fatal error: %s\n", msg);
365 abort();
366}
367
368/* Clean up and exit */
369
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000370#ifdef USE_THREAD
371extern int threads_started;
372#endif
373
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000374void
375goaway(sts)
376 int sts;
377{
Guido van Rossum59bff391992-09-03 20:28:00 +0000378 object *exitfunc = sysget("exitfunc");
379
380 if (exitfunc) {
381 object *arg;
382 object *res;
383 sysset("exitfunc", (object *)NULL);
384 arg = newtupleobject(0);
385 if (arg == NULL)
386 res = NULL;
387 else {
388 res = call_object(exitfunc, arg);
389 DECREF(arg);
390 }
391 if (res == NULL) {
392 fprintf(stderr, "Error in sys.exitfunc:\n");
393 print_error();
394 }
395 }
396
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000397 flushline();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000398
399#ifdef USE_THREAD
400
401 /* Other threads may still be active, so skip most of the
402 cleanup actions usually done (these are mostly for
403 debugging anyway). */
404
Guido van Rossumdf72a651992-08-12 15:27:32 +0000405 (void) save_thread();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000406 donecalls();
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000407 if (threads_started)
408 _exit_prog(sts);
409 else
410 exit_prog(sts);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000411
412#else /* USE_THREAD */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000413
414 /* XXX Call doneimport() before donecalls(), since donecalls()
415 calls wdone(), and doneimport() may close windows */
416 doneimport();
417 donecalls();
418
419 err_clear();
420
421#ifdef REF_DEBUG
422 fprintf(stderr, "[%ld refs]\n", ref_total);
423#endif
424
425#ifdef TRACE_REFS
426 if (askyesno("Print left references?")) {
427 printrefs(stderr);
428 }
429#endif /* TRACE_REFS */
430
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000431 exit(sts);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000432#endif /* USE_THREAD */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000433 /*NOTREACHED*/
434}
435
436#ifdef TRACE_REFS
437/* Ask a yes/no question */
438
Guido van Rossum59bff391992-09-03 20:28:00 +0000439int
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000440askyesno(prompt)
441 char *prompt;
442{
443 char buf[256];
444
445 printf("%s [ny] ", prompt);
446 if (fgets(buf, sizeof buf, stdin) == NULL)
447 return 0;
448 return buf[0] == 'y' || buf[0] == 'Y';
449}
450#endif
451
452#ifdef applec /* MPW (also usable for Think C 3.0) */
453
454/* Check for file descriptor connected to interactive device.
455 Pretend that stdin is always interactive, other files never. */
456
457int
458isatty(fd)
459 int fd;
460{
461 return fd == fileno(stdin);
462}
463
464#endif