comparison Python/pythonmain.c @ 923:72aceed365d4 legacy-trunk

* Makefile adapted to changes below. * split pythonmain.c in two: most stuff goes to pythonrun.c, in the library. * new optional built-in threadmodule.c, build upon Sjoerd's thread.{c,h}. * new module from Sjoerd: mmmodule.c (dynamically loaded). * new module from Sjoerd: sv (svgen.py, svmodule.c.proto). * new files thread.{c,h} (from Sjoerd). * new xxmodule.c (example only). * myselect.h: bzero -> memset * select.c: bzero -> memset; removed global variable
author Guido van Rossum <guido@python.org>
date Tue, 04 Aug 1992 12:41:02 +0000
parents a8ca720fd921
children c156d4960c2b
comparison
equal deleted inserted replaced
922:c12b445cdace 923:72aceed365d4
24 24
25 /* Python interpreter main program */ 25 /* Python interpreter main program */
26 26
27 #include "allobjects.h" 27 #include "allobjects.h"
28 28
29 #include "grammar.h"
30 #include "node.h"
31 #include "parsetok.h"
32 #include "graminit.h"
33 #include "errcode.h"
34 #include "sysmodule.h"
35 #include "compile.h"
36 #include "ceval.h"
37 #include "pythonrun.h"
38 #include "import.h"
39
40 extern char *getpythonpath(); 29 extern char *getpythonpath();
41 30
42 extern grammar gram; /* From graminit.c */ 31 extern int debugging; /* Needed by parser.c */
43 32 extern int verbose; /* Needed by import.c */
44 int debugging; /* Needed by parser.c */
45 int verbose; /* Needed by import.c */
46 33
47 /* Interface to getopt(): */ 34 /* Interface to getopt(): */
48 extern int optind; 35 extern int optind;
49 extern char *optarg; 36 extern char *optarg;
50 extern int getopt PROTO((int, char **, char *)); 37 extern int getopt PROTO((int, char **, char *));
106 } 93 }
107 } 94 }
108 95
109 initall(); 96 initall();
110 97
111 setpythonpath(getpythonpath());
112 if (command != NULL) { 98 if (command != NULL) {
113 /* Backup optind and force sys.argv[0] = '-c' */ 99 /* Backup optind and force sys.argv[0] = '-c' */
114 optind--; 100 optind--;
115 argv[optind] = "-c"; 101 argv[optind] = "-c";
116 } 102 }
103
117 setpythonargv(argc-optind, argv+optind); 104 setpythonargv(argc-optind, argv+optind);
118 105
119 if (command) { 106 if (command) {
120 sts = run_command(command) != 0; 107 sts = run_command(command) != 0;
121 } 108 }
124 } 111 }
125 112
126 goaway(sts); 113 goaway(sts);
127 /*NOTREACHED*/ 114 /*NOTREACHED*/
128 } 115 }
129
130 /* Initialize all */
131
132 void
133 initall()
134 {
135 static int inited;
136
137 if (inited)
138 return;
139 inited = 1;
140
141 initimport();
142
143 /* Modules 'builtin' and 'sys' are initialized here,
144 they are needed by random bits of the interpreter.
145 All other modules are optional and should be initialized
146 by the initcalls() of a specific configuration. */
147
148 initbuiltin(); /* Also initializes builtin exceptions */
149 initsys();
150
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 = "???";
165 if (isatty((int)fileno(fp)))
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);
224 }
225 else {
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;
268 }
269 DECREF(v);
270 return 0;
271 }
272
273 int
274 run_command(command)
275 char *command;
276 {
277 object *m, *d, *v;
278 m = add_module("__main__");
279 if (m == NULL)
280 return -1;
281 d = getmoduledict(m);
282 v = run_string(command, file_input, d, d);
283 flushline();
284 if (v == NULL) {
285 print_error();
286 return -1;
287 }
288 DECREF(v);
289 return 0;
290 }
291
292 void
293 print_error()
294 {
295 object *exception, *v;
296 err_get(&exception, &v);
297 if (exception == SystemExit) {
298 if (v == NULL || v == None)
299 goaway(0);
300 if (is_intobject(v))
301 goaway((int)getintvalue(v));
302 else {
303 printobject(v, stderr, PRINT_RAW);
304 fprintf(stderr, "\n");
305 goaway(1);
306 }
307 }
308 sysset("last_type", exception);
309 sysset("last_value", v);
310 if (printobject(exception, stderr, PRINT_RAW) != 0)
311 err_clear();
312 if (v != NULL && v != None) {
313 fprintf(stderr, ": ");
314 if (printobject(v, stderr, PRINT_RAW) != 0)
315 err_clear();
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);
346 }
347
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
439 goaway(sts)
440 int sts;
441 {
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
461 exit(sts);
462 /*NOTREACHED*/
463 }
464
465 #ifdef TRACE_REFS
466 /* Ask a yes/no question */
467
468 static int
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 }
479 #endif
480
481 #ifdef applec /* MPW (also usable for Think C 3.0) */
482
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
493 #endif