comparison Python/pythonmain.c @ 86:a6934380c6e7 legacy-trunk

"Compiling" version
author Guido van Rossum <guido@python.org>
date Thu, 20 Dec 1990 15:06:42 +0000
parents de34f4a4d353
children dac8625c8d9b
comparison
equal deleted inserted replaced
85:06c92f491f58 86:a6934380c6e7
1 /* Python interpreter main program */ 1 /* Python interpreter main program */
2 2
3 #include <stdio.h> 3 #include "allobjects.h"
4 #include <ctype.h> 4
5 #include "string.h"
6
7 extern char *getpythonpath();
8
9 #include "PROTO.h"
10 #include "grammar.h" 5 #include "grammar.h"
11 #include "node.h" 6 #include "node.h"
12 #include "parsetok.h" 7 #include "parsetok.h"
13 #include "graminit.h" 8 #include "graminit.h"
14 #include "errcode.h" 9 #include "errcode.h"
15 #include "object.h"
16 #include "stringobject.h"
17 #include "sysmodule.h" 10 #include "sysmodule.h"
11 #include "compile.h"
12 #include "ceval.h"
13 #include "pythonrun.h"
14 #include "import.h"
15
16 extern char *getpythonpath();
18 17
19 extern grammar gram; /* From graminit.c */ 18 extern grammar gram; /* From graminit.c */
20 19
21 int debugging; 20 #ifdef DEBUG
21 int debugging; /* Needed by parser.c */
22 #endif
22 23
23 main(argc, argv) 24 main(argc, argv)
24 int argc; 25 int argc;
25 char **argv; 26 char **argv;
26 { 27 {
27 char *filename = NULL; 28 char *filename = NULL;
28 FILE *fp = stdin; 29 FILE *fp = stdin;
29 int ret; 30
30
31 initargs(&argc, &argv); 31 initargs(&argc, &argv);
32
33 initintr(); /* For intrcheck() */
34 32
35 if (argc > 1 && strcmp(argv[1], "-") != 0) 33 if (argc > 1 && strcmp(argv[1], "-") != 0)
36 filename = argv[1]; 34 filename = argv[1];
37 35
38 if (filename != NULL) { 36 if (filename != NULL) {
39 if ((fp = fopen(filename, "r")) == NULL) { 37 if ((fp = fopen(filename, "r")) == NULL) {
40 fprintf(stderr, "python: can't open file '%s'\n", 38 fprintf(stderr, "python: can't open file '%s'\n",
41 filename); 39 filename);
42 exit(2); 40 exit(2);
43 } 41 }
44 } 42 }
45 43
46 /* XXX what is the ideal initialization order? */ 44 initall();
47 /* XXX exceptions are initialized by initrun but this 45
48 may be too late */ 46 setpythonpath(getpythonpath());
49 47 setpythonargv(argc-1, argv+1);
50 initsys(argc-1, argv+1); 48
49 goaway(run(fp, filename == NULL ? "<stdin>" : filename));
50 /*NOTREACHED*/
51 }
52
53 /* Initialize all */
54
55 void
56 initall()
57 {
58 static int inited;
59
60 if (inited)
61 return;
62
63 initimport();
64
65 initbuiltin(); /* Also initializes builtin exceptions */
66 initsys();
51 inittime(); 67 inittime();
52 initmath(); 68 initmath();
53 69
54 setpythonpath(getpythonpath()); 70 initcalls(); /* Configuration-dependent initializations */
55 71
56 initrun(); 72 initintr(); /* For intrcheck() */
57 initcalls(); 73
58 74 inited = 1;
59 if (!isatty(fileno(fp))) { 75 }
60 ret = runfile(fp, file_input, (char *)NULL, (char *)NULL); 76
77 /* Parse input from a file and execute it */
78
79 int
80 run(fp, filename)
81 FILE *fp;
82 char *filename;
83 {
84 if (filename == NULL)
85 filename = "???";
86 if (isatty(fileno(fp)))
87 return run_tty_loop(fp, filename);
88 else
89 return run_script(fp, filename);
90 }
91
92 int
93 run_tty_loop(fp, filename)
94 FILE *fp;
95 char *filename;
96 {
97 object *v;
98 int ret;
99 v = sysget("ps1");
100 if (v == NULL) {
101 sysset("ps1", v = newstringobject(">>> "));
102 XDECREF(v);
103 }
104 v = sysget("ps2");
105 if (v == NULL) {
106 sysset("ps2", v = newstringobject("... "));
107 XDECREF(v);
108 }
109 for (;;) {
110 ret = run_tty_1(fp, filename);
111 #ifdef REF_DEBUG
112 fprintf(stderr, "[%ld refs]\n", ref_total);
113 #endif
114 if (ret == E_EOF)
115 return 0;
116 /*
117 if (ret == E_NOMEM)
118 return -1;
119 */
120 }
121 }
122
123 int
124 run_tty_1(fp, filename)
125 FILE *fp;
126 char *filename;
127 {
128 object *m, *d, *v, *w;
129 node *n;
130 char *ps1, *ps2;
131 int err;
132 v = sysget("ps1");
133 w = sysget("ps2");
134 if (v != NULL && is_stringobject(v)) {
135 INCREF(v);
136 ps1 = getstringvalue(v);
61 } 137 }
62 else { 138 else {
63 object *v, *w; 139 v = NULL;
64 sysset("ps1", v = newstringobject(">>> ")); 140 ps1 = "";
65 sysset("ps2", w = newstringobject("... ")); 141 }
66 DECREF(v); 142 if (w != NULL && is_stringobject(w)) {
67 DECREF(w); 143 INCREF(w);
68 for (;;) { 144 ps2 = getstringvalue(w);
69 char *ps1 = NULL, *ps2 = NULL; 145 }
70 v = sysget("ps1"); 146 else {
71 w = sysget("ps2"); 147 w = NULL;
72 if (v != NULL && is_stringobject(v)) { 148 ps2 = "";
73 INCREF(v); 149 }
74 ps1 = getstringvalue(v); 150 err = parsefile(fp, filename, &gram, single_input, ps1, ps2, &n);
75 } 151 XDECREF(v);
76 else 152 XDECREF(w);
77 v = NULL; 153 if (err == E_EOF)
78 if (w != NULL && is_stringobject(w)) { 154 return E_EOF;
79 INCREF(w); 155 if (err != E_DONE) {
80 ps2 = getstringvalue(w); 156 err_input(err);
81 } 157 print_error();
82 else 158 return err;
83 w = NULL; 159 }
84 ret = runfile(fp, single_input, ps1, ps2); 160 m = add_module("__main__");
85 if (v != NULL) 161 if (m == NULL)
86 DECREF(v); 162 return -1;
87 if (w != NULL) 163 d = getmoduledict(m);
88 DECREF(w); 164 v = run_node(n, filename, d, d);
89 if (ret == E_EOF || ret == E_NOMEM) 165 flushline();
90 break; 166 if (v == NULL) {
91 } 167 print_error();
92 } 168 return -1;
93 goaway(ret == E_DONE || ret == E_EOF ? 0 : 1); 169 }
94 /*NOTREACHED*/ 170 DECREF(v);
95 } 171 return 0;
96 172 }
173
174 int
175 run_script(fp, filename)
176 FILE *fp;
177 char *filename;
178 {
179 object *m, *d, *v;
180 m = add_module("__main__");
181 if (m == NULL)
182 return -1;
183 d = getmoduledict(m);
184 v = run_file(fp, filename, file_input, d, d);
185 flushline();
186 if (v == NULL) {
187 print_error();
188 return -1;
189 }
190 DECREF(v);
191 return 0;
192 }
193
194 void
195 print_error()
196 {
197 object *exception, *v;
198 err_get(&exception, &v);
199 fprintf(stderr, "Unhandled exception: ");
200 printobject(exception, stderr, PRINT_RAW);
201 if (v != NULL && v != None) {
202 fprintf(stderr, ": ");
203 printobject(v, stderr, PRINT_RAW);
204 }
205 fprintf(stderr, "\n");
206 XDECREF(exception);
207 XDECREF(v);
208 printtraceback(stderr);
209 }
210
211 object *
212 run_string(str, start, globals, locals)
213 char *str;
214 int start;
215 /*dict*/object *globals, *locals;
216 {
217 node *n;
218 int err;
219 err = parse_string(str, start, &n);
220 return run_err_node(err, n, "<string>", globals, locals);
221 }
222
223 object *
224 run_file(fp, filename, start, globals, locals)
225 FILE *fp;
226 char *filename;
227 int start;
228 /*dict*/object *globals, *locals;
229 {
230 node *n;
231 int err;
232 err = parse_file(fp, filename, start, &n);
233 return run_err_node(err, n, filename, globals, locals);
234 }
235
236 object *
237 run_err_node(err, n, filename, globals, locals)
238 int err;
239 node *n;
240 char *filename;
241 /*dict*/object *globals, *locals;
242 {
243 if (err != E_DONE) {
244 err_input(err);
245 return NULL;
246 }
247 return run_node(n, filename, globals, locals);
248 }
249
250 object *
251 run_node(n, filename, globals, locals)
252 node *n;
253 char *filename;
254 /*dict*/object *globals, *locals;
255 {
256 if (globals == NULL) {
257 globals = getglobals();
258 if (locals == NULL)
259 locals = getlocals();
260 }
261 else {
262 if (locals == NULL)
263 locals = globals;
264 }
265 return eval_node(n, filename, globals, locals);
266 }
267
268 object *
269 eval_node(n, filename, globals, locals)
270 node *n;
271 char *filename;
272 object *globals;
273 object *locals;
274 {
275 codeobject *co;
276 object *v;
277 co = compile(n, filename);
278 freetree(n);
279 if (co == NULL)
280 return NULL;
281 v = eval_code(co, globals, locals, (object *)NULL);
282 DECREF(co);
283 return v;
284 }
285
286 /* Simplified interface to parsefile */
287
288 int
289 parse_file(fp, filename, start, n_ret)
290 FILE *fp;
291 char *filename;
292 int start;
293 node **n_ret;
294 {
295 return parsefile(fp, filename, &gram, start,
296 (char *)0, (char *)0, n_ret);
297 }
298
299 /* Simplified interface to parsestring */
300
301 int
302 parse_string(str, start, n_ret)
303 char *str;
304 int start;
305 node **n_ret;
306 {
307 int err = parsestring(str, &gram, start, n_ret);
308 /* Don't confuse early end of string with early end of input */
309 if (err == E_EOF)
310 err = E_SYNTAX;
311 return err;
312 }
313
314 /* Print fatal error message and abort */
315
316 void
317 fatal(msg)
318 char *msg;
319 {
320 fprintf(stderr, "Fatal error: %s\n", msg);
321 abort();
322 }
323
324 /* Clean up and exit */
325
326 void
97 goaway(sts) 327 goaway(sts)
98 int sts; 328 int sts;
99 { 329 {
330 flushline();
331
332 /* XXX Call doneimport() before donecalls(), since donecalls()
333 calls wdone(), and doneimport() may close windows */
334 doneimport();
335 donecalls();
336
337 err_clear();
338
339 #ifdef REF_DEBUG
340 fprintf(stderr, "[%ld refs]\n", ref_total);
341 #endif
342
100 #ifdef THINK_C 343 #ifdef THINK_C
101 if (sts == 0) 344 if (sts == 0)
102 Click_On(0); 345 Click_On(0);
103 #endif 346 #endif
104 closerun(); 347
105 donecalls(); 348 #ifdef TRACE_REFS
349 if (askyesno("Print left references?")) {
350 #ifdef THINK_C
351 Click_On(1);
352 #endif
353 printrefs(stderr);
354 }
355 #endif /* TRACE_REFS */
356
106 exit(sts); 357 exit(sts);
107 /*NOTREACHED*/ 358 /*NOTREACHED*/
108 } 359 }
109 360
110 /* Parse input from a file and execute it */ 361 static
362 finaloutput()
363 {
364 #ifdef TRACE_REFS
365 if (!askyesno("Print left references?"))
366 return;
367 #ifdef THINK_C
368 Click_On(1);
369 #endif /* THINK_C */
370 printrefs(stderr);
371 #endif /* TRACE_REFS */
372 }
373
374 /* Ask a yes/no question */
111 375
112 static int 376 static int
113 runfile(fp, start, ps1, ps2)
114 FILE *fp;
115 int start;
116 char *ps1, *ps2;
117 {
118 node *n;
119 int ret;
120 ret = parsefile(fp, &gram, start, ps1, ps2, &n);
121 if (ret != E_DONE)
122 return ret;
123 return execute(n) == 0 ? E_DONE : E_ERROR;
124 }
125
126 /* Ask a yes/no question */
127
128 int
129 askyesno(prompt) 377 askyesno(prompt)
130 char *prompt; 378 char *prompt;
131 { 379 {
132 char buf[256]; 380 char buf[256];
133 381
149 return fd == fileno(stdin); 397 return fd == fileno(stdin);
150 } 398 }
151 399
152 #endif 400 #endif
153 401
154 /* WISH LIST 402 /* XXX WISH LIST
155 403
156 - improved per-module error handling; different use of errno
157 - possible new types: 404 - possible new types:
158 - iterator (for range, keys, ...) 405 - iterator (for range, keys, ...)
159 - improve interpreter error handling, e.g., true tracebacks 406 - improve interpreter error handling, e.g., true tracebacks
160 - release parse trees when no longer needed (make them objects?)
161 - faster parser (for long modules)
162 - save precompiled modules on file? 407 - save precompiled modules on file?
163 - fork threads, locking 408 - fork threads, locking
164 - allow syntax extensions 409 - allow syntax extensions
165 */ 410 */
411
412 /* "Floccinaucinihilipilification" */