comparison Python/pythonrun.c @ 5170:f5d707ce4223 legacy-trunk

Greatly renamed. Not a very thorough job -- I'm going to restructure it anyway.
author Guido van Rossum <guido@python.org>
date Wed, 05 Mar 1997 00:20:32 +0000
parents a9a6d416b945
children 07ed52e200db
comparison
equal deleted inserted replaced
5169:2ea9c425a767 5170:f5d707ce4223
29 29
30 ******************************************************************/ 30 ******************************************************************/
31 31
32 /* Python interpreter top-level routines, including init/exit */ 32 /* Python interpreter top-level routines, including init/exit */
33 33
34 #include "allobjects.h" 34 #include "Python.h"
35 35
36 #include "grammar.h" 36 #include "grammar.h"
37 #include "node.h" 37 #include "node.h"
38 #include "parsetok.h" 38 #include "parsetok.h"
39 #include "graminit.h" 39 #include "graminit.h"
40 #undef argument /* Avoid conflict on Mac */ 40 #undef argument /* Avoid conflict on Mac */
41 #include "errcode.h" 41 #include "errcode.h"
42 #include "sysmodule.h"
43 #include "bltinmodule.h"
44 #include "compile.h" 42 #include "compile.h"
45 #include "eval.h" 43 #include "eval.h"
46 #include "ceval.h"
47 #include "import.h"
48 #include "marshal.h" 44 #include "marshal.h"
49 45
50 #ifdef HAVE_UNISTD_H 46 #ifdef HAVE_UNISTD_H
51 #include <unistd.h> 47 #include <unistd.h>
52 #endif 48 #endif
59 #undef BYTE 55 #undef BYTE
60 #undef arglist 56 #undef arglist
61 #include "windows.h" 57 #include "windows.h"
62 #endif 58 #endif
63 59
64 extern char *getpythonpath(); 60 extern char *Py_GetPath();
65 61
66 extern grammar gram; /* From graminit.c */ 62 extern grammar _PyParser_Grammar; /* From graminit.c */
67 63
68 /* Forward */ 64 /* Forward */
69 static void initmain PROTO((void)); 65 static void initmain Py_PROTO((void));
70 static object *run_err_node PROTO((node *n, char *filename, 66 static PyObject *run_err_node Py_PROTO((node *n, char *filename,
71 object *globals, object *locals)); 67 PyObject *globals, PyObject *locals));
72 static object *run_node PROTO((node *n, char *filename, 68 static PyObject *run_node Py_PROTO((node *n, char *filename,
73 object *globals, object *locals)); 69 PyObject *globals, PyObject *locals));
74 static object *run_pyc_file PROTO((FILE *fp, char *filename, 70 static PyObject *run_pyc_file Py_PROTO((FILE *fp, char *filename,
75 object *globals, object *locals)); 71 PyObject *globals, PyObject *locals));
76 static void err_input PROTO((perrdetail *)); 72 static void err_input Py_PROTO((perrdetail *));
77 static void initsigs PROTO((void)); 73 static void initsigs Py_PROTO((void));
78 74
79 int debugging; /* Needed by parser.c */ 75 int Py_DebugFlag; /* Needed by parser.c */
80 int verbose; /* Needed by import.c */ 76 int Py_VerboseFlag; /* Needed by import.c */
81 int suppress_print; /* Needed by ceval.c */ 77 int Py_SuppressPrintingFlag; /* Needed by ceval.c */
82 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ 78 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
83 79
84 /* Initialize all */ 80 /* Initialize all */
85 81
86 void 82 void
87 initall() 83 Py_Initialize()
88 { 84 {
89 static int inited; 85 static int inited;
90 86
91 if (inited) 87 if (inited)
92 return; 88 return;
93 inited = 1; 89 inited = 1;
94 90
95 initimport(); 91 PyImport_Init();
96 92
97 /* Modules '__builtin__' and 'sys' are initialized here, 93 /* Modules '__builtin__' and 'sys' are initialized here,
98 they are needed by random bits of the interpreter. 94 they are needed by random bits of the interpreter.
99 All other modules are optional and are initialized 95 All other modules are optional and are initialized
100 when they are first imported. */ 96 when they are first imported. */
101 97
102 initbuiltin(); /* Also initializes builtin exceptions */ 98 PyBuiltin_Init(); /* Also initializes builtin exceptions */
103 initsys(); 99 PySys_Init();
104 100
105 setpythonpath(getpythonpath()); 101 PySys_SetPath(Py_GetPath());
106 102
107 initsigs(); /* Signal handling stuff, including initintr() */ 103 initsigs(); /* Signal handling stuff, including initintr() */
108 104
109 initmain(); 105 initmain();
110 } 106 }
112 /* Create __main__ module */ 108 /* Create __main__ module */
113 109
114 static void 110 static void
115 initmain() 111 initmain()
116 { 112 {
117 object *m, *d; 113 PyObject *m, *d;
118 m = add_module("__main__"); 114 m = PyImport_AddModule("__main__");
119 if (m == NULL) 115 if (m == NULL)
120 fatal("can't create __main__ module"); 116 Py_FatalError("can't create __main__ module");
121 d = getmoduledict(m); 117 d = PyModule_GetDict(m);
122 if (dictlookup(d, "__builtins__") == NULL) { 118 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
123 if (dictinsert(d, "__builtins__", getbuiltins())) 119 if (PyDict_SetItemString(d, "__builtins__",
124 fatal("can't add __builtins__ to __main__"); 120 PyEval_GetBuiltins()))
121 Py_FatalError("can't add __builtins__ to __main__");
125 } 122 }
126 } 123 }
127 124
128 /* Parse input from a file and execute it */ 125 /* Parse input from a file and execute it */
129 126
130 int 127 int
131 run(fp, filename) 128 PyRun_AnyFile(fp, filename)
132 FILE *fp; 129 FILE *fp;
133 char *filename; 130 char *filename;
134 { 131 {
135 if (filename == NULL) 132 if (filename == NULL)
136 filename = "???"; 133 filename = "???";
137 if (Py_FdIsInteractive(fp, filename)) 134 if (Py_FdIsInteractive(fp, filename))
138 return run_tty_loop(fp, filename); 135 return PyRun_InteractiveLoop(fp, filename);
139 else 136 else
140 return run_script(fp, filename); 137 return PyRun_SimpleFile(fp, filename);
141 } 138 }
142 139
143 int 140 int
144 run_tty_loop(fp, filename) 141 PyRun_InteractiveLoop(fp, filename)
145 FILE *fp; 142 FILE *fp;
146 char *filename; 143 char *filename;
147 { 144 {
148 object *v; 145 PyObject *v;
149 int ret; 146 int ret;
150 v = sysget("ps1"); 147 v = PySys_GetObject("ps1");
151 if (v == NULL) { 148 if (v == NULL) {
152 sysset("ps1", v = newstringobject(">>> ")); 149 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
153 XDECREF(v); 150 Py_XDECREF(v);
154 } 151 }
155 v = sysget("ps2"); 152 v = PySys_GetObject("ps2");
156 if (v == NULL) { 153 if (v == NULL) {
157 sysset("ps2", v = newstringobject("... ")); 154 PySys_SetObject("ps2", v = PyString_FromString("... "));
158 XDECREF(v); 155 Py_XDECREF(v);
159 } 156 }
160 for (;;) { 157 for (;;) {
161 ret = run_tty_1(fp, filename); 158 ret = PyRun_InteractiveOne(fp, filename);
162 #ifdef Py_REF_DEBUG 159 #ifdef Py_REF_DEBUG
163 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal); 160 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
164 #endif 161 #endif
165 if (ret == E_EOF) 162 if (ret == E_EOF)
166 return 0; 163 return 0;
170 */ 167 */
171 } 168 }
172 } 169 }
173 170
174 int 171 int
175 run_tty_1(fp, filename) 172 PyRun_InteractiveOne(fp, filename)
176 FILE *fp; 173 FILE *fp;
177 char *filename; 174 char *filename;
178 { 175 {
179 object *m, *d, *v, *w; 176 PyObject *m, *d, *v, *w;
180 node *n; 177 node *n;
181 perrdetail err; 178 perrdetail err;
182 char *ps1, *ps2; 179 char *ps1, *ps2;
183 v = sysget("ps1"); 180 v = PySys_GetObject("ps1");
184 w = sysget("ps2"); 181 w = PySys_GetObject("ps2");
185 if (v != NULL && is_stringobject(v)) { 182 if (v != NULL && PyString_Check(v)) {
186 INCREF(v); 183 Py_INCREF(v);
187 ps1 = getstringvalue(v); 184 ps1 = PyString_AsString(v);
188 } 185 }
189 else { 186 else {
190 v = NULL; 187 v = NULL;
191 ps1 = ""; 188 ps1 = "";
192 } 189 }
193 if (w != NULL && is_stringobject(w)) { 190 if (w != NULL && PyString_Check(w)) {
194 INCREF(w); 191 Py_INCREF(w);
195 ps2 = getstringvalue(w); 192 ps2 = PyString_AsString(w);
196 } 193 }
197 else { 194 else {
198 w = NULL; 195 w = NULL;
199 ps2 = ""; 196 ps2 = "";
200 } 197 }
201 BGN_SAVE 198 Py_BEGIN_ALLOW_THREADS
202 n = parsefile(fp, filename, &gram, single_input, ps1, ps2, &err); 199 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar,
203 END_SAVE 200 single_input, ps1, ps2, &err);
204 XDECREF(v); 201 Py_END_ALLOW_THREADS
205 XDECREF(w); 202 Py_XDECREF(v);
203 Py_XDECREF(w);
206 if (n == NULL) { 204 if (n == NULL) {
207 if (err.error == E_EOF) { 205 if (err.error == E_EOF) {
208 if (err.text) 206 if (err.text)
209 free(err.text); 207 free(err.text);
210 return E_EOF; 208 return E_EOF;
211 } 209 }
212 err_input(&err); 210 err_input(&err);
213 print_error(); 211 PyErr_Print();
214 return err.error; 212 return err.error;
215 } 213 }
216 m = add_module("__main__"); 214 m = PyImport_AddModule("__main__");
217 if (m == NULL) 215 if (m == NULL)
218 return -1; 216 return -1;
219 d = getmoduledict(m); 217 d = PyModule_GetDict(m);
220 v = run_node(n, filename, d, d); 218 v = run_node(n, filename, d, d);
221 if (v == NULL) { 219 if (v == NULL) {
222 print_error(); 220 PyErr_Print();
223 return -1; 221 return -1;
224 } 222 }
225 DECREF(v); 223 Py_DECREF(v);
226 flushline(); 224 Py_FlushLine();
227 return 0; 225 return 0;
228 } 226 }
229 227
230 int 228 int
231 run_script(fp, filename) 229 PyRun_SimpleFile(fp, filename)
232 FILE *fp; 230 FILE *fp;
233 char *filename; 231 char *filename;
234 { 232 {
235 object *m, *d, *v; 233 PyObject *m, *d, *v;
236 char *ext; 234 char *ext;
237 235
238 m = add_module("__main__"); 236 m = PyImport_AddModule("__main__");
239 if (m == NULL) 237 if (m == NULL)
240 return -1; 238 return -1;
241 d = getmoduledict(m); 239 d = PyModule_GetDict(m);
242 ext = filename + strlen(filename) - 4; 240 ext = filename + strlen(filename) - 4;
243 #ifdef macintosh 241 #ifdef macintosh
244 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */ 242 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
245 if ( strcmp(ext, ".pyc") == 0 || getfiletype(filename) == 'PYC ' || 243 if ( strcmp(ext, ".pyc") == 0 || getfiletype(filename) == 'PYC ' ||
246 getfiletype(filename) == 'APPL' ) { 244 getfiletype(filename) == 'APPL' ) {
253 fprintf(stderr, "python: Can't reopen .pyc file\n"); 251 fprintf(stderr, "python: Can't reopen .pyc file\n");
254 return -1; 252 return -1;
255 } 253 }
256 v = run_pyc_file(fp, filename, d, d); 254 v = run_pyc_file(fp, filename, d, d);
257 } else { 255 } else {
258 v = run_file(fp, filename, file_input, d, d); 256 v = PyRun_File(fp, filename, file_input, d, d);
259 } 257 }
260 if (v == NULL) { 258 if (v == NULL) {
261 print_error(); 259 PyErr_Print();
262 return -1; 260 return -1;
263 } 261 }
264 DECREF(v); 262 Py_DECREF(v);
265 flushline(); 263 Py_FlushLine();
266 return 0; 264 return 0;
267 } 265 }
268 266
269 int 267 int
270 run_command(command) 268 PyRun_SimpleString(command)
271 char *command; 269 char *command;
272 { 270 {
273 object *m, *d, *v; 271 PyObject *m, *d, *v;
274 m = add_module("__main__"); 272 m = PyImport_AddModule("__main__");
275 if (m == NULL) 273 if (m == NULL)
276 return -1; 274 return -1;
277 d = getmoduledict(m); 275 d = PyModule_GetDict(m);
278 v = run_string(command, file_input, d, d); 276 v = PyRun_String(command, file_input, d, d);
279 if (v == NULL) { 277 if (v == NULL) {
280 print_error(); 278 PyErr_Print();
281 return -1; 279 return -1;
282 } 280 }
283 DECREF(v); 281 Py_DECREF(v);
284 flushline(); 282 Py_FlushLine();
285 return 0; 283 return 0;
286 } 284 }
287 285
288 void 286 void
289 print_error() 287 PyErr_Print()
290 { 288 {
291 object *exception, *v, *tb, *f; 289 PyObject *exception, *v, *tb, *f;
292 err_fetch(&exception, &v, &tb); 290 PyErr_Fetch(&exception, &v, &tb);
293 flushline(); 291 Py_FlushLine();
294 fflush(stdout); 292 fflush(stdout);
295 if (exception == NULL) 293 if (exception == NULL)
296 fatal("print_error called but no exception"); 294 Py_FatalError("print_error called but no exception");
297 if (exception == SystemExit) { 295 if (exception == PyExc_SystemExit) {
298 if (v == NULL || v == None) 296 if (v == NULL || v == Py_None)
299 goaway(0); 297 Py_Exit(0);
300 if (is_intobject(v)) 298 if (PyInt_Check(v))
301 goaway((int)getintvalue(v)); 299 Py_Exit((int)PyInt_AsLong(v));
302 else { 300 else {
303 /* OK to use real stderr here */ 301 /* OK to use real stderr here */
304 printobject(v, stderr, PRINT_RAW); 302 PyObject_Print(v, stderr, Py_PRINT_RAW);
305 fprintf(stderr, "\n"); 303 fprintf(stderr, "\n");
306 goaway(1); 304 Py_Exit(1);
307 } 305 }
308 } 306 }
309 sysset("last_type", exception); 307 PySys_SetObject("last_type", exception);
310 sysset("last_value", v); 308 PySys_SetObject("last_value", v);
311 sysset("last_traceback", tb); 309 PySys_SetObject("last_traceback", tb);
312 f = sysget("stderr"); 310 f = PySys_GetObject("stderr");
313 if (f == NULL) 311 if (f == NULL)
314 fprintf(stderr, "lost sys.stderr\n"); 312 fprintf(stderr, "lost sys.stderr\n");
315 else { 313 else {
316 tb_print(tb, f); 314 PyTraceBack_Print(tb, f);
317 if (exception == SyntaxError) { 315 if (exception == PyExc_SyntaxError) {
318 object *message; 316 PyObject *message;
319 char *filename, *text; 317 char *filename, *text;
320 int lineno, offset; 318 int lineno, offset;
321 if (!getargs(v, "(O(ziiz))", &message, 319 if (!PyArg_Parse(v, "(O(ziiz))", &message,
322 &filename, &lineno, &offset, &text)) 320 &filename, &lineno, &offset, &text))
323 err_clear(); 321 PyErr_Clear();
324 else { 322 else {
325 char buf[10]; 323 char buf[10];
326 writestring(" File \"", f); 324 PyFile_WriteString(" File \"", f);
327 if (filename == NULL) 325 if (filename == NULL)
328 writestring("<string>", f); 326 PyFile_WriteString("<string>", f);
329 else 327 else
330 writestring(filename, f); 328 PyFile_WriteString(filename, f);
331 writestring("\", line ", f); 329 PyFile_WriteString("\", line ", f);
332 sprintf(buf, "%d", lineno); 330 sprintf(buf, "%d", lineno);
333 writestring(buf, f); 331 PyFile_WriteString(buf, f);
334 writestring("\n", f); 332 PyFile_WriteString("\n", f);
335 if (text != NULL) { 333 if (text != NULL) {
336 char *nl; 334 char *nl;
337 if (offset > 0 && 335 if (offset > 0 &&
338 offset == strlen(text)) 336 offset == strlen(text))
339 offset--; 337 offset--;
347 } 345 }
348 while (*text == ' ' || *text == '\t') { 346 while (*text == ' ' || *text == '\t') {
349 text++; 347 text++;
350 offset--; 348 offset--;
351 } 349 }
352 writestring(" ", f); 350 PyFile_WriteString(" ", f);
353 writestring(text, f); 351 PyFile_WriteString(text, f);
354 if (*text == '\0' || 352 if (*text == '\0' ||
355 text[strlen(text)-1] != '\n') 353 text[strlen(text)-1] != '\n')
356 writestring("\n", f); 354 PyFile_WriteString("\n", f);
357 writestring(" ", f); 355 PyFile_WriteString(" ", f);
358 offset--; 356 offset--;
359 while (offset > 0) { 357 while (offset > 0) {
360 writestring(" ", f); 358 PyFile_WriteString(" ", f);
361 offset--; 359 offset--;
362 } 360 }
363 writestring("^\n", f); 361 PyFile_WriteString("^\n", f);
364 } 362 }
365 INCREF(message); 363 Py_INCREF(message);
366 DECREF(v); 364 Py_DECREF(v);
367 v = message; 365 v = message;
368 } 366 }
369 } 367 }
370 if (is_classobject(exception)) { 368 if (PyClass_Check(exception)) {
371 object* className = ((classobject*)exception)->cl_name; 369 PyObject* className =
370 ((PyClassObject*)exception)->cl_name;
372 if (className == NULL) 371 if (className == NULL)
373 writestring("<unknown>", f); 372 PyFile_WriteString("<unknown>", f);
374 else { 373 else {
375 if (writeobject(className, f, PRINT_RAW) != 0) 374 if (PyFile_WriteObject(className, f,
376 err_clear(); 375 Py_PRINT_RAW) != 0)
376 PyErr_Clear();
377 } 377 }
378 } else { 378 } else {
379 if (writeobject(exception, f, PRINT_RAW) != 0) 379 if (PyFile_WriteObject(exception, f,
380 err_clear(); 380 Py_PRINT_RAW) != 0)
381 PyErr_Clear();
381 } 382 }
382 if (v != NULL && v != None) { 383 if (v != NULL && v != Py_None) {
383 writestring(": ", f); 384 PyFile_WriteString(": ", f);
384 if (writeobject(v, f, PRINT_RAW) != 0) 385 if (PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
385 err_clear(); 386 PyErr_Clear();
386 } 387 }
387 writestring("\n", f); 388 PyFile_WriteString("\n", f);
388 } 389 }
389 XDECREF(exception); 390 Py_XDECREF(exception);
390 XDECREF(v); 391 Py_XDECREF(v);
391 XDECREF(tb); 392 Py_XDECREF(tb);
392 } 393 }
393 394
394 object * 395 PyObject *
395 run_string(str, start, globals, locals) 396 PyRun_String(str, start, globals, locals)
396 char *str; 397 char *str;
397 int start; 398 int start;
398 object *globals, *locals; 399 PyObject *globals, *locals;
399 { 400 {
400 return run_err_node(parse_string(str, start), 401 return run_err_node(PyParser_SimpleParseString(str, start),
401 "<string>", globals, locals); 402 "<string>", globals, locals);
402 } 403 }
403 404
404 object * 405 PyObject *
405 run_file(fp, filename, start, globals, locals) 406 PyRun_File(fp, filename, start, globals, locals)
406 FILE *fp; 407 FILE *fp;
407 char *filename; 408 char *filename;
408 int start; 409 int start;
409 object *globals, *locals; 410 PyObject *globals, *locals;
410 { 411 {
411 return run_err_node(parse_file(fp, filename, start), 412 return run_err_node(PyParser_SimpleParseFile(fp, filename, start),
412 filename, globals, locals); 413 filename, globals, locals);
413 } 414 }
414 415
415 static object * 416 static PyObject *
416 run_err_node(n, filename, globals, locals) 417 run_err_node(n, filename, globals, locals)
417 node *n; 418 node *n;
418 char *filename; 419 char *filename;
419 object *globals, *locals; 420 PyObject *globals, *locals;
420 { 421 {
421 if (n == NULL) 422 if (n == NULL)
422 return NULL; 423 return NULL;
423 return run_node(n, filename, globals, locals); 424 return run_node(n, filename, globals, locals);
424 } 425 }
425 426
426 static object * 427 static PyObject *
427 run_node(n, filename, globals, locals) 428 run_node(n, filename, globals, locals)
428 node *n; 429 node *n;
429 char *filename; 430 char *filename;
430 object *globals, *locals; 431 PyObject *globals, *locals;
431 { 432 {
432 codeobject *co; 433 PyCodeObject *co;
433 object *v; 434 PyObject *v;
434 co = compile(n, filename); 435 co = PyNode_Compile(n, filename);
435 freetree(n); 436 PyNode_Free(n);
436 if (co == NULL) 437 if (co == NULL)
437 return NULL; 438 return NULL;
438 v = eval_code(co, globals, locals); 439 v = PyEval_EvalCode(co, globals, locals);
439 DECREF(co); 440 Py_DECREF(co);
440 return v; 441 return v;
441 } 442 }
442 443
443 static object * 444 static PyObject *
444 run_pyc_file(fp, filename, globals, locals) 445 run_pyc_file(fp, filename, globals, locals)
445 FILE *fp; 446 FILE *fp;
446 char *filename; 447 char *filename;
447 object *globals, *locals; 448 PyObject *globals, *locals;
448 { 449 {
449 codeobject *co; 450 PyCodeObject *co;
450 object *v; 451 PyObject *v;
451 long magic; 452 long magic;
452 long get_pyc_magic(); 453 long PyImport_GetMagicNumber();
453 454
454 magic = rd_long(fp); 455 magic = PyMarshal_ReadLongFromFile(fp);
455 if (magic != get_pyc_magic()) { 456 if (magic != PyImport_GetMagicNumber()) {
456 err_setstr(RuntimeError, 457 PyErr_SetString(PyExc_RuntimeError,
457 "Bad magic number in .pyc file"); 458 "Bad magic number in .pyc file");
458 return NULL; 459 return NULL;
459 } 460 }
460 (void) rd_long(fp); 461 (void) PyMarshal_ReadLongFromFile(fp);
461 v = rd_object(fp); 462 v = PyMarshal_ReadObjectFromFile(fp);
462 fclose(fp); 463 fclose(fp);
463 if (v == NULL || !is_codeobject(v)) { 464 if (v == NULL || !PyCode_Check(v)) {
464 XDECREF(v); 465 Py_XDECREF(v);
465 err_setstr(RuntimeError, 466 PyErr_SetString(PyExc_RuntimeError,
466 "Bad code object in .pyc file"); 467 "Bad code object in .pyc file");
467 return NULL; 468 return NULL;
468 } 469 }
469 co = (codeobject *)v; 470 co = (PyCodeObject *)v;
470 v = eval_code(co, globals, locals); 471 v = PyEval_EvalCode(co, globals, locals);
471 DECREF(co); 472 Py_DECREF(co);
472 return v; 473 return v;
473 } 474 }
474 475
475 object * 476 PyObject *
476 compile_string(str, filename, start) 477 Py_CompileString(str, filename, start)
477 char *str; 478 char *str;
478 char *filename; 479 char *filename;
479 int start; 480 int start;
480 { 481 {
481 node *n; 482 node *n;
482 codeobject *co; 483 PyCodeObject *co;
483 n = parse_string(str, start); 484 n = PyParser_SimpleParseString(str, start);
484 if (n == NULL) 485 if (n == NULL)
485 return NULL; 486 return NULL;
486 co = compile(n, filename); 487 co = PyNode_Compile(n, filename);
487 freetree(n); 488 PyNode_Free(n);
488 return (object *)co; 489 return (PyObject *)co;
489 } 490 }
490 491
491 /* Simplified interface to parsefile -- return node or set exception */ 492 /* Simplified interface to parsefile -- return node or set exception */
492 493
493 node * 494 node *
494 parse_file(fp, filename, start) 495 PyParser_SimpleParseFile(fp, filename, start)
495 FILE *fp; 496 FILE *fp;
496 char *filename; 497 char *filename;
497 int start; 498 int start;
498 { 499 {
499 node *n; 500 node *n;
500 perrdetail err; 501 perrdetail err;
501 BGN_SAVE 502 Py_BEGIN_ALLOW_THREADS
502 n = parsefile(fp, filename, &gram, start, 503 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start,
503 (char *)0, (char *)0, &err); 504 (char *)0, (char *)0, &err);
504 END_SAVE 505 Py_END_ALLOW_THREADS
505 if (n == NULL) 506 if (n == NULL)
506 err_input(&err); 507 err_input(&err);
507 return n; 508 return n;
508 } 509 }
509 510
510 /* Simplified interface to parsestring -- return node or set exception */ 511 /* Simplified interface to parsestring -- return node or set exception */
511 512
512 node * 513 node *
513 parse_string(str, start) 514 PyParser_SimpleParseString(str, start)
514 char *str; 515 char *str;
515 int start; 516 int start;
516 { 517 {
517 node *n; 518 node *n;
518 perrdetail err; 519 perrdetail err;
519 n = parsestring(str, &gram, start, &err); 520 n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err);
520 if (n == NULL) 521 if (n == NULL)
521 err_input(&err); 522 err_input(&err);
522 return n; 523 return n;
523 } 524 }
524 525
526 527
527 static void 528 static void
528 err_input(err) 529 err_input(err)
529 perrdetail *err; 530 perrdetail *err;
530 { 531 {
531 object *v, *w; 532 PyObject *v, *w;
532 char *msg = NULL; 533 char *msg = NULL;
533 v = mkvalue("(ziiz)", err->filename, 534 v = Py_BuildValue("(ziiz)", err->filename,
534 err->lineno, err->offset, err->text); 535 err->lineno, err->offset, err->text);
535 if (err->text != NULL) { 536 if (err->text != NULL) {
536 free(err->text); 537 free(err->text);
537 err->text = NULL; 538 err->text = NULL;
538 } 539 }
543 case E_TOKEN: 544 case E_TOKEN:
544 msg = "invalid token"; 545 msg = "invalid token";
545 546
546 break; 547 break;
547 case E_INTR: 548 case E_INTR:
548 err_set(KeyboardInterrupt); 549 PyErr_SetNone(PyExc_KeyboardInterrupt);
549 return; 550 return;
550 case E_NOMEM: 551 case E_NOMEM:
551 err_nomem(); 552 PyErr_NoMemory();
552 return; 553 return;
553 case E_EOF: 554 case E_EOF:
554 msg = "unexpected EOF while parsing"; 555 msg = "unexpected EOF while parsing";
555 break; 556 break;
556 default: 557 default:
557 fprintf(stderr, "error=%d\n", err->error); 558 fprintf(stderr, "error=%d\n", err->error);
558 msg = "unknown parsing error"; 559 msg = "unknown parsing error";
559 break; 560 break;
560 } 561 }
561 w = mkvalue("(sO)", msg, v); 562 w = Py_BuildValue("(sO)", msg, v);
562 XDECREF(v); 563 Py_XDECREF(v);
563 err_setval(SyntaxError, w); 564 PyErr_SetObject(PyExc_SyntaxError, w);
564 XDECREF(w); 565 Py_XDECREF(w);
565 } 566 }
566 567
567 /* Print fatal error message and abort */ 568 /* Print fatal error message and abort */
568 569
569 void 570 void
570 fatal(msg) 571 Py_FatalError(msg)
571 char *msg; 572 char *msg;
572 { 573 {
573 fprintf(stderr, "Fatal Python error: %s\n", msg); 574 fprintf(stderr, "Fatal Python error: %s\n", msg);
574 #ifdef macintosh 575 #ifdef macintosh
575 for (;;); 576 for (;;);
584 585
585 /* Clean up and exit */ 586 /* Clean up and exit */
586 587
587 #ifdef WITH_THREAD 588 #ifdef WITH_THREAD
588 #include "thread.h" 589 #include "thread.h"
589 int threads_started = 0; /* Set by threadmodule.c and maybe others */ 590 int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
590 #endif 591 #endif
591 592
592 #define NEXITFUNCS 32 593 #define NEXITFUNCS 32
593 static void (*exitfuncs[NEXITFUNCS])(); 594 static void (*exitfuncs[NEXITFUNCS])();
594 static int nexitfuncs = 0; 595 static int nexitfuncs = 0;
595 596
596 int Py_AtExit(func) 597 int Py_AtExit(func)
597 void (*func) PROTO((void)); 598 void (*func) Py_PROTO((void));
598 { 599 {
599 if (nexitfuncs >= NEXITFUNCS) 600 if (nexitfuncs >= NEXITFUNCS)
600 return -1; 601 return -1;
601 exitfuncs[nexitfuncs++] = func; 602 exitfuncs[nexitfuncs++] = func;
602 return 0; 603 return 0;
603 } 604 }
604 605
605 void 606 void
606 cleanup() 607 Py_Cleanup()
607 { 608 {
608 object *exitfunc = sysget("exitfunc"); 609 PyObject *exitfunc = PySys_GetObject("exitfunc");
609 610
610 if (exitfunc) { 611 if (exitfunc) {
611 object *res; 612 PyObject *res;
612 INCREF(exitfunc); 613 Py_INCREF(exitfunc);
613 sysset("exitfunc", (object *)NULL); 614 PySys_SetObject("exitfunc", (PyObject *)NULL);
614 res = call_object(exitfunc, (object *)NULL); 615 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
615 if (res == NULL) { 616 if (res == NULL) {
616 fprintf(stderr, "Error in sys.exitfunc:\n"); 617 fprintf(stderr, "Error in sys.exitfunc:\n");
617 print_error(); 618 PyErr_Print();
618 } 619 }
619 DECREF(exitfunc); 620 Py_DECREF(exitfunc);
620 } 621 }
621 622
622 flushline(); 623 Py_FlushLine();
623 624
624 while (nexitfuncs > 0) 625 while (nexitfuncs > 0)
625 (*exitfuncs[--nexitfuncs])(); 626 (*exitfuncs[--nexitfuncs])();
626 } 627 }
627 628
628 #ifdef COUNT_ALLOCS 629 #ifdef COUNT_ALLOCS
629 extern void dump_counts PROTO((void)); 630 extern void dump_counts Py_PROTO((void));
630 #endif 631 #endif
631 632
632 void 633 void
633 goaway(sts) 634 Py_Exit(sts)
634 int sts; 635 int sts;
635 { 636 {
636 cleanup(); 637 Py_Cleanup();
637 638
638 #ifdef COUNT_ALLOCS 639 #ifdef COUNT_ALLOCS
639 dump_counts(); 640 dump_counts();
640 #endif 641 #endif
641 642
643 644
644 /* Other threads may still be active, so skip most of the 645 /* Other threads may still be active, so skip most of the
645 cleanup actions usually done (these are mostly for 646 cleanup actions usually done (these are mostly for
646 debugging anyway). */ 647 debugging anyway). */
647 648
648 (void) save_thread(); 649 (void) PyEval_SaveThread();
649 #ifndef NO_EXIT_PROG 650 #ifndef NO_EXIT_PROG
650 if (threads_started) 651 if (_PyThread_Started)
651 _exit_prog(sts); 652 _exit_prog(sts);
652 else 653 else
653 exit_prog(sts); 654 exit_prog(sts);
654 #else /* !NO_EXIT_PROG */ 655 #else /* !NO_EXIT_PROG */
655 if (threads_started) 656 if (_PyThread_Started)
656 _exit(sts); 657 _exit(sts);
657 else 658 else
658 exit(sts); 659 exit(sts);
659 #endif /* !NO_EXIT_PROG */ 660 #endif /* !NO_EXIT_PROG */
660 661
661 #else /* WITH_THREAD */ 662 #else /* WITH_THREAD */
662 663
663 doneimport(); 664 PyImport_Cleanup();
664 665
665 err_clear(); 666 PyErr_Clear();
666 667
667 #ifdef Py_REF_DEBUG 668 #ifdef Py_REF_DEBUG
668 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal); 669 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
669 #endif 670 #endif
670 671
671 #ifdef Py_TRACE_REFS 672 #ifdef Py_TRACE_REFS
672 if (askyesno("Print left references?")) { 673 if (_Py_AskYesNo("Print left references?")) {
673 _Py_PrintReferences(stderr); 674 _Py_PrintReferences(stderr);
674 } 675 }
675 #endif /* Py_TRACE_REFS */ 676 #endif /* Py_TRACE_REFS */
676 677
677 #ifdef macintosh 678 #ifdef macintosh
687 static RETSIGTYPE 688 static RETSIGTYPE
688 sighandler(sig) 689 sighandler(sig)
689 int sig; 690 int sig;
690 { 691 {
691 signal(sig, SIG_DFL); /* Don't catch recursive signals */ 692 signal(sig, SIG_DFL); /* Don't catch recursive signals */
692 cleanup(); /* Do essential clean-up */ 693 Py_Cleanup(); /* Do essential clean-up */
693 #ifdef HAVE_GETPID 694 #ifdef HAVE_GETPID
694 kill(getpid(), sig); /* Pretend the signal killed us */ 695 kill(getpid(), sig); /* Pretend the signal killed us */
695 #else 696 #else
696 exit(1); 697 exit(1);
697 #endif 698 #endif
720 signal(SIGTERM, sighandler); 721 signal(SIGTERM, sighandler);
721 else 722 else
722 signal(SIGTERM, t); 723 signal(SIGTERM, t);
723 #endif 724 #endif
724 #endif /* HAVE_SIGNAL_H */ 725 #endif /* HAVE_SIGNAL_H */
725 initintr(); /* May imply initsignal() */ 726 PyOS_InitInterrupts(); /* May imply initsignal() */
726 } 727 }
727 728
728 #ifdef Py_TRACE_REFS 729 #ifdef Py_TRACE_REFS
729 /* Ask a yes/no question */ 730 /* Ask a yes/no question */
730 731
731 int 732 int
732 askyesno(prompt) 733 _Py_AskYesNo(prompt)
733 char *prompt; 734 char *prompt;
734 { 735 {
735 char buf[256]; 736 char buf[256];
736 737
737 printf("%s [ny] ", prompt); 738 printf("%s [ny] ", prompt);