1 /***********************************************************
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, The Netherlands.
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Python interpreter top-level routines, including init/exit */
27 #include "allobjects.h"
34 #include "sysmodule.h"
38 #include "pythonrun.h"
45 extern char *getpythonpath();
47 extern grammar gram
; /* From graminit.c */
50 static object
*run_err_node
PROTO((node
*n
, char *filename
,
51 object
*globals
, object
*locals
));
52 static object
*run_node
PROTO((node
*n
, char *filename
,
53 object
*globals
, object
*locals
));
54 static void err_input
PROTO((perrdetail
*));
55 static void initsigs
PROTO((void));
57 int debugging
; /* Needed by parser.c */
58 int verbose
; /* Needed by import.c */
73 /* Modules '__builtin__' and 'sys' are initialized here,
74 they are needed by random bits of the interpreter.
75 All other modules are optional and are initialized
76 when they are first imported. */
78 initbuiltin(); /* Also initializes builtin exceptions */
81 setpythonpath(getpythonpath());
83 initsigs(); /* Signal handling stuff, including initintr() */
86 /* Parse input from a file and execute it */
95 if (isatty((int)fileno(fp
)))
96 return run_tty_loop(fp
, filename
);
98 return run_script(fp
, filename
);
102 run_tty_loop(fp
, filename
)
110 sysset("ps1", v
= newstringobject(">>> "));
115 sysset("ps2", v
= newstringobject("... "));
119 ret
= run_tty_1(fp
, filename
);
121 fprintf(stderr
, "[%ld refs]\n", ref_total
);
133 run_tty_1(fp
, filename
)
137 object
*m
, *d
, *v
, *w
;
144 if (v
!= NULL
&& is_stringobject(v
)) {
146 ps1
= getstringvalue(v
);
152 if (w
!= NULL
&& is_stringobject(w
)) {
154 ps2
= getstringvalue(w
);
161 n
= parsefile(fp
, filename
, &gram
, single_input
, ps1
, ps2
, &err
);
166 if (err
.error
== E_EOF
) {
175 m
= add_module("__main__");
178 d
= getmoduledict(m
);
179 v
= run_node(n
, filename
, d
, d
);
190 run_script(fp
, filename
)
195 m
= add_module("__main__");
198 d
= getmoduledict(m
);
199 v
= run_file(fp
, filename
, file_input
, d
, d
);
214 m
= add_module("__main__");
217 d
= getmoduledict(m
);
218 v
= run_string(command
, file_input
, d
, d
);
231 object
*exception
, *v
, *f
;
232 err_get(&exception
, &v
);
233 if (exception
== NULL
) {
234 fprintf(stderr
, "print_error called but no exception\n");
237 if (exception
== SystemExit
) {
238 if (v
== NULL
|| v
== None
)
241 goaway((int)getintvalue(v
));
243 /* OK to use real stderr here */
244 printobject(v
, stderr
, PRINT_RAW
);
245 fprintf(stderr
, "\n");
249 sysset("last_type", exception
);
250 sysset("last_value", v
);
251 f
= sysget("stderr");
253 fprintf(stderr
, "lost sys.stderr\n");
256 if (exception
== SyntaxError
) {
258 char *filename
, *text
;
260 if (!getargs(v
, "(O(ziiz))", &message
,
261 &filename
, &lineno
, &offset
, &text
))
265 writestring(" File \"", f
);
266 if (filename
== NULL
)
267 writestring("<string>", f
);
269 writestring(filename
, f
);
270 writestring("\", line ", f
);
271 sprintf(buf
, "%d", lineno
);
273 writestring("\n", f
);
275 while (*text
== ' ' || *text
== '\t') {
280 writestring(text
, f
);
282 text
[strlen(text
)-1] != '\n')
283 writestring("\n", f
);
290 writestring("^\n", f
);
295 if (writeobject(exception
, f
, PRINT_RAW
) != 0)
297 if (v
!= NULL
&& v
!= None
) {
298 writestring(": ", f
);
299 if (writeobject(v
, f
, PRINT_RAW
) != 0)
302 writestring("\n", f
);
309 run_string(str
, start
, globals
, locals
)
312 object
*globals
, *locals
;
314 return run_err_node(parse_string(str
, start
),
315 "<string>", globals
, locals
);
319 run_file(fp
, filename
, start
, globals
, locals
)
323 object
*globals
, *locals
;
325 return run_err_node(parse_file(fp
, filename
, start
),
326 filename
, globals
, locals
);
330 run_err_node(n
, filename
, globals
, locals
)
333 object
*globals
, *locals
;
337 return run_node(n
, filename
, globals
, locals
);
341 run_node(n
, filename
, globals
, locals
)
344 object
*globals
, *locals
;
348 co
= compile(n
, filename
);
352 v
= eval_code(co
, globals
, locals
, (object
*)NULL
, (object
*)NULL
);
358 compile_string(str
, filename
, start
)
366 n
= parse_string(str
, start
);
369 co
= compile(n
, filename
);
374 /* Simplified interface to parsefile -- return node or set exception */
377 parse_file(fp
, filename
, start
)
385 n
= parsefile(fp
, filename
, &gram
, start
,
386 (char *)0, (char *)0, &err
);
393 /* Simplified interface to parsestring -- return node or set exception */
396 parse_string(str
, start
)
402 n
= parsestring(str
, &gram
, start
, &err
);
408 /* Set the error appropriate to the given input error code (see errcode.h) */
416 v
= mkvalue("(ziiz)", err
->filename
,
417 err
->lineno
, err
->offset
, err
->text
);
418 if (err
->text
!= NULL
) {
422 switch (err
->error
) {
424 msg
= "invalid syntax";
427 msg
= "invalid token";
431 err_set(KeyboardInterrupt
);
437 msg
= "unexpected EOF while parsing";
440 fprintf(stderr
, "error=%d\n", err
->error
);
441 msg
= "unknown parsing error";
444 w
= mkvalue("(sO)", msg
, v
);
446 err_setval(SyntaxError
, w
);
450 /* Print fatal error message and abort */
456 fprintf(stderr
, "Fatal error: %s\n", msg
);
460 /* Clean up and exit */
463 int threads_started
= 0; /* Set by threadmodule.c and maybe others */
469 object
*exitfunc
= sysget("exitfunc");
474 sysset("exitfunc", (object
*)NULL
);
475 arg
= newtupleobject(0);
479 res
= call_object(exitfunc
, arg
);
483 fprintf(stderr
, "Error in sys.exitfunc:\n");
492 extern void dump_counts
PROTO((void));
507 /* Other threads may still be active, so skip most of the
508 cleanup actions usually done (these are mostly for
509 debugging anyway). */
511 (void) save_thread();
517 #else /* WITH_THREAD */
524 fprintf(stderr
, "[%ld refs]\n", ref_total
);
528 if (askyesno("Print left references?")) {
531 #endif /* TRACE_REFS */
534 #endif /* WITH_THREAD */
543 signal(sig
, SIG_DFL
); /* Don't catch recursive signals */
544 cleanup(); /* Do essential clean-up */
546 kill(getpid(), sig
); /* Pretend the signal killed us */
560 if (signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
561 signal(SIGHUP
, sighandler
);
564 if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
)
565 signal(SIGTERM
, sighandler
);
567 #endif /* HAVE_SIGNAL_H */
571 /* Ask a yes/no question */
579 printf("%s [ny] ", prompt
);
580 if (fgets(buf
, sizeof buf
, stdin
) == NULL
)
582 return buf
[0] == 'y' || buf
[0] == 'Y';
586 #ifdef applec /* MPW (also usable for Think C 3.0) */
588 /* Check for file descriptor connected to interactive device.
589 Pretend that stdin is always interactive, other files never. */
595 return fd
== fileno(stdin
);