Mercurial > cpython-lru_cache-weakref
annotate Python/pythonmain.c @ 547:e198cc98a4fd legacy-trunk
Catch SystemExit and go away if it is caught.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 31 Dec 1991 13:14:48 +0000 |
| parents | 85bd7dac13f3 |
| children | 0614d887d10a |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
| 2 Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The | |
| 3 Netherlands. | |
| 4 | |
| 5 All Rights Reserved | |
| 6 | |
| 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. | |
| 14 | |
| 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. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
| 6 | 25 /* Python interpreter main program */ |
| 26 | |
|
206
3cc54972ab52
Move allobjects.h to front (needed by THINK C precompiled headers)
Guido van Rossum <guido@python.org>
parents:
179
diff
changeset
|
27 #include "allobjects.h" |
|
156
43cabac5023d
Include patchlevel.h (to ensure it is distributed with the rest).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
28 |
|
206
3cc54972ab52
Move allobjects.h to front (needed by THINK C precompiled headers)
Guido van Rossum <guido@python.org>
parents:
179
diff
changeset
|
29 #include "patchlevel.h" |
| 6 | 30 |
| 31 #include "grammar.h" | |
| 32 #include "node.h" | |
| 33 #include "parsetok.h" | |
| 34 #include "graminit.h" | |
| 35 #include "errcode.h" | |
| 36 #include "sysmodule.h" | |
| 86 | 37 #include "compile.h" |
| 38 #include "ceval.h" | |
| 39 #include "pythonrun.h" | |
| 40 #include "import.h" | |
| 41 | |
| 42 extern char *getpythonpath(); | |
| 6 | 43 |
| 44 extern grammar gram; /* From graminit.c */ | |
| 45 | |
| 86 | 46 #ifdef DEBUG |
| 47 int debugging; /* Needed by parser.c */ | |
| 48 #endif | |
| 6 | 49 |
|
282
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
50 /* Interface to getopt(): */ |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
51 extern int optind; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
52 extern char *optarg; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
53 extern int getopt PROTO((int, char **, char *)); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
54 |
| 6 | 55 main(argc, argv) |
| 56 int argc; | |
| 57 char **argv; | |
| 58 { | |
|
282
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
59 int c; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
60 int sts; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
61 char *command = NULL; |
| 6 | 62 char *filename = NULL; |
| 63 FILE *fp = stdin; | |
| 86 | 64 |
|
282
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
65 initargs(&argc, &argv); /* Defined in config*.c */ |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
66 |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
67 while ((c = getopt(argc, argv, "c:")) != EOF) { |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
68 switch (c) { |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
69 |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
70 default: |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
71 fprintf(stderr, |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
72 "usage: %s [-c cmd | file | -] [arg] ...\n", |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
73 argv[0]); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
74 exit(2); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
75 /*NOTREACHED*/ |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
76 |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
77 case 'c': |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
78 if (command != NULL) { |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
79 fprintf(stderr, "%s: duplicate -c option\n", |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
80 argv[0]); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
81 exit(2); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
82 } |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
83 command = malloc(strlen(optarg) + 2); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
84 /* Ignore malloc errors this early... */ |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
85 strcpy(command, optarg); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
86 strcat(command, "\n"); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
87 break; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
88 |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
89 } |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
90 } |
| 6 | 91 |
|
282
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
92 if (command == NULL && optind < argc && strcmp(argv[optind], "-") != 0) |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
93 filename = argv[optind]; |
| 86 | 94 |
| 6 | 95 if (filename != NULL) { |
| 96 if ((fp = fopen(filename, "r")) == NULL) { | |
|
282
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
97 fprintf(stderr, "%s: can't open file '%s'\n", |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
98 argv[0], filename); |
| 6 | 99 exit(2); |
| 100 } | |
| 101 } | |
| 102 | |
|
282
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
103 initall(); /* Defined in config*.c */ |
| 86 | 104 |
| 105 setpythonpath(getpythonpath()); | |
|
282
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
106 if (command != NULL) { |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
107 /* Backup optind and force sys.argv[0] = '-c' */ |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
108 optind--; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
109 argv[optind] = "-c"; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
110 } |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
111 setpythonargv(argc-optind, argv+optind); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
112 |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
113 if (command) { |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
114 sts = run_command(command) != 0; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
115 } |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
116 else { |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
117 sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
118 } |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
119 |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
120 goaway(sts); |
| 86 | 121 /*NOTREACHED*/ |
| 122 } | |
| 123 | |
| 124 /* Initialize all */ | |
| 125 | |
| 126 void | |
| 127 initall() | |
| 128 { | |
| 129 static int inited; | |
| 130 | |
| 131 if (inited) | |
| 132 return; | |
|
94
dac8625c8d9b
Changes for THINK C 4.0.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
133 inited = 1; |
| 86 | 134 |
| 135 initimport(); | |
| 136 | |
|
94
dac8625c8d9b
Changes for THINK C 4.0.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
137 /* Modules 'builtin' and 'sys' are initialized here, |
|
dac8625c8d9b
Changes for THINK C 4.0.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
138 they are needed by random bits of the interpreter. |
|
dac8625c8d9b
Changes for THINK C 4.0.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
139 All other modules are optional and should be initialized |
|
dac8625c8d9b
Changes for THINK C 4.0.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
140 by the initcalls() of a specific configuration. */ |
|
dac8625c8d9b
Changes for THINK C 4.0.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
141 |
| 86 | 142 initbuiltin(); /* Also initializes builtin exceptions */ |
| 143 initsys(); | |
| 6 | 144 |
| 86 | 145 initcalls(); /* Configuration-dependent initializations */ |
| 146 | |
| 147 initintr(); /* For intrcheck() */ | |
| 148 } | |
| 149 | |
| 150 /* Parse input from a file and execute it */ | |
| 151 | |
| 152 int | |
| 153 run(fp, filename) | |
| 154 FILE *fp; | |
| 155 char *filename; | |
| 156 { | |
| 157 if (filename == NULL) | |
| 158 filename = "???"; | |
| 159 if (isatty(fileno(fp))) | |
| 160 return run_tty_loop(fp, filename); | |
| 161 else | |
| 162 return run_script(fp, filename); | |
| 163 } | |
| 164 | |
| 165 int | |
| 166 run_tty_loop(fp, filename) | |
| 167 FILE *fp; | |
| 168 char *filename; | |
| 169 { | |
| 170 object *v; | |
| 171 int ret; | |
| 172 v = sysget("ps1"); | |
| 173 if (v == NULL) { | |
| 174 sysset("ps1", v = newstringobject(">>> ")); | |
| 175 XDECREF(v); | |
| 176 } | |
| 177 v = sysget("ps2"); | |
| 178 if (v == NULL) { | |
| 179 sysset("ps2", v = newstringobject("... ")); | |
| 180 XDECREF(v); | |
| 181 } | |
| 182 for (;;) { | |
| 183 ret = run_tty_1(fp, filename); | |
| 184 #ifdef REF_DEBUG | |
| 185 fprintf(stderr, "[%ld refs]\n", ref_total); | |
| 186 #endif | |
| 187 if (ret == E_EOF) | |
| 188 return 0; | |
| 189 /* | |
| 190 if (ret == E_NOMEM) | |
| 191 return -1; | |
| 192 */ | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 int | |
| 197 run_tty_1(fp, filename) | |
| 198 FILE *fp; | |
| 199 char *filename; | |
| 200 { | |
| 201 object *m, *d, *v, *w; | |
| 202 node *n; | |
| 203 char *ps1, *ps2; | |
| 204 int err; | |
| 205 v = sysget("ps1"); | |
| 206 w = sysget("ps2"); | |
| 207 if (v != NULL && is_stringobject(v)) { | |
| 208 INCREF(v); | |
| 209 ps1 = getstringvalue(v); | |
| 210 } | |
| 211 else { | |
| 212 v = NULL; | |
| 213 ps1 = ""; | |
| 214 } | |
| 215 if (w != NULL && is_stringobject(w)) { | |
| 216 INCREF(w); | |
| 217 ps2 = getstringvalue(w); | |
| 6 | 218 } |
| 219 else { | |
| 86 | 220 w = NULL; |
| 221 ps2 = ""; | |
| 222 } | |
| 223 err = parsefile(fp, filename, &gram, single_input, ps1, ps2, &n); | |
| 224 XDECREF(v); | |
| 225 XDECREF(w); | |
| 226 if (err == E_EOF) | |
| 227 return E_EOF; | |
| 228 if (err != E_DONE) { | |
| 229 err_input(err); | |
| 230 print_error(); | |
| 231 return err; | |
| 232 } | |
| 233 m = add_module("__main__"); | |
| 234 if (m == NULL) | |
| 235 return -1; | |
| 236 d = getmoduledict(m); | |
| 237 v = run_node(n, filename, d, d); | |
| 238 flushline(); | |
| 239 if (v == NULL) { | |
| 240 print_error(); | |
| 241 return -1; | |
| 242 } | |
| 243 DECREF(v); | |
| 244 return 0; | |
| 245 } | |
| 246 | |
| 247 int | |
| 248 run_script(fp, filename) | |
| 249 FILE *fp; | |
| 250 char *filename; | |
| 251 { | |
| 252 object *m, *d, *v; | |
| 253 m = add_module("__main__"); | |
| 254 if (m == NULL) | |
| 255 return -1; | |
| 256 d = getmoduledict(m); | |
| 257 v = run_file(fp, filename, file_input, d, d); | |
| 258 flushline(); | |
| 259 if (v == NULL) { | |
| 260 print_error(); | |
| 261 return -1; | |
| 6 | 262 } |
| 86 | 263 DECREF(v); |
| 264 return 0; | |
| 265 } | |
| 266 | |
|
282
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
267 int |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
268 run_command(command) |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
269 char *command; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
270 { |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
271 object *m, *d, *v; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
272 m = add_module("__main__"); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
273 if (m == NULL) |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
274 return -1; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
275 d = getmoduledict(m); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
276 v = run_string(command, file_input, d, d); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
277 flushline(); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
278 if (v == NULL) { |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
279 print_error(); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
280 return -1; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
281 } |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
282 DECREF(v); |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
283 return 0; |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
284 } |
|
d5c9ae422b41
Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents:
262
diff
changeset
|
285 |
| 86 | 286 void |
| 287 print_error() | |
| 288 { | |
| 289 object *exception, *v; | |
| 290 err_get(&exception, &v); | |
|
547
e198cc98a4fd
Catch SystemExit and go away if it is caught.
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
291 if (exception == SystemExit) { |
|
e198cc98a4fd
Catch SystemExit and go away if it is caught.
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
292 if (v == NULL || v == None) |
|
e198cc98a4fd
Catch SystemExit and go away if it is caught.
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
293 goaway(0); |
|
e198cc98a4fd
Catch SystemExit and go away if it is caught.
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
294 if (is_intobject(v)) |
|
e198cc98a4fd
Catch SystemExit and go away if it is caught.
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
295 goaway((int)getintvalue(v)); |
|
e198cc98a4fd
Catch SystemExit and go away if it is caught.
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
296 else { |
|
e198cc98a4fd
Catch SystemExit and go away if it is caught.
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
297 printobject(v, stderr, PRINT_RAW); |
|
e198cc98a4fd
Catch SystemExit and go away if it is caught.
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
298 fprintf(stderr, "\n"); |
|
e198cc98a4fd
Catch SystemExit and go away if it is caught.
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
299 goaway(1); |
|
e198cc98a4fd
Catch SystemExit and go away if it is caught.
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
300 } |
|
e198cc98a4fd
Catch SystemExit and go away if it is caught.
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
301 } |
| 86 | 302 fprintf(stderr, "Unhandled exception: "); |
|
285
85bd7dac13f3
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
282
diff
changeset
|
303 if (printobject(exception, stderr, PRINT_RAW) != 0) |
|
85bd7dac13f3
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
282
diff
changeset
|
304 err_clear(); |
| 86 | 305 if (v != NULL && v != None) { |
| 306 fprintf(stderr, ": "); | |
|
285
85bd7dac13f3
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
282
diff
changeset
|
307 if (printobject(v, stderr, PRINT_RAW) != 0) |
|
85bd7dac13f3
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
282
diff
changeset
|
308 err_clear(); |
| 86 | 309 } |
| 310 fprintf(stderr, "\n"); | |
| 311 XDECREF(exception); | |
| 312 XDECREF(v); | |
| 313 printtraceback(stderr); | |
| 314 } | |
| 315 | |
| 316 object * | |
| 317 run_string(str, start, globals, locals) | |
| 318 char *str; | |
| 319 int start; | |
| 320 /*dict*/object *globals, *locals; | |
| 321 { | |
| 322 node *n; | |
| 323 int err; | |
| 324 err = parse_string(str, start, &n); | |
| 325 return run_err_node(err, n, "<string>", globals, locals); | |
| 326 } | |
| 327 | |
| 328 object * | |
| 329 run_file(fp, filename, start, globals, locals) | |
| 330 FILE *fp; | |
| 331 char *filename; | |
| 332 int start; | |
| 333 /*dict*/object *globals, *locals; | |
| 334 { | |
| 335 node *n; | |
| 336 int err; | |
| 337 err = parse_file(fp, filename, start, &n); | |
| 338 return run_err_node(err, n, filename, globals, locals); | |
| 6 | 339 } |
| 340 | |
| 86 | 341 object * |
| 342 run_err_node(err, n, filename, globals, locals) | |
| 343 int err; | |
| 344 node *n; | |
| 345 char *filename; | |
| 346 /*dict*/object *globals, *locals; | |
| 347 { | |
| 348 if (err != E_DONE) { | |
| 349 err_input(err); | |
| 350 return NULL; | |
| 351 } | |
| 352 return run_node(n, filename, globals, locals); | |
| 353 } | |
| 354 | |
| 355 object * | |
| 356 run_node(n, filename, globals, locals) | |
| 357 node *n; | |
| 358 char *filename; | |
| 359 /*dict*/object *globals, *locals; | |
| 360 { | |
| 361 if (globals == NULL) { | |
| 362 globals = getglobals(); | |
| 363 if (locals == NULL) | |
| 364 locals = getlocals(); | |
| 365 } | |
| 366 else { | |
| 367 if (locals == NULL) | |
| 368 locals = globals; | |
| 369 } | |
| 370 return eval_node(n, filename, globals, locals); | |
| 371 } | |
| 372 | |
| 373 object * | |
| 374 eval_node(n, filename, globals, locals) | |
| 375 node *n; | |
| 376 char *filename; | |
| 377 object *globals; | |
| 378 object *locals; | |
| 379 { | |
| 380 codeobject *co; | |
| 381 object *v; | |
| 382 co = compile(n, filename); | |
| 383 freetree(n); | |
| 384 if (co == NULL) | |
| 385 return NULL; | |
| 386 v = eval_code(co, globals, locals, (object *)NULL); | |
| 387 DECREF(co); | |
| 388 return v; | |
| 389 } | |
| 390 | |
| 391 /* Simplified interface to parsefile */ | |
| 392 | |
| 393 int | |
| 394 parse_file(fp, filename, start, n_ret) | |
| 395 FILE *fp; | |
| 396 char *filename; | |
| 397 int start; | |
| 398 node **n_ret; | |
| 399 { | |
| 400 return parsefile(fp, filename, &gram, start, | |
| 401 (char *)0, (char *)0, n_ret); | |
| 402 } | |
| 403 | |
| 404 /* Simplified interface to parsestring */ | |
| 405 | |
| 406 int | |
| 407 parse_string(str, start, n_ret) | |
| 408 char *str; | |
| 409 int start; | |
| 410 node **n_ret; | |
| 411 { | |
| 412 int err = parsestring(str, &gram, start, n_ret); | |
| 413 /* Don't confuse early end of string with early end of input */ | |
| 414 if (err == E_EOF) | |
| 415 err = E_SYNTAX; | |
| 416 return err; | |
| 417 } | |
| 418 | |
| 419 /* Print fatal error message and abort */ | |
| 420 | |
| 421 void | |
| 422 fatal(msg) | |
| 423 char *msg; | |
| 424 { | |
| 425 fprintf(stderr, "Fatal error: %s\n", msg); | |
| 426 abort(); | |
| 427 } | |
| 428 | |
| 429 /* Clean up and exit */ | |
| 430 | |
| 431 void | |
| 6 | 432 goaway(sts) |
| 433 int sts; | |
| 434 { | |
| 86 | 435 flushline(); |
| 436 | |
| 437 /* XXX Call doneimport() before donecalls(), since donecalls() | |
| 438 calls wdone(), and doneimport() may close windows */ | |
| 439 doneimport(); | |
| 440 donecalls(); | |
| 441 | |
| 442 err_clear(); | |
| 443 | |
| 444 #ifdef REF_DEBUG | |
| 445 fprintf(stderr, "[%ld refs]\n", ref_total); | |
| 446 #endif | |
| 447 | |
| 448 #ifdef TRACE_REFS | |
| 449 if (askyesno("Print left references?")) { | |
| 450 printrefs(stderr); | |
| 451 } | |
| 452 #endif /* TRACE_REFS */ | |
| 453 | |
| 6 | 454 exit(sts); |
| 455 /*NOTREACHED*/ | |
| 456 } | |
| 457 | |
| 86 | 458 static |
| 459 finaloutput() | |
| 6 | 460 { |
| 86 | 461 #ifdef TRACE_REFS |
| 462 if (!askyesno("Print left references?")) | |
| 463 return; | |
| 464 printrefs(stderr); | |
| 465 #endif /* TRACE_REFS */ | |
| 6 | 466 } |
| 467 | |
| 468 /* Ask a yes/no question */ | |
| 469 | |
| 86 | 470 static int |
| 6 | 471 askyesno(prompt) |
| 472 char *prompt; | |
| 473 { | |
| 474 char buf[256]; | |
| 475 | |
| 476 printf("%s [ny] ", prompt); | |
| 477 if (fgets(buf, sizeof buf, stdin) == NULL) | |
| 478 return 0; | |
| 479 return buf[0] == 'y' || buf[0] == 'Y'; | |
| 480 } | |
| 481 | |
|
262
6acd41a45d8d
Removed Think C 3.0 stuff and wish list.
Guido van Rossum <guido@python.org>
parents:
206
diff
changeset
|
482 #ifdef applec /* MPW (also usable for Think C 3.0) */ |
|
70
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
483 |
| 6 | 484 /* Check for file descriptor connected to interactive device. |
| 485 Pretend that stdin is always interactive, other files never. */ | |
| 486 | |
| 487 int | |
| 488 isatty(fd) | |
| 489 int fd; | |
| 490 { | |
| 491 return fd == fileno(stdin); | |
| 492 } | |
| 493 | |
|
179
0c355e9ee854
Added isatty() for MPW.
Guido van Rossum <guido@python.org>
parents:
156
diff
changeset
|
494 #endif |
