blob: c8921da2fda1de92a4ec194cfee78bf5ef422a16 [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{
217 object *exception, *v;
218 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 {
225 printobject(v, stderr, PRINT_RAW);
226 fprintf(stderr, "\n");
227 goaway(1);
228 }
229 }
230 sysset("last_type", exception);
231 sysset("last_value", v);
232 if (printobject(exception, stderr, PRINT_RAW) != 0)
233 err_clear();
234 if (v != NULL && v != None) {
235 fprintf(stderr, ": ");
236 if (printobject(v, stderr, PRINT_RAW) != 0)
237 err_clear();
238 }
239 fprintf(stderr, "\n");
240 XDECREF(exception);
241 XDECREF(v);
242 printtraceback(stderr);
243}
244
245object *
246run_string(str, start, globals, locals)
247 char *str;
248 int start;
249 /*dict*/object *globals, *locals;
250{
251 node *n;
252 int err;
253 err = parse_string(str, start, &n);
254 return run_err_node(err, n, "<string>", globals, locals);
255}
256
257object *
258run_file(fp, filename, start, globals, locals)
259 FILE *fp;
260 char *filename;
261 int start;
262 /*dict*/object *globals, *locals;
263{
264 node *n;
265 int err;
266 err = parse_file(fp, filename, start, &n);
267 return run_err_node(err, n, filename, globals, locals);
268}
269
270object *
271run_err_node(err, n, filename, globals, locals)
272 int err;
273 node *n;
274 char *filename;
275 /*dict*/object *globals, *locals;
276{
277 if (err != E_DONE) {
278 err_input(err);
279 return NULL;
280 }
281 return run_node(n, filename, globals, locals);
282}
283
284object *
285run_node(n, filename, globals, locals)
286 node *n;
287 char *filename;
288 /*dict*/object *globals, *locals;
289{
290 if (globals == NULL) {
291 globals = getglobals();
292 if (locals == NULL)
293 locals = getlocals();
294 }
295 else {
296 if (locals == NULL)
297 locals = globals;
298 }
299 return eval_node(n, filename, globals, locals);
300}
301
302object *
303eval_node(n, filename, globals, locals)
304 node *n;
305 char *filename;
306 object *globals;
307 object *locals;
308{
309 codeobject *co;
310 object *v;
311 co = compile(n, filename);
312 freetree(n);
313 if (co == NULL)
314 return NULL;
315 v = eval_code(co, globals, locals, (object *)NULL);
316 DECREF(co);
317 return v;
318}
319
320/* Simplified interface to parsefile */
321
322int
323parse_file(fp, filename, start, n_ret)
324 FILE *fp;
325 char *filename;
326 int start;
327 node **n_ret;
328{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000329 int ret;
330 BGN_SAVE
331 ret = parsefile(fp, filename, &gram, start,
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000332 (char *)0, (char *)0, n_ret);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000333 END_SAVE
334 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000335}
336
337/* Simplified interface to parsestring */
338
339int
340parse_string(str, start, n_ret)
341 char *str;
342 int start;
343 node **n_ret;
344{
345 int err = parsestring(str, &gram, start, n_ret);
346 /* Don't confuse early end of string with early end of input */
347 if (err == E_EOF)
348 err = E_SYNTAX;
349 return err;
350}
351
352/* Print fatal error message and abort */
353
354void
355fatal(msg)
356 char *msg;
357{
358 fprintf(stderr, "Fatal error: %s\n", msg);
359 abort();
360}
361
362/* Clean up and exit */
363
364void
365goaway(sts)
366 int sts;
367{
368 flushline();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000369
370#ifdef USE_THREAD
371
372 /* Other threads may still be active, so skip most of the
373 cleanup actions usually done (these are mostly for
374 debugging anyway). */
375
376 (void *) save_thread();
377 donecalls();
378 exit_prog(sts);
379
380#else /* USE_THREAD */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000381
382 /* XXX Call doneimport() before donecalls(), since donecalls()
383 calls wdone(), and doneimport() may close windows */
384 doneimport();
385 donecalls();
386
387 err_clear();
388
389#ifdef REF_DEBUG
390 fprintf(stderr, "[%ld refs]\n", ref_total);
391#endif
392
393#ifdef TRACE_REFS
394 if (askyesno("Print left references?")) {
395 printrefs(stderr);
396 }
397#endif /* TRACE_REFS */
398
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000399 exit(sts);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000400#endif /* USE_THREAD */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000401 /*NOTREACHED*/
402}
403
404#ifdef TRACE_REFS
405/* Ask a yes/no question */
406
407static int
408askyesno(prompt)
409 char *prompt;
410{
411 char buf[256];
412
413 printf("%s [ny] ", prompt);
414 if (fgets(buf, sizeof buf, stdin) == NULL)
415 return 0;
416 return buf[0] == 'y' || buf[0] == 'Y';
417}
418#endif
419
420#ifdef applec /* MPW (also usable for Think C 3.0) */
421
422/* Check for file descriptor connected to interactive device.
423 Pretend that stdin is always interactive, other files never. */
424
425int
426isatty(fd)
427 int fd;
428{
429 return fd == fileno(stdin);
430}
431
432#endif