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