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