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