Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 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" |
Guido van Rossum | befa14f | 1995-09-18 21:42:42 +0000 | [diff] [blame] | 33 | #undef argument /* Avoid conflict on Mac */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 34 | #include "errcode.h" |
| 35 | #include "sysmodule.h" |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 36 | #include "bltinmodule.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 37 | #include "compile.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 38 | #include "eval.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 39 | #include "ceval.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 40 | #include "import.h" |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 41 | #include "marshal.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 43 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 44 | #include <signal.h> |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 47 | #ifdef MS_WIN32 |
Guido van Rossum | a44823b | 1995-03-14 15:01:17 +0000 | [diff] [blame] | 48 | #undef BYTE |
Guido van Rossum | 4f1c59b | 1996-04-09 02:37:03 +0000 | [diff] [blame] | 49 | #undef arglist |
Guido van Rossum | a44823b | 1995-03-14 15:01:17 +0000 | [diff] [blame] | 50 | #include "windows.h" |
| 51 | #endif |
| 52 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 53 | extern char *getpythonpath(); |
| 54 | |
| 55 | extern grammar gram; /* From graminit.c */ |
| 56 | |
Guido van Rossum | b73cc04 | 1993-11-01 16:28:59 +0000 | [diff] [blame] | 57 | /* Forward */ |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 58 | static void initmain PROTO((void)); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 59 | static object *run_err_node PROTO((node *n, char *filename, |
Guido van Rossum | b73cc04 | 1993-11-01 16:28:59 +0000 | [diff] [blame] | 60 | object *globals, object *locals)); |
| 61 | static object *run_node PROTO((node *n, char *filename, |
| 62 | object *globals, object *locals)); |
Guido van Rossum | 262e124 | 1995-02-07 15:30:45 +0000 | [diff] [blame] | 63 | static object *run_pyc_file PROTO((FILE *fp, char *filename, |
| 64 | object *globals, object *locals)); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 65 | static void err_input PROTO((perrdetail *)); |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 66 | static void initsigs PROTO((void)); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 68 | int debugging; /* Needed by parser.c */ |
| 69 | int verbose; /* Needed by import.c */ |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 70 | int suppress_print; /* Needed by ceval.c */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 71 | |
| 72 | /* Initialize all */ |
| 73 | |
| 74 | void |
| 75 | initall() |
| 76 | { |
| 77 | static int inited; |
| 78 | |
| 79 | if (inited) |
| 80 | return; |
| 81 | inited = 1; |
| 82 | |
| 83 | initimport(); |
| 84 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 85 | /* Modules '__builtin__' and 'sys' are initialized here, |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 86 | they are needed by random bits of the interpreter. |
| 87 | All other modules are optional and are initialized |
| 88 | when they are first imported. */ |
| 89 | |
| 90 | initbuiltin(); /* Also initializes builtin exceptions */ |
| 91 | initsys(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 92 | |
| 93 | setpythonpath(getpythonpath()); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 94 | |
| 95 | initsigs(); /* Signal handling stuff, including initintr() */ |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 96 | |
| 97 | initmain(); |
| 98 | } |
| 99 | |
| 100 | /* Create __main__ module */ |
| 101 | |
| 102 | static void |
| 103 | initmain() |
| 104 | { |
| 105 | object *m, *d; |
| 106 | m = add_module("__main__"); |
| 107 | if (m == NULL) |
| 108 | fatal("can't create __main__ module"); |
| 109 | d = getmoduledict(m); |
| 110 | if (dictlookup(d, "__builtins__") == NULL) { |
Guido van Rossum | 94390ec | 1995-01-12 11:37:57 +0000 | [diff] [blame] | 111 | if (dictinsert(d, "__builtins__", getbuiltins())) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 112 | fatal("can't add __builtins__ to __main__"); |
| 113 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* Parse input from a file and execute it */ |
| 117 | |
| 118 | int |
| 119 | run(fp, filename) |
| 120 | FILE *fp; |
| 121 | char *filename; |
| 122 | { |
| 123 | if (filename == NULL) |
| 124 | filename = "???"; |
| 125 | if (isatty((int)fileno(fp))) |
| 126 | return run_tty_loop(fp, filename); |
| 127 | else |
| 128 | return run_script(fp, filename); |
| 129 | } |
| 130 | |
| 131 | int |
| 132 | run_tty_loop(fp, filename) |
| 133 | FILE *fp; |
| 134 | char *filename; |
| 135 | { |
| 136 | object *v; |
| 137 | int ret; |
| 138 | v = sysget("ps1"); |
| 139 | if (v == NULL) { |
| 140 | sysset("ps1", v = newstringobject(">>> ")); |
| 141 | XDECREF(v); |
| 142 | } |
| 143 | v = sysget("ps2"); |
| 144 | if (v == NULL) { |
| 145 | sysset("ps2", v = newstringobject("... ")); |
| 146 | XDECREF(v); |
| 147 | } |
| 148 | for (;;) { |
| 149 | ret = run_tty_1(fp, filename); |
Guido van Rossum | aae0d32 | 1996-05-22 16:35:33 +0000 | [diff] [blame] | 150 | #ifdef Py_REF_DEBUG |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 151 | fprintf(stderr, "[%ld refs]\n", _Py_RefTotal); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 152 | #endif |
| 153 | if (ret == E_EOF) |
| 154 | return 0; |
| 155 | /* |
| 156 | if (ret == E_NOMEM) |
| 157 | return -1; |
| 158 | */ |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | int |
| 163 | run_tty_1(fp, filename) |
| 164 | FILE *fp; |
| 165 | char *filename; |
| 166 | { |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 167 | object *m, *d, *v, *w; |
| 168 | node *n; |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 169 | perrdetail err; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 170 | char *ps1, *ps2; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 171 | v = sysget("ps1"); |
| 172 | w = sysget("ps2"); |
| 173 | if (v != NULL && is_stringobject(v)) { |
| 174 | INCREF(v); |
| 175 | ps1 = getstringvalue(v); |
| 176 | } |
| 177 | else { |
| 178 | v = NULL; |
| 179 | ps1 = ""; |
| 180 | } |
| 181 | if (w != NULL && is_stringobject(w)) { |
| 182 | INCREF(w); |
| 183 | ps2 = getstringvalue(w); |
| 184 | } |
| 185 | else { |
| 186 | w = NULL; |
| 187 | ps2 = ""; |
| 188 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 189 | BGN_SAVE |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 190 | n = parsefile(fp, filename, &gram, single_input, ps1, ps2, &err); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 191 | END_SAVE |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 192 | XDECREF(v); |
| 193 | XDECREF(w); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 194 | if (n == NULL) { |
| 195 | if (err.error == E_EOF) { |
| 196 | if (err.text) |
| 197 | free(err.text); |
| 198 | return E_EOF; |
| 199 | } |
| 200 | err_input(&err); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 201 | print_error(); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 202 | return err.error; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 203 | } |
| 204 | m = add_module("__main__"); |
| 205 | if (m == NULL) |
| 206 | return -1; |
| 207 | d = getmoduledict(m); |
| 208 | v = run_node(n, filename, d, d); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 209 | if (v == NULL) { |
| 210 | print_error(); |
| 211 | return -1; |
| 212 | } |
| 213 | DECREF(v); |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 214 | flushline(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | int |
| 219 | run_script(fp, filename) |
| 220 | FILE *fp; |
| 221 | char *filename; |
| 222 | { |
| 223 | object *m, *d, *v; |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 224 | char *ext; |
| 225 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 226 | m = add_module("__main__"); |
| 227 | if (m == NULL) |
| 228 | return -1; |
| 229 | d = getmoduledict(m); |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 230 | ext = filename + strlen(filename) - 4; |
Jack Jansen | bd06e96 | 1995-02-13 11:44:56 +0000 | [diff] [blame] | 231 | #ifdef macintosh |
| 232 | /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */ |
| 233 | if ( strcmp(ext, ".pyc") == 0 || getfiletype(filename) == 'PYC ' || |
| 234 | getfiletype(filename) == 'APPL' ) { |
| 235 | #else |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 236 | if ( strcmp(ext, ".pyc") == 0 ) { |
Jack Jansen | bd06e96 | 1995-02-13 11:44:56 +0000 | [diff] [blame] | 237 | #endif /* macintosh */ |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 238 | /* Try to run a pyc file. First, re-open in binary */ |
Jack Jansen | e00637b | 1994-12-14 12:58:37 +0000 | [diff] [blame] | 239 | /* Don't close, done in main: fclose(fp); */ |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 240 | if( (fp = fopen(filename, "rb")) == NULL ) { |
| 241 | fprintf(stderr, "python: Can't reopen .pyc file\n"); |
| 242 | return -1; |
| 243 | } |
| 244 | v = run_pyc_file(fp, filename, d, d); |
| 245 | } else { |
| 246 | v = run_file(fp, filename, file_input, d, d); |
| 247 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 248 | if (v == NULL) { |
| 249 | print_error(); |
| 250 | return -1; |
| 251 | } |
| 252 | DECREF(v); |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 253 | flushline(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | int |
| 258 | run_command(command) |
| 259 | char *command; |
| 260 | { |
| 261 | object *m, *d, *v; |
| 262 | m = add_module("__main__"); |
| 263 | if (m == NULL) |
| 264 | return -1; |
| 265 | d = getmoduledict(m); |
| 266 | v = run_string(command, file_input, d, d); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 267 | if (v == NULL) { |
| 268 | print_error(); |
| 269 | return -1; |
| 270 | } |
| 271 | DECREF(v); |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 272 | flushline(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | void |
| 277 | print_error() |
| 278 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 279 | object *exception, *v, *tb, *f; |
| 280 | err_fetch(&exception, &v, &tb); |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 281 | flushline(); |
Guido van Rossum | 527c264 | 1995-03-30 11:01:44 +0000 | [diff] [blame] | 282 | fflush(stdout); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 283 | if (exception == NULL) |
| 284 | fatal("print_error called but no exception"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 285 | if (exception == SystemExit) { |
| 286 | if (v == NULL || v == None) |
| 287 | goaway(0); |
| 288 | if (is_intobject(v)) |
| 289 | goaway((int)getintvalue(v)); |
| 290 | else { |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 291 | /* OK to use real stderr here */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 292 | printobject(v, stderr, PRINT_RAW); |
| 293 | fprintf(stderr, "\n"); |
| 294 | goaway(1); |
| 295 | } |
| 296 | } |
| 297 | sysset("last_type", exception); |
| 298 | sysset("last_value", v); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 299 | sysset("last_traceback", tb); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 300 | f = sysget("stderr"); |
| 301 | if (f == NULL) |
| 302 | fprintf(stderr, "lost sys.stderr\n"); |
| 303 | else { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 304 | tb_print(tb, f); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 305 | if (exception == SyntaxError) { |
| 306 | object *message; |
| 307 | char *filename, *text; |
| 308 | int lineno, offset; |
| 309 | if (!getargs(v, "(O(ziiz))", &message, |
| 310 | &filename, &lineno, &offset, &text)) |
| 311 | err_clear(); |
| 312 | else { |
| 313 | char buf[10]; |
| 314 | writestring(" File \"", f); |
| 315 | if (filename == NULL) |
| 316 | writestring("<string>", f); |
| 317 | else |
| 318 | writestring(filename, f); |
| 319 | writestring("\", line ", f); |
| 320 | sprintf(buf, "%d", lineno); |
| 321 | writestring(buf, f); |
| 322 | writestring("\n", f); |
| 323 | if (text != NULL) { |
Guido van Rossum | 798199d | 1994-09-19 08:08:50 +0000 | [diff] [blame] | 324 | char *nl; |
| 325 | if (offset > 0 && |
| 326 | offset == strlen(text)) |
| 327 | offset--; |
| 328 | for (;;) { |
| 329 | nl = strchr(text, '\n'); |
| 330 | if (nl == NULL || |
| 331 | nl-text >= offset) |
| 332 | break; |
| 333 | offset -= (nl+1-text); |
| 334 | text = nl+1; |
| 335 | } |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 336 | while (*text == ' ' || *text == '\t') { |
| 337 | text++; |
| 338 | offset--; |
| 339 | } |
| 340 | writestring(" ", f); |
| 341 | writestring(text, f); |
| 342 | if (*text == '\0' || |
| 343 | text[strlen(text)-1] != '\n') |
| 344 | writestring("\n", f); |
| 345 | writestring(" ", f); |
| 346 | offset--; |
| 347 | while (offset > 0) { |
| 348 | writestring(" ", f); |
| 349 | offset--; |
| 350 | } |
| 351 | writestring("^\n", f); |
| 352 | } |
| 353 | INCREF(message); |
| 354 | DECREF(v); |
| 355 | v = message; |
| 356 | } |
| 357 | } |
Guido van Rossum | 262e124 | 1995-02-07 15:30:45 +0000 | [diff] [blame] | 358 | if (is_classobject(exception)) { |
| 359 | object* className = ((classobject*)exception)->cl_name; |
| 360 | if (className == NULL) |
| 361 | writestring("<unknown>", f); |
| 362 | else { |
| 363 | if (writeobject(className, f, PRINT_RAW) != 0) |
| 364 | err_clear(); |
| 365 | } |
| 366 | } else { |
| 367 | if (writeobject(exception, f, PRINT_RAW) != 0) |
| 368 | err_clear(); |
| 369 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 370 | if (v != NULL && v != None) { |
| 371 | writestring(": ", f); |
| 372 | if (writeobject(v, f, PRINT_RAW) != 0) |
| 373 | err_clear(); |
| 374 | } |
| 375 | writestring("\n", f); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 376 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 377 | XDECREF(exception); |
| 378 | XDECREF(v); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 379 | XDECREF(tb); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | object * |
| 383 | run_string(str, start, globals, locals) |
| 384 | char *str; |
| 385 | int start; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 386 | object *globals, *locals; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 387 | { |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 388 | return run_err_node(parse_string(str, start), |
| 389 | "<string>", globals, locals); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | object * |
| 393 | run_file(fp, filename, start, globals, locals) |
| 394 | FILE *fp; |
| 395 | char *filename; |
| 396 | int start; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 397 | object *globals, *locals; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 398 | { |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 399 | return run_err_node(parse_file(fp, filename, start), |
| 400 | filename, globals, locals); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 403 | static object * |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 404 | run_err_node(n, filename, globals, locals) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 405 | node *n; |
| 406 | char *filename; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 407 | object *globals, *locals; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 408 | { |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 409 | if (n == NULL) |
| 410 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 411 | return run_node(n, filename, globals, locals); |
| 412 | } |
| 413 | |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 414 | static object * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 415 | run_node(n, filename, globals, locals) |
| 416 | node *n; |
| 417 | char *filename; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 418 | object *globals, *locals; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 419 | { |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 420 | codeobject *co; |
| 421 | object *v; |
| 422 | co = compile(n, filename); |
| 423 | freetree(n); |
| 424 | if (co == NULL) |
| 425 | return NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 426 | v = eval_code(co, globals, locals); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 427 | DECREF(co); |
| 428 | return v; |
| 429 | } |
| 430 | |
Guido van Rossum | 8ae87c0 | 1995-01-26 00:40:38 +0000 | [diff] [blame] | 431 | static object * |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 432 | run_pyc_file(fp, filename, globals, locals) |
| 433 | FILE *fp; |
| 434 | char *filename; |
| 435 | object *globals, *locals; |
| 436 | { |
| 437 | codeobject *co; |
| 438 | object *v; |
| 439 | long magic; |
| 440 | long get_pyc_magic(); |
| 441 | |
| 442 | magic = rd_long(fp); |
| 443 | if (magic != get_pyc_magic()) { |
| 444 | err_setstr(RuntimeError, |
| 445 | "Bad magic number in .pyc file"); |
| 446 | return NULL; |
| 447 | } |
| 448 | (void) rd_long(fp); |
| 449 | v = rd_object(fp); |
| 450 | fclose(fp); |
| 451 | if (v == NULL || !is_codeobject(v)) { |
| 452 | XDECREF(v); |
| 453 | err_setstr(RuntimeError, |
| 454 | "Bad code object in .pyc file"); |
| 455 | return NULL; |
| 456 | } |
| 457 | co = (codeobject *)v; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 458 | v = eval_code(co, globals, locals); |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 459 | DECREF(co); |
| 460 | return v; |
| 461 | } |
| 462 | |
| 463 | object * |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 464 | compile_string(str, filename, start) |
| 465 | char *str; |
| 466 | char *filename; |
| 467 | int start; |
| 468 | { |
| 469 | node *n; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 470 | codeobject *co; |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 471 | n = parse_string(str, start); |
| 472 | if (n == NULL) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 473 | return NULL; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 474 | co = compile(n, filename); |
| 475 | freetree(n); |
| 476 | return (object *)co; |
| 477 | } |
| 478 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 479 | /* Simplified interface to parsefile -- return node or set exception */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 480 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 481 | node * |
| 482 | parse_file(fp, filename, start) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 483 | FILE *fp; |
| 484 | char *filename; |
| 485 | int start; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 486 | { |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 487 | node *n; |
| 488 | perrdetail err; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 489 | BGN_SAVE |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 490 | n = parsefile(fp, filename, &gram, start, |
| 491 | (char *)0, (char *)0, &err); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 492 | END_SAVE |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 493 | if (n == NULL) |
| 494 | err_input(&err); |
| 495 | return n; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 498 | /* Simplified interface to parsestring -- return node or set exception */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 499 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 500 | node * |
| 501 | parse_string(str, start) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 502 | char *str; |
| 503 | int start; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 504 | { |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 505 | node *n; |
| 506 | perrdetail err; |
| 507 | n = parsestring(str, &gram, start, &err); |
| 508 | if (n == NULL) |
| 509 | err_input(&err); |
| 510 | return n; |
| 511 | } |
| 512 | |
| 513 | /* Set the error appropriate to the given input error code (see errcode.h) */ |
| 514 | |
| 515 | static void |
| 516 | err_input(err) |
| 517 | perrdetail *err; |
| 518 | { |
| 519 | object *v, *w; |
| 520 | char *msg = NULL; |
| 521 | v = mkvalue("(ziiz)", err->filename, |
| 522 | err->lineno, err->offset, err->text); |
| 523 | if (err->text != NULL) { |
| 524 | free(err->text); |
| 525 | err->text = NULL; |
| 526 | } |
| 527 | switch (err->error) { |
| 528 | case E_SYNTAX: |
| 529 | msg = "invalid syntax"; |
| 530 | break; |
| 531 | case E_TOKEN: |
| 532 | msg = "invalid token"; |
| 533 | |
| 534 | break; |
| 535 | case E_INTR: |
| 536 | err_set(KeyboardInterrupt); |
| 537 | return; |
| 538 | case E_NOMEM: |
| 539 | err_nomem(); |
| 540 | return; |
| 541 | case E_EOF: |
| 542 | msg = "unexpected EOF while parsing"; |
| 543 | break; |
| 544 | default: |
| 545 | fprintf(stderr, "error=%d\n", err->error); |
| 546 | msg = "unknown parsing error"; |
| 547 | break; |
| 548 | } |
| 549 | w = mkvalue("(sO)", msg, v); |
| 550 | XDECREF(v); |
| 551 | err_setval(SyntaxError, w); |
| 552 | XDECREF(w); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /* Print fatal error message and abort */ |
| 556 | |
| 557 | void |
| 558 | fatal(msg) |
| 559 | char *msg; |
| 560 | { |
Guido van Rossum | 83dd6c3 | 1994-09-29 09:38:33 +0000 | [diff] [blame] | 561 | fprintf(stderr, "Fatal Python error: %s\n", msg); |
Guido van Rossum | 8ae87c0 | 1995-01-26 00:40:38 +0000 | [diff] [blame] | 562 | #ifdef macintosh |
| 563 | for (;;); |
| 564 | #endif |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 565 | #ifdef MS_WIN32 |
Guido van Rossum | a44823b | 1995-03-14 15:01:17 +0000 | [diff] [blame] | 566 | OutputDebugString("Fatal Python error:"); |
| 567 | OutputDebugString(msg); |
| 568 | OutputDebugString("\n"); |
| 569 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 570 | abort(); |
| 571 | } |
| 572 | |
| 573 | /* Clean up and exit */ |
| 574 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 575 | #ifdef WITH_THREAD |
| 576 | #include "thread.h" |
| 577 | int threads_started = 0; /* Set by threadmodule.c and maybe others */ |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 578 | #endif |
| 579 | |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 580 | #define NEXITFUNCS 32 |
| 581 | static void (*exitfuncs[NEXITFUNCS])(); |
| 582 | static int nexitfuncs = 0; |
| 583 | |
| 584 | int Py_AtExit(func) |
| 585 | void (*func) PROTO((void)); |
| 586 | { |
| 587 | if (nexitfuncs >= NEXITFUNCS) |
| 588 | return -1; |
| 589 | exitfuncs[nexitfuncs++] = func; |
| 590 | return 0; |
| 591 | } |
| 592 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 593 | void |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 594 | cleanup() |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 595 | { |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 596 | object *exitfunc = sysget("exitfunc"); |
| 597 | |
| 598 | if (exitfunc) { |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 599 | object *res; |
Guido van Rossum | 1c45ca3 | 1995-10-07 19:14:01 +0000 | [diff] [blame] | 600 | INCREF(exitfunc); |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 601 | sysset("exitfunc", (object *)NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 602 | res = call_object(exitfunc, (object *)NULL); |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 603 | if (res == NULL) { |
| 604 | fprintf(stderr, "Error in sys.exitfunc:\n"); |
| 605 | print_error(); |
| 606 | } |
Guido van Rossum | 1c45ca3 | 1995-10-07 19:14:01 +0000 | [diff] [blame] | 607 | DECREF(exitfunc); |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 610 | flushline(); |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 611 | |
| 612 | while (nexitfuncs > 0) |
| 613 | (*exitfuncs[--nexitfuncs])(); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 616 | #ifdef COUNT_ALLOCS |
| 617 | extern void dump_counts PROTO((void)); |
| 618 | #endif |
| 619 | |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 620 | void |
| 621 | goaway(sts) |
| 622 | int sts; |
| 623 | { |
| 624 | cleanup(); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 625 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 626 | #ifdef COUNT_ALLOCS |
| 627 | dump_counts(); |
| 628 | #endif |
| 629 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 630 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 631 | |
| 632 | /* Other threads may still be active, so skip most of the |
| 633 | cleanup actions usually done (these are mostly for |
| 634 | debugging anyway). */ |
| 635 | |
Guido van Rossum | df72a65 | 1992-08-12 15:27:32 +0000 | [diff] [blame] | 636 | (void) save_thread(); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 637 | #ifndef NO_EXIT_PROG |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 638 | if (threads_started) |
| 639 | _exit_prog(sts); |
| 640 | else |
| 641 | exit_prog(sts); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 642 | #else /* !NO_EXIT_PROG */ |
| 643 | if (threads_started) |
| 644 | _exit(sts); |
| 645 | else |
| 646 | exit(sts); |
| 647 | #endif /* !NO_EXIT_PROG */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 648 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 649 | #else /* WITH_THREAD */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 650 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 651 | doneimport(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 652 | |
| 653 | err_clear(); |
| 654 | |
Guido van Rossum | aae0d32 | 1996-05-22 16:35:33 +0000 | [diff] [blame] | 655 | #ifdef Py_REF_DEBUG |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 656 | fprintf(stderr, "[%ld refs]\n", _Py_RefTotal); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 657 | #endif |
| 658 | |
Guido van Rossum | aae0d32 | 1996-05-22 16:35:33 +0000 | [diff] [blame] | 659 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 660 | if (askyesno("Print left references?")) { |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 661 | _Py_PrintReferences(stderr); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 662 | } |
Guido van Rossum | aae0d32 | 1996-05-22 16:35:33 +0000 | [diff] [blame] | 663 | #endif /* Py_TRACE_REFS */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 664 | |
Jack Jansen | 66a8977 | 1995-10-27 13:22:14 +0000 | [diff] [blame] | 665 | #ifdef macintosh |
| 666 | PyMac_Exit(sts); |
| 667 | #else |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 668 | exit(sts); |
Jack Jansen | 66a8977 | 1995-10-27 13:22:14 +0000 | [diff] [blame] | 669 | #endif |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 670 | #endif /* WITH_THREAD */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 671 | /*NOTREACHED*/ |
| 672 | } |
| 673 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 674 | #ifdef HAVE_SIGNAL_H |
| 675 | static RETSIGTYPE |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 676 | sighandler(sig) |
| 677 | int sig; |
| 678 | { |
| 679 | signal(sig, SIG_DFL); /* Don't catch recursive signals */ |
| 680 | cleanup(); /* Do essential clean-up */ |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 681 | #ifdef HAVE_GETPID |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 682 | kill(getpid(), sig); /* Pretend the signal killed us */ |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 683 | #else |
| 684 | exit(1); |
| 685 | #endif |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 686 | /*NOTREACHED*/ |
| 687 | } |
| 688 | #endif |
| 689 | |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 690 | static void |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 691 | initsigs() |
| 692 | { |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 693 | RETSIGTYPE (*t)(); |
| 694 | #ifdef HAVE_SIGNAL_H |
| 695 | #ifdef SIGPIPE |
| 696 | signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 697 | #endif |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 698 | #ifdef SIGHUP |
| 699 | t = signal(SIGHUP, SIG_IGN); |
| 700 | if (t == SIG_DFL) |
| 701 | signal(SIGHUP, sighandler); |
| 702 | else |
| 703 | signal(SIGHUP, t); |
| 704 | #endif |
| 705 | #ifdef SIGTERM |
| 706 | t = signal(SIGTERM, SIG_IGN); |
| 707 | if (t == SIG_DFL) |
| 708 | signal(SIGTERM, sighandler); |
| 709 | else |
| 710 | signal(SIGTERM, t); |
| 711 | #endif |
| 712 | #endif /* HAVE_SIGNAL_H */ |
| 713 | initintr(); /* May imply initsignal() */ |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Guido van Rossum | aae0d32 | 1996-05-22 16:35:33 +0000 | [diff] [blame] | 716 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 717 | /* Ask a yes/no question */ |
| 718 | |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 719 | int |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 720 | askyesno(prompt) |
| 721 | char *prompt; |
| 722 | { |
| 723 | char buf[256]; |
| 724 | |
| 725 | printf("%s [ny] ", prompt); |
| 726 | if (fgets(buf, sizeof buf, stdin) == NULL) |
| 727 | return 0; |
| 728 | return buf[0] == 'y' || buf[0] == 'Y'; |
| 729 | } |
| 730 | #endif |
| 731 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 732 | #ifdef MPW |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 733 | |
| 734 | /* Check for file descriptor connected to interactive device. |
| 735 | Pretend that stdin is always interactive, other files never. */ |
| 736 | |
| 737 | int |
| 738 | isatty(fd) |
| 739 | int fd; |
| 740 | { |
| 741 | return fd == fileno(stdin); |
| 742 | } |
| 743 | |
| 744 | #endif |