Mercurial > cpython-lru_cache-weakref
comparison Python/pythonrun.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 | |
| children | c156d4960c2b |
comparison
equal
deleted
inserted
replaced
| 922:c12b445cdace | 923:72aceed365d4 |
|---|---|
| 1 /*********************************************************** | |
| 2 Copyright 1991, 1992 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 | |
| 25 /* Python interpreter top-level routines, including init/exit */ | |
| 26 | |
| 27 #include "allobjects.h" | |
| 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 #ifdef USE_THREAD | |
| 41 extern void *save_thread(); | |
| 42 #endif | |
| 43 | |
| 44 extern char *getpythonpath(); | |
| 45 | |
| 46 extern grammar gram; /* From graminit.c */ | |
| 47 | |
| 48 int debugging; /* Needed by parser.c */ | |
| 49 int verbose; /* Needed by import.c */ | |
| 50 | |
| 51 /* Initialize all */ | |
| 52 | |
| 53 void | |
| 54 initall() | |
| 55 { | |
| 56 static int inited; | |
| 57 | |
| 58 if (inited) | |
| 59 return; | |
| 60 inited = 1; | |
| 61 | |
| 62 initimport(); | |
| 63 | |
| 64 /* Modules 'builtin' and 'sys' are initialized here, | |
| 65 they are needed by random bits of the interpreter. | |
| 66 All other modules are optional and are initialized | |
| 67 when they are first imported. */ | |
| 68 | |
| 69 initbuiltin(); /* Also initializes builtin exceptions */ | |
| 70 initsys(); | |
| 71 | |
| 72 initcalls(); /* Configuration-dependent initializations */ | |
| 73 | |
| 74 initintr(); /* For intrcheck() */ | |
| 75 | |
| 76 setpythonpath(getpythonpath()); | |
| 77 } | |
| 78 | |
| 79 /* Parse input from a file and execute it */ | |
| 80 | |
| 81 int | |
| 82 run(fp, filename) | |
| 83 FILE *fp; | |
| 84 char *filename; | |
| 85 { | |
| 86 if (filename == NULL) | |
| 87 filename = "???"; | |
| 88 if (isatty((int)fileno(fp))) | |
| 89 return run_tty_loop(fp, filename); | |
| 90 else | |
| 91 return run_script(fp, filename); | |
| 92 } | |
| 93 | |
| 94 int | |
| 95 run_tty_loop(fp, filename) | |
| 96 FILE *fp; | |
| 97 char *filename; | |
| 98 { | |
| 99 object *v; | |
| 100 int ret; | |
| 101 v = sysget("ps1"); | |
| 102 if (v == NULL) { | |
| 103 sysset("ps1", v = newstringobject(">>> ")); | |
| 104 XDECREF(v); | |
| 105 } | |
| 106 v = sysget("ps2"); | |
| 107 if (v == NULL) { | |
| 108 sysset("ps2", v = newstringobject("... ")); | |
| 109 XDECREF(v); | |
| 110 } | |
| 111 for (;;) { | |
| 112 ret = run_tty_1(fp, filename); | |
| 113 #ifdef REF_DEBUG | |
| 114 fprintf(stderr, "[%ld refs]\n", ref_total); | |
| 115 #endif | |
| 116 if (ret == E_EOF) | |
| 117 return 0; | |
| 118 /* | |
| 119 if (ret == E_NOMEM) | |
| 120 return -1; | |
| 121 */ | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 int | |
| 126 run_tty_1(fp, filename) | |
| 127 FILE *fp; | |
| 128 char *filename; | |
| 129 { | |
| 130 void *save, *save_thread(), restore_thread(); | |
| 131 object *m, *d, *v, *w; | |
| 132 node *n; | |
| 133 char *ps1, *ps2; | |
| 134 int err; | |
| 135 v = sysget("ps1"); | |
| 136 w = sysget("ps2"); | |
| 137 if (v != NULL && is_stringobject(v)) { | |
| 138 INCREF(v); | |
| 139 ps1 = getstringvalue(v); | |
| 140 } | |
| 141 else { | |
| 142 v = NULL; | |
| 143 ps1 = ""; | |
| 144 } | |
| 145 if (w != NULL && is_stringobject(w)) { | |
| 146 INCREF(w); | |
| 147 ps2 = getstringvalue(w); | |
| 148 } | |
| 149 else { | |
| 150 w = NULL; | |
| 151 ps2 = ""; | |
| 152 } | |
| 153 save = save_thread(); | |
| 154 err = parsefile(fp, filename, &gram, single_input, ps1, ps2, &n); | |
| 155 restore_thread(save); | |
| 156 XDECREF(v); | |
| 157 XDECREF(w); | |
| 158 if (err == E_EOF) | |
| 159 return E_EOF; | |
| 160 if (err != E_DONE) { | |
| 161 err_input(err); | |
| 162 print_error(); | |
| 163 return err; | |
| 164 } | |
| 165 m = add_module("__main__"); | |
| 166 if (m == NULL) | |
| 167 return -1; | |
| 168 d = getmoduledict(m); | |
| 169 v = run_node(n, filename, d, d); | |
| 170 flushline(); | |
| 171 if (v == NULL) { | |
| 172 print_error(); | |
| 173 return -1; | |
| 174 } | |
| 175 DECREF(v); | |
| 176 return 0; | |
| 177 } | |
| 178 | |
| 179 int | |
| 180 run_script(fp, filename) | |
| 181 FILE *fp; | |
| 182 char *filename; | |
| 183 { | |
| 184 object *m, *d, *v; | |
| 185 m = add_module("__main__"); | |
| 186 if (m == NULL) | |
| 187 return -1; | |
| 188 d = getmoduledict(m); | |
| 189 v = run_file(fp, filename, file_input, d, d); | |
| 190 flushline(); | |
| 191 if (v == NULL) { | |
| 192 print_error(); | |
| 193 return -1; | |
| 194 } | |
| 195 DECREF(v); | |
| 196 return 0; | |
| 197 } | |
| 198 | |
| 199 int | |
| 200 run_command(command) | |
| 201 char *command; | |
| 202 { | |
| 203 object *m, *d, *v; | |
| 204 m = add_module("__main__"); | |
| 205 if (m == NULL) | |
| 206 return -1; | |
| 207 d = getmoduledict(m); | |
| 208 v = run_string(command, file_input, d, d); | |
| 209 flushline(); | |
| 210 if (v == NULL) { | |
| 211 print_error(); | |
| 212 return -1; | |
| 213 } | |
| 214 DECREF(v); | |
| 215 return 0; | |
| 216 } | |
| 217 | |
| 218 void | |
| 219 print_error() | |
| 220 { | |
| 221 object *exception, *v; | |
| 222 err_get(&exception, &v); | |
| 223 if (exception == SystemExit) { | |
| 224 if (v == NULL || v == None) | |
| 225 goaway(0); | |
| 226 if (is_intobject(v)) | |
| 227 goaway((int)getintvalue(v)); | |
| 228 else { | |
| 229 printobject(v, stderr, PRINT_RAW); | |
| 230 fprintf(stderr, "\n"); | |
| 231 goaway(1); | |
| 232 } | |
| 233 } | |
| 234 sysset("last_type", exception); | |
| 235 sysset("last_value", v); | |
| 236 if (printobject(exception, stderr, PRINT_RAW) != 0) | |
| 237 err_clear(); | |
| 238 if (v != NULL && v != None) { | |
| 239 fprintf(stderr, ": "); | |
| 240 if (printobject(v, stderr, PRINT_RAW) != 0) | |
| 241 err_clear(); | |
| 242 } | |
| 243 fprintf(stderr, "\n"); | |
| 244 XDECREF(exception); | |
| 245 XDECREF(v); | |
| 246 printtraceback(stderr); | |
| 247 } | |
| 248 | |
| 249 object * | |
| 250 run_string(str, start, globals, locals) | |
| 251 char *str; | |
| 252 int start; | |
| 253 /*dict*/object *globals, *locals; | |
| 254 { | |
| 255 node *n; | |
| 256 int err; | |
| 257 err = parse_string(str, start, &n); | |
| 258 return run_err_node(err, n, "<string>", globals, locals); | |
| 259 } | |
| 260 | |
| 261 object * | |
| 262 run_file(fp, filename, start, globals, locals) | |
| 263 FILE *fp; | |
| 264 char *filename; | |
| 265 int start; | |
| 266 /*dict*/object *globals, *locals; | |
| 267 { | |
| 268 node *n; | |
| 269 int err; | |
| 270 err = parse_file(fp, filename, start, &n); | |
| 271 return run_err_node(err, n, filename, globals, locals); | |
| 272 } | |
| 273 | |
| 274 object * | |
| 275 run_err_node(err, n, filename, globals, locals) | |
| 276 int err; | |
| 277 node *n; | |
| 278 char *filename; | |
| 279 /*dict*/object *globals, *locals; | |
| 280 { | |
| 281 if (err != E_DONE) { | |
| 282 err_input(err); | |
| 283 return NULL; | |
| 284 } | |
| 285 return run_node(n, filename, globals, locals); | |
| 286 } | |
| 287 | |
| 288 object * | |
| 289 run_node(n, filename, globals, locals) | |
| 290 node *n; | |
| 291 char *filename; | |
| 292 /*dict*/object *globals, *locals; | |
| 293 { | |
| 294 if (globals == NULL) { | |
| 295 globals = getglobals(); | |
| 296 if (locals == NULL) | |
| 297 locals = getlocals(); | |
| 298 } | |
| 299 else { | |
| 300 if (locals == NULL) | |
| 301 locals = globals; | |
| 302 } | |
| 303 return eval_node(n, filename, globals, locals); | |
| 304 } | |
| 305 | |
| 306 object * | |
| 307 eval_node(n, filename, globals, locals) | |
| 308 node *n; | |
| 309 char *filename; | |
| 310 object *globals; | |
| 311 object *locals; | |
| 312 { | |
| 313 codeobject *co; | |
| 314 object *v; | |
| 315 co = compile(n, filename); | |
| 316 freetree(n); | |
| 317 if (co == NULL) | |
| 318 return NULL; | |
| 319 v = eval_code(co, globals, locals, (object *)NULL); | |
| 320 DECREF(co); | |
| 321 return v; | |
| 322 } | |
| 323 | |
| 324 /* Simplified interface to parsefile */ | |
| 325 | |
| 326 int | |
| 327 parse_file(fp, filename, start, n_ret) | |
| 328 FILE *fp; | |
| 329 char *filename; | |
| 330 int start; | |
| 331 node **n_ret; | |
| 332 { | |
| 333 return parsefile(fp, filename, &gram, start, | |
| 334 (char *)0, (char *)0, n_ret); | |
| 335 } | |
| 336 | |
| 337 /* Simplified interface to parsestring */ | |
| 338 | |
| 339 int | |
| 340 parse_string(str, start, n_ret) | |
| 341 char *str; | |
| 342 int start; | |
| 343 node **n_ret; | |
| 344 { | |
| 345 int err = parsestring(str, &gram, start, n_ret); | |
| 346 /* Don't confuse early end of string with early end of input */ | |
| 347 if (err == E_EOF) | |
| 348 err = E_SYNTAX; | |
| 349 return err; | |
| 350 } | |
| 351 | |
| 352 /* Print fatal error message and abort */ | |
| 353 | |
| 354 void | |
| 355 fatal(msg) | |
| 356 char *msg; | |
| 357 { | |
| 358 fprintf(stderr, "Fatal error: %s\n", msg); | |
| 359 abort(); | |
| 360 } | |
| 361 | |
| 362 /* Clean up and exit */ | |
| 363 | |
| 364 void | |
| 365 goaway(sts) | |
| 366 int sts; | |
| 367 { | |
| 368 flushline(); | |
| 369 | |
| 370 /* XXX Call doneimport() before donecalls(), since donecalls() | |
| 371 calls wdone(), and doneimport() may close windows */ | |
| 372 doneimport(); | |
| 373 donecalls(); | |
| 374 | |
| 375 err_clear(); | |
| 376 | |
| 377 #ifdef REF_DEBUG | |
| 378 fprintf(stderr, "[%ld refs]\n", ref_total); | |
| 379 #endif | |
| 380 | |
| 381 #ifdef TRACE_REFS | |
| 382 if (askyesno("Print left references?")) { | |
| 383 printrefs(stderr); | |
| 384 } | |
| 385 #endif /* TRACE_REFS */ | |
| 386 | |
| 387 #ifdef USE_THREAD | |
| 388 (void) save_thread(); | |
| 389 exit_prog(sts); | |
| 390 #else | |
| 391 exit(sts); | |
| 392 #endif | |
| 393 /*NOTREACHED*/ | |
| 394 } | |
| 395 | |
| 396 #ifdef TRACE_REFS | |
| 397 /* Ask a yes/no question */ | |
| 398 | |
| 399 static int | |
| 400 askyesno(prompt) | |
| 401 char *prompt; | |
| 402 { | |
| 403 char buf[256]; | |
| 404 | |
| 405 printf("%s [ny] ", prompt); | |
| 406 if (fgets(buf, sizeof buf, stdin) == NULL) | |
| 407 return 0; | |
| 408 return buf[0] == 'y' || buf[0] == 'Y'; | |
| 409 } | |
| 410 #endif | |
| 411 | |
| 412 #ifdef applec /* MPW (also usable for Think C 3.0) */ | |
| 413 | |
| 414 /* Check for file descriptor connected to interactive device. | |
| 415 Pretend that stdin is always interactive, other files never. */ | |
| 416 | |
| 417 int | |
| 418 isatty(fd) | |
| 419 int fd; | |
| 420 { | |
| 421 return fd == fileno(stdin); | |
| 422 } | |
| 423 | |
| 424 #endif |
