blob: a25cbba848f1064cd1be66c2bd4eba1a1c29c8ea [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The 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"
Guido van Rossum6135a871995-01-09 17:53:26 +000035#include "bltinmodule.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000036#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000037#include "eval.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000038#include "ceval.h"
39#include "pythonrun.h"
40#include "import.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000041#include "marshal.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000042
Guido van Rossuma110aa61994-08-29 12:50:44 +000043#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000044#include <signal.h>
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000045#endif
46
Guido van Rossum1984f1e1992-08-04 12:41:02 +000047extern char *getpythonpath();
48
49extern grammar gram; /* From graminit.c */
50
Guido van Rossumb73cc041993-11-01 16:28:59 +000051/* Forward */
Guido van Rossum6135a871995-01-09 17:53:26 +000052static void initmain PROTO((void));
Guido van Rossuma110aa61994-08-29 12:50:44 +000053static object *run_err_node PROTO((node *n, char *filename,
Guido van Rossumb73cc041993-11-01 16:28:59 +000054 object *globals, object *locals));
55static object *run_node PROTO((node *n, char *filename,
56 object *globals, object *locals));
Guido van Rossuma110aa61994-08-29 12:50:44 +000057static void err_input PROTO((perrdetail *));
Guido van Rossumb376a4a1993-11-23 17:53:17 +000058static void initsigs PROTO((void));
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000059
Guido van Rossum1984f1e1992-08-04 12:41:02 +000060int debugging; /* Needed by parser.c */
61int verbose; /* Needed by import.c */
Guido van Rossuma110aa61994-08-29 12:50:44 +000062int suppress_print; /* Needed by ceval.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000063
64/* Initialize all */
65
66void
67initall()
68{
69 static int inited;
70
71 if (inited)
72 return;
73 inited = 1;
74
75 initimport();
76
Guido van Rossuma110aa61994-08-29 12:50:44 +000077 /* Modules '__builtin__' and 'sys' are initialized here,
Guido van Rossum1984f1e1992-08-04 12:41:02 +000078 they are needed by random bits of the interpreter.
79 All other modules are optional and are initialized
80 when they are first imported. */
81
82 initbuiltin(); /* Also initializes builtin exceptions */
83 initsys();
Guido van Rossum1984f1e1992-08-04 12:41:02 +000084
85 setpythonpath(getpythonpath());
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000086
87 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum6135a871995-01-09 17:53:26 +000088
89 initmain();
90}
91
92/* Create __main__ module */
93
94static void
95initmain()
96{
97 object *m, *d;
98 m = add_module("__main__");
99 if (m == NULL)
100 fatal("can't create __main__ module");
101 d = getmoduledict(m);
102 if (dictlookup(d, "__builtins__") == NULL) {
103 if (dictinsert(d, "__builtins__", getbuiltindict()))
104 fatal("can't add __builtins__ to __main__");
105 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000106}
107
108/* Parse input from a file and execute it */
109
110int
111run(fp, filename)
112 FILE *fp;
113 char *filename;
114{
115 if (filename == NULL)
116 filename = "???";
117 if (isatty((int)fileno(fp)))
118 return run_tty_loop(fp, filename);
119 else
120 return run_script(fp, filename);
121}
122
123int
124run_tty_loop(fp, filename)
125 FILE *fp;
126 char *filename;
127{
128 object *v;
129 int ret;
130 v = sysget("ps1");
131 if (v == NULL) {
132 sysset("ps1", v = newstringobject(">>> "));
133 XDECREF(v);
134 }
135 v = sysget("ps2");
136 if (v == NULL) {
137 sysset("ps2", v = newstringobject("... "));
138 XDECREF(v);
139 }
140 for (;;) {
141 ret = run_tty_1(fp, filename);
142#ifdef REF_DEBUG
143 fprintf(stderr, "[%ld refs]\n", ref_total);
144#endif
145 if (ret == E_EOF)
146 return 0;
147 /*
148 if (ret == E_NOMEM)
149 return -1;
150 */
151 }
152}
153
154int
155run_tty_1(fp, filename)
156 FILE *fp;
157 char *filename;
158{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000159 object *m, *d, *v, *w;
160 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000161 perrdetail err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000162 char *ps1, *ps2;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000163 v = sysget("ps1");
164 w = sysget("ps2");
165 if (v != NULL && is_stringobject(v)) {
166 INCREF(v);
167 ps1 = getstringvalue(v);
168 }
169 else {
170 v = NULL;
171 ps1 = "";
172 }
173 if (w != NULL && is_stringobject(w)) {
174 INCREF(w);
175 ps2 = getstringvalue(w);
176 }
177 else {
178 w = NULL;
179 ps2 = "";
180 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000181 BGN_SAVE
Guido van Rossuma110aa61994-08-29 12:50:44 +0000182 n = parsefile(fp, filename, &gram, single_input, ps1, ps2, &err);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000183 END_SAVE
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000184 XDECREF(v);
185 XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000186 if (n == NULL) {
187 if (err.error == E_EOF) {
188 if (err.text)
189 free(err.text);
190 return E_EOF;
191 }
192 err_input(&err);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000193 print_error();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000194 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000195 }
196 m = add_module("__main__");
197 if (m == NULL)
198 return -1;
199 d = getmoduledict(m);
200 v = run_node(n, filename, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000201 if (v == NULL) {
202 print_error();
203 return -1;
204 }
205 DECREF(v);
Guido van Rossum6d023c91995-01-04 19:12:13 +0000206 flushline();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000207 return 0;
208}
209
210int
211run_script(fp, filename)
212 FILE *fp;
213 char *filename;
214{
215 object *m, *d, *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000216 char *ext;
217
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218 m = add_module("__main__");
219 if (m == NULL)
220 return -1;
221 d = getmoduledict(m);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000222 ext = filename + strlen(filename) - 4;
223 if ( strcmp(ext, ".pyc") == 0 ) {
224 /* Try to run a pyc file. First, re-open in binary */
Jack Jansene00637b1994-12-14 12:58:37 +0000225 /* Don't close, done in main: fclose(fp); */
Guido van Rossumfdef2711994-09-14 13:31:04 +0000226 if( (fp = fopen(filename, "rb")) == NULL ) {
227 fprintf(stderr, "python: Can't reopen .pyc file\n");
228 return -1;
229 }
230 v = run_pyc_file(fp, filename, d, d);
231 } else {
232 v = run_file(fp, filename, file_input, d, d);
233 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000234 if (v == NULL) {
235 print_error();
236 return -1;
237 }
238 DECREF(v);
Guido van Rossum6d023c91995-01-04 19:12:13 +0000239 flushline();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000240 return 0;
241}
242
243int
244run_command(command)
245 char *command;
246{
247 object *m, *d, *v;
248 m = add_module("__main__");
249 if (m == NULL)
250 return -1;
251 d = getmoduledict(m);
252 v = run_string(command, file_input, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000253 if (v == NULL) {
254 print_error();
255 return -1;
256 }
257 DECREF(v);
Guido van Rossum6d023c91995-01-04 19:12:13 +0000258 flushline();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000259 return 0;
260}
261
262void
263print_error()
264{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000265 object *exception, *v, *tb, *f;
266 err_fetch(&exception, &v, &tb);
Guido van Rossum6d023c91995-01-04 19:12:13 +0000267 flushline();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000268 if (exception == NULL)
269 fatal("print_error called but no exception");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000270 if (exception == SystemExit) {
271 if (v == NULL || v == None)
272 goaway(0);
273 if (is_intobject(v))
274 goaway((int)getintvalue(v));
275 else {
Guido van Rossum3165fe61992-09-25 21:59:05 +0000276 /* OK to use real stderr here */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000277 printobject(v, stderr, PRINT_RAW);
278 fprintf(stderr, "\n");
279 goaway(1);
280 }
281 }
282 sysset("last_type", exception);
283 sysset("last_value", v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000284 sysset("last_traceback", tb);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000285 f = sysget("stderr");
286 if (f == NULL)
287 fprintf(stderr, "lost sys.stderr\n");
288 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000289 tb_print(tb, f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000290 if (exception == SyntaxError) {
291 object *message;
292 char *filename, *text;
293 int lineno, offset;
294 if (!getargs(v, "(O(ziiz))", &message,
295 &filename, &lineno, &offset, &text))
296 err_clear();
297 else {
298 char buf[10];
299 writestring(" File \"", f);
300 if (filename == NULL)
301 writestring("<string>", f);
302 else
303 writestring(filename, f);
304 writestring("\", line ", f);
305 sprintf(buf, "%d", lineno);
306 writestring(buf, f);
307 writestring("\n", f);
308 if (text != NULL) {
Guido van Rossum798199d1994-09-19 08:08:50 +0000309 char *nl;
310 if (offset > 0 &&
311 offset == strlen(text))
312 offset--;
313 for (;;) {
314 nl = strchr(text, '\n');
315 if (nl == NULL ||
316 nl-text >= offset)
317 break;
318 offset -= (nl+1-text);
319 text = nl+1;
320 }
Guido van Rossuma110aa61994-08-29 12:50:44 +0000321 while (*text == ' ' || *text == '\t') {
322 text++;
323 offset--;
324 }
325 writestring(" ", f);
326 writestring(text, f);
327 if (*text == '\0' ||
328 text[strlen(text)-1] != '\n')
329 writestring("\n", f);
330 writestring(" ", f);
331 offset--;
332 while (offset > 0) {
333 writestring(" ", f);
334 offset--;
335 }
336 writestring("^\n", f);
337 }
338 INCREF(message);
339 DECREF(v);
340 v = message;
341 }
342 }
Guido van Rossum3165fe61992-09-25 21:59:05 +0000343 if (writeobject(exception, f, PRINT_RAW) != 0)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000344 err_clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +0000345 if (v != NULL && v != None) {
346 writestring(": ", f);
347 if (writeobject(v, f, PRINT_RAW) != 0)
348 err_clear();
349 }
350 writestring("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000351 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000352 XDECREF(exception);
353 XDECREF(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000354 XDECREF(tb);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000355}
356
357object *
358run_string(str, start, globals, locals)
359 char *str;
360 int start;
Guido van Rossum5b722181993-03-30 17:46:03 +0000361 object *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000362{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000363 return run_err_node(parse_string(str, start),
364 "<string>", globals, locals);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000365}
366
367object *
368run_file(fp, filename, start, globals, locals)
369 FILE *fp;
370 char *filename;
371 int start;
Guido van Rossum5b722181993-03-30 17:46:03 +0000372 object *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000373{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000374 return run_err_node(parse_file(fp, filename, start),
375 filename, globals, locals);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000376}
377
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000378static object *
Guido van Rossuma110aa61994-08-29 12:50:44 +0000379run_err_node(n, filename, globals, locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000380 node *n;
381 char *filename;
Guido van Rossum5b722181993-03-30 17:46:03 +0000382 object *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000383{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000384 if (n == NULL)
385 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000386 return run_node(n, filename, globals, locals);
387}
388
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000389static object *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000390run_node(n, filename, globals, locals)
391 node *n;
392 char *filename;
Guido van Rossum5b722181993-03-30 17:46:03 +0000393 object *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000394{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000395 codeobject *co;
396 object *v;
397 co = compile(n, filename);
398 freetree(n);
399 if (co == NULL)
400 return NULL;
Guido van Rossum81daa321993-05-20 14:24:46 +0000401 v = eval_code(co, globals, locals, (object *)NULL, (object *)NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000402 DECREF(co);
403 return v;
404}
405
Guido van Rossum5b722181993-03-30 17:46:03 +0000406object *
Guido van Rossumfdef2711994-09-14 13:31:04 +0000407run_pyc_file(fp, filename, globals, locals)
408 FILE *fp;
409 char *filename;
410 object *globals, *locals;
411{
412 codeobject *co;
413 object *v;
414 long magic;
415 long get_pyc_magic();
416
417 magic = rd_long(fp);
418 if (magic != get_pyc_magic()) {
419 err_setstr(RuntimeError,
420 "Bad magic number in .pyc file");
421 return NULL;
422 }
423 (void) rd_long(fp);
424 v = rd_object(fp);
425 fclose(fp);
426 if (v == NULL || !is_codeobject(v)) {
427 XDECREF(v);
428 err_setstr(RuntimeError,
429 "Bad code object in .pyc file");
430 return NULL;
431 }
432 co = (codeobject *)v;
433 v = eval_code(co, globals, locals, (object *)NULL, (object *)NULL);
434 DECREF(co);
435 return v;
436}
437
438object *
Guido van Rossum5b722181993-03-30 17:46:03 +0000439compile_string(str, filename, start)
440 char *str;
441 char *filename;
442 int start;
443{
444 node *n;
Guido van Rossum5b722181993-03-30 17:46:03 +0000445 codeobject *co;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000446 n = parse_string(str, start);
447 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +0000448 return NULL;
Guido van Rossum5b722181993-03-30 17:46:03 +0000449 co = compile(n, filename);
450 freetree(n);
451 return (object *)co;
452}
453
Guido van Rossuma110aa61994-08-29 12:50:44 +0000454/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000455
Guido van Rossuma110aa61994-08-29 12:50:44 +0000456node *
457parse_file(fp, filename, start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000458 FILE *fp;
459 char *filename;
460 int start;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000461{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000462 node *n;
463 perrdetail err;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000464 BGN_SAVE
Guido van Rossuma110aa61994-08-29 12:50:44 +0000465 n = parsefile(fp, filename, &gram, start,
466 (char *)0, (char *)0, &err);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000467 END_SAVE
Guido van Rossuma110aa61994-08-29 12:50:44 +0000468 if (n == NULL)
469 err_input(&err);
470 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000471}
472
Guido van Rossuma110aa61994-08-29 12:50:44 +0000473/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000474
Guido van Rossuma110aa61994-08-29 12:50:44 +0000475node *
476parse_string(str, start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000477 char *str;
478 int start;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000479{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000480 node *n;
481 perrdetail err;
482 n = parsestring(str, &gram, start, &err);
483 if (n == NULL)
484 err_input(&err);
485 return n;
486}
487
488/* Set the error appropriate to the given input error code (see errcode.h) */
489
490static void
491err_input(err)
492 perrdetail *err;
493{
494 object *v, *w;
495 char *msg = NULL;
496 v = mkvalue("(ziiz)", err->filename,
497 err->lineno, err->offset, err->text);
498 if (err->text != NULL) {
499 free(err->text);
500 err->text = NULL;
501 }
502 switch (err->error) {
503 case E_SYNTAX:
504 msg = "invalid syntax";
505 break;
506 case E_TOKEN:
507 msg = "invalid token";
508
509 break;
510 case E_INTR:
511 err_set(KeyboardInterrupt);
512 return;
513 case E_NOMEM:
514 err_nomem();
515 return;
516 case E_EOF:
517 msg = "unexpected EOF while parsing";
518 break;
519 default:
520 fprintf(stderr, "error=%d\n", err->error);
521 msg = "unknown parsing error";
522 break;
523 }
524 w = mkvalue("(sO)", msg, v);
525 XDECREF(v);
526 err_setval(SyntaxError, w);
527 XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000528}
529
530/* Print fatal error message and abort */
531
532void
533fatal(msg)
534 char *msg;
535{
Guido van Rossum83dd6c31994-09-29 09:38:33 +0000536 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000537 abort();
538}
539
540/* Clean up and exit */
541
Guido van Rossuma110aa61994-08-29 12:50:44 +0000542#ifdef WITH_THREAD
543#include "thread.h"
544int threads_started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000545#endif
546
Guido van Rossum1662dd51994-09-07 14:38:28 +0000547#define NEXITFUNCS 32
548static void (*exitfuncs[NEXITFUNCS])();
549static int nexitfuncs = 0;
550
551int Py_AtExit(func)
552 void (*func) PROTO((void));
553{
554 if (nexitfuncs >= NEXITFUNCS)
555 return -1;
556 exitfuncs[nexitfuncs++] = func;
557 return 0;
558}
559
Guido van Rossuma110aa61994-08-29 12:50:44 +0000560void
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000561cleanup()
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000562{
Guido van Rossum59bff391992-09-03 20:28:00 +0000563 object *exitfunc = sysget("exitfunc");
564
565 if (exitfunc) {
566 object *arg;
567 object *res;
568 sysset("exitfunc", (object *)NULL);
569 arg = newtupleobject(0);
570 if (arg == NULL)
571 res = NULL;
572 else {
573 res = call_object(exitfunc, arg);
574 DECREF(arg);
575 }
576 if (res == NULL) {
577 fprintf(stderr, "Error in sys.exitfunc:\n");
578 print_error();
579 }
580 }
581
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000582 flushline();
Guido van Rossum1662dd51994-09-07 14:38:28 +0000583
584 while (nexitfuncs > 0)
585 (*exitfuncs[--nexitfuncs])();
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000586}
587
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000588#ifdef COUNT_ALLOCS
589extern void dump_counts PROTO((void));
590#endif
591
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000592void
593goaway(sts)
594 int sts;
595{
596 cleanup();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000597
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000598#ifdef COUNT_ALLOCS
599 dump_counts();
600#endif
601
Guido van Rossuma110aa61994-08-29 12:50:44 +0000602#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000603
604 /* Other threads may still be active, so skip most of the
605 cleanup actions usually done (these are mostly for
606 debugging anyway). */
607
Guido van Rossumdf72a651992-08-12 15:27:32 +0000608 (void) save_thread();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000609#ifndef NO_EXIT_PROG
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000610 if (threads_started)
611 _exit_prog(sts);
612 else
613 exit_prog(sts);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000614#else /* !NO_EXIT_PROG */
615 if (threads_started)
616 _exit(sts);
617 else
618 exit(sts);
619#endif /* !NO_EXIT_PROG */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000620
Guido van Rossuma110aa61994-08-29 12:50:44 +0000621#else /* WITH_THREAD */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000622
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000623 doneimport();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000624
625 err_clear();
626
627#ifdef REF_DEBUG
628 fprintf(stderr, "[%ld refs]\n", ref_total);
629#endif
630
631#ifdef TRACE_REFS
632 if (askyesno("Print left references?")) {
633 printrefs(stderr);
634 }
635#endif /* TRACE_REFS */
636
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000637 exit(sts);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000638#endif /* WITH_THREAD */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000639 /*NOTREACHED*/
640}
641
Guido van Rossuma110aa61994-08-29 12:50:44 +0000642#ifdef HAVE_SIGNAL_H
643static RETSIGTYPE
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000644sighandler(sig)
645 int sig;
646{
647 signal(sig, SIG_DFL); /* Don't catch recursive signals */
648 cleanup(); /* Do essential clean-up */
Guido van Rossuma110aa61994-08-29 12:50:44 +0000649#ifdef HAVE_GETPID
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000650 kill(getpid(), sig); /* Pretend the signal killed us */
Guido van Rossuma110aa61994-08-29 12:50:44 +0000651#else
652 exit(1);
653#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000654 /*NOTREACHED*/
655}
656#endif
657
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000658static void
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000659initsigs()
660{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000661 RETSIGTYPE (*t)();
662#ifdef HAVE_SIGNAL_H
663#ifdef SIGPIPE
664 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000665#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +0000666#ifdef SIGHUP
667 t = signal(SIGHUP, SIG_IGN);
668 if (t == SIG_DFL)
669 signal(SIGHUP, sighandler);
670 else
671 signal(SIGHUP, t);
672#endif
673#ifdef SIGTERM
674 t = signal(SIGTERM, SIG_IGN);
675 if (t == SIG_DFL)
676 signal(SIGTERM, sighandler);
677 else
678 signal(SIGTERM, t);
679#endif
680#endif /* HAVE_SIGNAL_H */
681 initintr(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000682}
683
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000684#ifdef TRACE_REFS
685/* Ask a yes/no question */
686
Guido van Rossum59bff391992-09-03 20:28:00 +0000687int
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000688askyesno(prompt)
689 char *prompt;
690{
691 char buf[256];
692
693 printf("%s [ny] ", prompt);
694 if (fgets(buf, sizeof buf, stdin) == NULL)
695 return 0;
696 return buf[0] == 'y' || buf[0] == 'Y';
697}
698#endif
699
Guido van Rossuma110aa61994-08-29 12:50:44 +0000700#ifdef MPW
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000701
702/* Check for file descriptor connected to interactive device.
703 Pretend that stdin is always interactive, other files never. */
704
705int
706isatty(fd)
707 int fd;
708{
709 return fd == fileno(stdin);
710}
711
712#endif