1
2 /* Top level execution of Python code (including in __main__) */
3
4 /* To help control the interfaces between the startup, execution and
5 * shutdown code, the phases are split across separate modules (boostrap,
6 * pythonrun, shutdown)
7 */
8
9 /* TODO: Cull includes following phase split */
10
11 #include "Python.h"
12
13 #include "Python-ast.h"
14 #undef Yield /* undefine macro conflicting with winbase.h */
15 #include "internal/pystate.h"
16 #include "grammar.h"
17 #include "node.h"
18 #include "token.h"
19 #include "parsetok.h"
20 #include "errcode.h"
21 #include "code.h"
22 #include "symtable.h"
23 #include "ast.h"
24 #include "marshal.h"
25 #include "osdefs.h"
26 #include <locale.h>
27
28 #ifdef HAVE_SIGNAL_H
29 #include <signal.h>
30 #endif
31
32 #ifdef MS_WINDOWS
33 #include "malloc.h" /* for alloca */
34 #endif
35
36 #ifdef MS_WINDOWS
37 #undef BYTE
38 #include "windows.h"
39 #endif
40
41 _Py_IDENTIFIER(builtins);
42 _Py_IDENTIFIER(excepthook);
43 _Py_IDENTIFIER(flush);
44 _Py_IDENTIFIER(last_traceback);
45 _Py_IDENTIFIER(last_type);
46 _Py_IDENTIFIER(last_value);
47 _Py_IDENTIFIER(ps1);
48 _Py_IDENTIFIER(ps2);
49 _Py_IDENTIFIER(stdin);
50 _Py_IDENTIFIER(stdout);
51 _Py_IDENTIFIER(stderr);
52 _Py_static_string(PyId_string, "<string>");
53
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57
58 extern grammar _PyParser_Grammar; /* From graminit.c */
59
60 /* Forward */
61 static void flush_io(void);
62 static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
63 PyCompilerFlags *, PyArena *);
64 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
65 PyCompilerFlags *);
66 static void err_input(perrdetail *);
67 static void err_free(perrdetail *);
68 static int PyRun_InteractiveOneObjectEx(FILE *, PyObject *, PyCompilerFlags *);
69
70 /* Parse input from a file and execute it */
71 int
PyRun_AnyFileExFlags(FILE * fp,const char * filename,int closeit,PyCompilerFlags * flags)72 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
73 PyCompilerFlags *flags)
74 {
75 if (filename == NULL)
76 filename = "???";
77 if (Py_FdIsInteractive(fp, filename)) {
78 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
79 if (closeit)
80 fclose(fp);
81 return err;
82 }
83 else
84 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
85 }
86
87 int
PyRun_InteractiveLoopFlags(FILE * fp,const char * filename_str,PyCompilerFlags * flags)88 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
89 {
90 PyObject *filename, *v;
91 int ret, err;
92 PyCompilerFlags local_flags;
93 int nomem_count = 0;
94 #ifdef Py_REF_DEBUG
95 int show_ref_count = PyThreadState_GET()->interp->core_config.show_ref_count;
96 #endif
97
98 filename = PyUnicode_DecodeFSDefault(filename_str);
99 if (filename == NULL) {
100 PyErr_Print();
101 return -1;
102 }
103
104 if (flags == NULL) {
105 flags = &local_flags;
106 local_flags.cf_flags = 0;
107 }
108 v = _PySys_GetObjectId(&PyId_ps1);
109 if (v == NULL) {
110 _PySys_SetObjectId(&PyId_ps1, v = PyUnicode_FromString(">>> "));
111 Py_XDECREF(v);
112 }
113 v = _PySys_GetObjectId(&PyId_ps2);
114 if (v == NULL) {
115 _PySys_SetObjectId(&PyId_ps2, v = PyUnicode_FromString("... "));
116 Py_XDECREF(v);
117 }
118 err = 0;
119 do {
120 ret = PyRun_InteractiveOneObjectEx(fp, filename, flags);
121 if (ret == -1 && PyErr_Occurred()) {
122 /* Prevent an endless loop after multiple consecutive MemoryErrors
123 * while still allowing an interactive command to fail with a
124 * MemoryError. */
125 if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
126 if (++nomem_count > 16) {
127 PyErr_Clear();
128 err = -1;
129 break;
130 }
131 } else {
132 nomem_count = 0;
133 }
134 PyErr_Print();
135 flush_io();
136 } else {
137 nomem_count = 0;
138 }
139 #ifdef Py_REF_DEBUG
140 if (show_ref_count) {
141 _PyDebug_PrintTotalRefs();
142 }
143 #endif
144 } while (ret != E_EOF);
145 Py_DECREF(filename);
146 return err;
147 }
148
149 /* compute parser flags based on compiler flags */
PARSER_FLAGS(PyCompilerFlags * flags)150 static int PARSER_FLAGS(PyCompilerFlags *flags)
151 {
152 int parser_flags = 0;
153 if (!flags)
154 return 0;
155 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
156 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
157 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
158 parser_flags |= PyPARSE_IGNORE_COOKIE;
159 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
160 parser_flags |= PyPARSE_BARRY_AS_BDFL;
161 return parser_flags;
162 }
163
164 #if 0
165 /* Keep an example of flags with future keyword support. */
166 #define PARSER_FLAGS(flags) \
167 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
168 PyPARSE_DONT_IMPLY_DEDENT : 0) \
169 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
170 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
171 #endif
172
173 /* A PyRun_InteractiveOneObject() auxiliary function that does not print the
174 * error on failure. */
175 static int
PyRun_InteractiveOneObjectEx(FILE * fp,PyObject * filename,PyCompilerFlags * flags)176 PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
177 PyCompilerFlags *flags)
178 {
179 PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name;
180 mod_ty mod;
181 PyArena *arena;
182 const char *ps1 = "", *ps2 = "", *enc = NULL;
183 int errcode = 0;
184 _Py_IDENTIFIER(encoding);
185 _Py_IDENTIFIER(__main__);
186
187 mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */
188 if (mod_name == NULL) {
189 return -1;
190 }
191
192 if (fp == stdin) {
193 /* Fetch encoding from sys.stdin if possible. */
194 v = _PySys_GetObjectId(&PyId_stdin);
195 if (v && v != Py_None) {
196 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
197 if (oenc)
198 enc = PyUnicode_AsUTF8(oenc);
199 if (!enc)
200 PyErr_Clear();
201 }
202 }
203 v = _PySys_GetObjectId(&PyId_ps1);
204 if (v != NULL) {
205 v = PyObject_Str(v);
206 if (v == NULL)
207 PyErr_Clear();
208 else if (PyUnicode_Check(v)) {
209 ps1 = PyUnicode_AsUTF8(v);
210 if (ps1 == NULL) {
211 PyErr_Clear();
212 ps1 = "";
213 }
214 }
215 }
216 w = _PySys_GetObjectId(&PyId_ps2);
217 if (w != NULL) {
218 w = PyObject_Str(w);
219 if (w == NULL)
220 PyErr_Clear();
221 else if (PyUnicode_Check(w)) {
222 ps2 = PyUnicode_AsUTF8(w);
223 if (ps2 == NULL) {
224 PyErr_Clear();
225 ps2 = "";
226 }
227 }
228 }
229 arena = PyArena_New();
230 if (arena == NULL) {
231 Py_XDECREF(v);
232 Py_XDECREF(w);
233 Py_XDECREF(oenc);
234 return -1;
235 }
236 mod = PyParser_ASTFromFileObject(fp, filename, enc,
237 Py_single_input, ps1, ps2,
238 flags, &errcode, arena);
239 Py_XDECREF(v);
240 Py_XDECREF(w);
241 Py_XDECREF(oenc);
242 if (mod == NULL) {
243 PyArena_Free(arena);
244 if (errcode == E_EOF) {
245 PyErr_Clear();
246 return E_EOF;
247 }
248 return -1;
249 }
250 m = PyImport_AddModuleObject(mod_name);
251 if (m == NULL) {
252 PyArena_Free(arena);
253 return -1;
254 }
255 d = PyModule_GetDict(m);
256 v = run_mod(mod, filename, d, d, flags, arena);
257 PyArena_Free(arena);
258 if (v == NULL) {
259 return -1;
260 }
261 Py_DECREF(v);
262 flush_io();
263 return 0;
264 }
265
266 int
PyRun_InteractiveOneObject(FILE * fp,PyObject * filename,PyCompilerFlags * flags)267 PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
268 {
269 int res;
270
271 res = PyRun_InteractiveOneObjectEx(fp, filename, flags);
272 if (res == -1) {
273 PyErr_Print();
274 flush_io();
275 }
276 return res;
277 }
278
279 int
PyRun_InteractiveOneFlags(FILE * fp,const char * filename_str,PyCompilerFlags * flags)280 PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
281 {
282 PyObject *filename;
283 int res;
284
285 filename = PyUnicode_DecodeFSDefault(filename_str);
286 if (filename == NULL) {
287 PyErr_Print();
288 return -1;
289 }
290 res = PyRun_InteractiveOneObject(fp, filename, flags);
291 Py_DECREF(filename);
292 return res;
293 }
294
295
296 /* Check whether a file maybe a pyc file: Look at the extension,
297 the file type, and, if we may close it, at the first few bytes. */
298
299 static int
maybe_pyc_file(FILE * fp,const char * filename,const char * ext,int closeit)300 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
301 {
302 if (strcmp(ext, ".pyc") == 0)
303 return 1;
304
305 /* Only look into the file if we are allowed to close it, since
306 it then should also be seekable. */
307 if (closeit) {
308 /* Read only two bytes of the magic. If the file was opened in
309 text mode, the bytes 3 and 4 of the magic (\r\n) might not
310 be read as they are on disk. */
311 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
312 unsigned char buf[2];
313 /* Mess: In case of -x, the stream is NOT at its start now,
314 and ungetc() was used to push back the first newline,
315 which makes the current stream position formally undefined,
316 and a x-platform nightmare.
317 Unfortunately, we have no direct way to know whether -x
318 was specified. So we use a terrible hack: if the current
319 stream position is not 0, we assume -x was specified, and
320 give up. Bug 132850 on SourceForge spells out the
321 hopelessness of trying anything else (fseek and ftell
322 don't work predictably x-platform for text-mode files).
323 */
324 int ispyc = 0;
325 if (ftell(fp) == 0) {
326 if (fread(buf, 1, 2, fp) == 2 &&
327 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
328 ispyc = 1;
329 rewind(fp);
330 }
331 return ispyc;
332 }
333 return 0;
334 }
335
336 static int
set_main_loader(PyObject * d,const char * filename,const char * loader_name)337 set_main_loader(PyObject *d, const char *filename, const char *loader_name)
338 {
339 PyInterpreterState *interp;
340 PyThreadState *tstate;
341 PyObject *filename_obj, *bootstrap, *loader_type = NULL, *loader;
342 int result = 0;
343
344 filename_obj = PyUnicode_DecodeFSDefault(filename);
345 if (filename_obj == NULL)
346 return -1;
347 /* Get current thread state and interpreter pointer */
348 tstate = PyThreadState_GET();
349 interp = tstate->interp;
350 bootstrap = PyObject_GetAttrString(interp->importlib,
351 "_bootstrap_external");
352 if (bootstrap != NULL) {
353 loader_type = PyObject_GetAttrString(bootstrap, loader_name);
354 Py_DECREF(bootstrap);
355 }
356 if (loader_type == NULL) {
357 Py_DECREF(filename_obj);
358 return -1;
359 }
360 loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
361 Py_DECREF(loader_type);
362 if (loader == NULL) {
363 return -1;
364 }
365 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
366 result = -1;
367 }
368 Py_DECREF(loader);
369 return result;
370 }
371
372 int
PyRun_SimpleFileExFlags(FILE * fp,const char * filename,int closeit,PyCompilerFlags * flags)373 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
374 PyCompilerFlags *flags)
375 {
376 PyObject *m, *d, *v;
377 const char *ext;
378 int set_file_name = 0, ret = -1;
379 size_t len;
380
381 m = PyImport_AddModule("__main__");
382 if (m == NULL)
383 return -1;
384 Py_INCREF(m);
385 d = PyModule_GetDict(m);
386 if (PyDict_GetItemString(d, "__file__") == NULL) {
387 PyObject *f;
388 f = PyUnicode_DecodeFSDefault(filename);
389 if (f == NULL)
390 goto done;
391 if (PyDict_SetItemString(d, "__file__", f) < 0) {
392 Py_DECREF(f);
393 goto done;
394 }
395 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
396 Py_DECREF(f);
397 goto done;
398 }
399 set_file_name = 1;
400 Py_DECREF(f);
401 }
402 len = strlen(filename);
403 ext = filename + len - (len > 4 ? 4 : 0);
404 if (maybe_pyc_file(fp, filename, ext, closeit)) {
405 FILE *pyc_fp;
406 /* Try to run a pyc file. First, re-open in binary */
407 if (closeit)
408 fclose(fp);
409 if ((pyc_fp = _Py_fopen(filename, "rb")) == NULL) {
410 fprintf(stderr, "python: Can't reopen .pyc file\n");
411 goto done;
412 }
413
414 if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
415 fprintf(stderr, "python: failed to set __main__.__loader__\n");
416 ret = -1;
417 fclose(pyc_fp);
418 goto done;
419 }
420 v = run_pyc_file(pyc_fp, filename, d, d, flags);
421 } else {
422 /* When running from stdin, leave __main__.__loader__ alone */
423 if (strcmp(filename, "<stdin>") != 0 &&
424 set_main_loader(d, filename, "SourceFileLoader") < 0) {
425 fprintf(stderr, "python: failed to set __main__.__loader__\n");
426 ret = -1;
427 goto done;
428 }
429 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
430 closeit, flags);
431 }
432 flush_io();
433 if (v == NULL) {
434 Py_CLEAR(m);
435 PyErr_Print();
436 goto done;
437 }
438 Py_DECREF(v);
439 ret = 0;
440 done:
441 if (set_file_name && PyDict_DelItemString(d, "__file__"))
442 PyErr_Clear();
443 Py_XDECREF(m);
444 return ret;
445 }
446
447 int
PyRun_SimpleStringFlags(const char * command,PyCompilerFlags * flags)448 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
449 {
450 PyObject *m, *d, *v;
451 m = PyImport_AddModule("__main__");
452 if (m == NULL)
453 return -1;
454 d = PyModule_GetDict(m);
455 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
456 if (v == NULL) {
457 PyErr_Print();
458 return -1;
459 }
460 Py_DECREF(v);
461 return 0;
462 }
463
464 static int
parse_syntax_error(PyObject * err,PyObject ** message,PyObject ** filename,int * lineno,int * offset,PyObject ** text)465 parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
466 int *lineno, int *offset, PyObject **text)
467 {
468 int hold;
469 PyObject *v;
470 _Py_IDENTIFIER(msg);
471 _Py_IDENTIFIER(filename);
472 _Py_IDENTIFIER(lineno);
473 _Py_IDENTIFIER(offset);
474 _Py_IDENTIFIER(text);
475
476 *message = NULL;
477 *filename = NULL;
478
479 /* new style errors. `err' is an instance */
480 *message = _PyObject_GetAttrId(err, &PyId_msg);
481 if (!*message)
482 goto finally;
483
484 v = _PyObject_GetAttrId(err, &PyId_filename);
485 if (!v)
486 goto finally;
487 if (v == Py_None) {
488 Py_DECREF(v);
489 *filename = _PyUnicode_FromId(&PyId_string);
490 if (*filename == NULL)
491 goto finally;
492 Py_INCREF(*filename);
493 }
494 else {
495 *filename = v;
496 }
497
498 v = _PyObject_GetAttrId(err, &PyId_lineno);
499 if (!v)
500 goto finally;
501 hold = _PyLong_AsInt(v);
502 Py_DECREF(v);
503 if (hold < 0 && PyErr_Occurred())
504 goto finally;
505 *lineno = hold;
506
507 v = _PyObject_GetAttrId(err, &PyId_offset);
508 if (!v)
509 goto finally;
510 if (v == Py_None) {
511 *offset = -1;
512 Py_DECREF(v);
513 } else {
514 hold = _PyLong_AsInt(v);
515 Py_DECREF(v);
516 if (hold < 0 && PyErr_Occurred())
517 goto finally;
518 *offset = hold;
519 }
520
521 v = _PyObject_GetAttrId(err, &PyId_text);
522 if (!v)
523 goto finally;
524 if (v == Py_None) {
525 Py_DECREF(v);
526 *text = NULL;
527 }
528 else {
529 *text = v;
530 }
531 return 1;
532
533 finally:
534 Py_XDECREF(*message);
535 Py_XDECREF(*filename);
536 return 0;
537 }
538
539 void
PyErr_Print(void)540 PyErr_Print(void)
541 {
542 PyErr_PrintEx(1);
543 }
544
545 static void
print_error_text(PyObject * f,int offset,PyObject * text_obj)546 print_error_text(PyObject *f, int offset, PyObject *text_obj)
547 {
548 const char *text;
549 const char *nl;
550
551 text = PyUnicode_AsUTF8(text_obj);
552 if (text == NULL)
553 return;
554
555 if (offset >= 0) {
556 if (offset > 0 && (size_t)offset == strlen(text) && text[offset - 1] == '\n')
557 offset--;
558 for (;;) {
559 nl = strchr(text, '\n');
560 if (nl == NULL || nl-text >= offset)
561 break;
562 offset -= (int)(nl+1-text);
563 text = nl+1;
564 }
565 while (*text == ' ' || *text == '\t' || *text == '\f') {
566 text++;
567 offset--;
568 }
569 }
570 PyFile_WriteString(" ", f);
571 PyFile_WriteString(text, f);
572 if (*text == '\0' || text[strlen(text)-1] != '\n')
573 PyFile_WriteString("\n", f);
574 if (offset == -1)
575 return;
576 PyFile_WriteString(" ", f);
577 while (--offset > 0)
578 PyFile_WriteString(" ", f);
579 PyFile_WriteString("^\n", f);
580 }
581
582 static void
handle_system_exit(void)583 handle_system_exit(void)
584 {
585 PyObject *exception, *value, *tb;
586 int exitcode = 0;
587
588 if (Py_InspectFlag)
589 /* Don't exit if -i flag was given. This flag is set to 0
590 * when entering interactive mode for inspecting. */
591 return;
592
593 PyErr_Fetch(&exception, &value, &tb);
594 fflush(stdout);
595 if (value == NULL || value == Py_None)
596 goto done;
597 if (PyExceptionInstance_Check(value)) {
598 /* The error code should be in the `code' attribute. */
599 _Py_IDENTIFIER(code);
600 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
601 if (code) {
602 Py_DECREF(value);
603 value = code;
604 if (value == Py_None)
605 goto done;
606 }
607 /* If we failed to dig out the 'code' attribute,
608 just let the else clause below print the error. */
609 }
610 if (PyLong_Check(value))
611 exitcode = (int)PyLong_AsLong(value);
612 else {
613 PyObject *sys_stderr = _PySys_GetObjectId(&PyId_stderr);
614 /* We clear the exception here to avoid triggering the assertion
615 * in PyObject_Str that ensures it won't silently lose exception
616 * details.
617 */
618 PyErr_Clear();
619 if (sys_stderr != NULL && sys_stderr != Py_None) {
620 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
621 } else {
622 PyObject_Print(value, stderr, Py_PRINT_RAW);
623 fflush(stderr);
624 }
625 PySys_WriteStderr("\n");
626 exitcode = 1;
627 }
628 done:
629 /* Restore and clear the exception info, in order to properly decref
630 * the exception, value, and traceback. If we just exit instead,
631 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
632 * some finalizers from running.
633 */
634 PyErr_Restore(exception, value, tb);
635 PyErr_Clear();
636 Py_Exit(exitcode);
637 /* NOTREACHED */
638 }
639
640 void
PyErr_PrintEx(int set_sys_last_vars)641 PyErr_PrintEx(int set_sys_last_vars)
642 {
643 PyObject *exception, *v, *tb, *hook;
644
645 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
646 handle_system_exit();
647 }
648 PyErr_Fetch(&exception, &v, &tb);
649 if (exception == NULL)
650 return;
651 PyErr_NormalizeException(&exception, &v, &tb);
652 if (tb == NULL) {
653 tb = Py_None;
654 Py_INCREF(tb);
655 }
656 PyException_SetTraceback(v, tb);
657 if (exception == NULL)
658 return;
659 /* Now we know v != NULL too */
660 if (set_sys_last_vars) {
661 if (_PySys_SetObjectId(&PyId_last_type, exception) < 0) {
662 PyErr_Clear();
663 }
664 if (_PySys_SetObjectId(&PyId_last_value, v) < 0) {
665 PyErr_Clear();
666 }
667 if (_PySys_SetObjectId(&PyId_last_traceback, tb) < 0) {
668 PyErr_Clear();
669 }
670 }
671 hook = _PySys_GetObjectId(&PyId_excepthook);
672 if (hook) {
673 PyObject* stack[3];
674 PyObject *result;
675
676 stack[0] = exception;
677 stack[1] = v;
678 stack[2] = tb;
679 result = _PyObject_FastCall(hook, stack, 3);
680 if (result == NULL) {
681 PyObject *exception2, *v2, *tb2;
682 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
683 handle_system_exit();
684 }
685 PyErr_Fetch(&exception2, &v2, &tb2);
686 PyErr_NormalizeException(&exception2, &v2, &tb2);
687 /* It should not be possible for exception2 or v2
688 to be NULL. However PyErr_Display() can't
689 tolerate NULLs, so just be safe. */
690 if (exception2 == NULL) {
691 exception2 = Py_None;
692 Py_INCREF(exception2);
693 }
694 if (v2 == NULL) {
695 v2 = Py_None;
696 Py_INCREF(v2);
697 }
698 fflush(stdout);
699 PySys_WriteStderr("Error in sys.excepthook:\n");
700 PyErr_Display(exception2, v2, tb2);
701 PySys_WriteStderr("\nOriginal exception was:\n");
702 PyErr_Display(exception, v, tb);
703 Py_DECREF(exception2);
704 Py_DECREF(v2);
705 Py_XDECREF(tb2);
706 }
707 Py_XDECREF(result);
708 } else {
709 PySys_WriteStderr("sys.excepthook is missing\n");
710 PyErr_Display(exception, v, tb);
711 }
712 Py_XDECREF(exception);
713 Py_XDECREF(v);
714 Py_XDECREF(tb);
715 }
716
717 static void
print_exception(PyObject * f,PyObject * value)718 print_exception(PyObject *f, PyObject *value)
719 {
720 int err = 0;
721 PyObject *type, *tb;
722 _Py_IDENTIFIER(print_file_and_line);
723
724 if (!PyExceptionInstance_Check(value)) {
725 err = PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
726 err += PyFile_WriteString(Py_TYPE(value)->tp_name, f);
727 err += PyFile_WriteString(" found\n", f);
728 if (err)
729 PyErr_Clear();
730 return;
731 }
732
733 Py_INCREF(value);
734 fflush(stdout);
735 type = (PyObject *) Py_TYPE(value);
736 tb = PyException_GetTraceback(value);
737 if (tb && tb != Py_None)
738 err = PyTraceBack_Print(tb, f);
739 if (err == 0 &&
740 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
741 {
742 PyObject *message, *filename, *text;
743 int lineno, offset;
744 if (!parse_syntax_error(value, &message, &filename,
745 &lineno, &offset, &text))
746 PyErr_Clear();
747 else {
748 PyObject *line;
749
750 Py_DECREF(value);
751 value = message;
752
753 line = PyUnicode_FromFormat(" File \"%U\", line %d\n",
754 filename, lineno);
755 Py_DECREF(filename);
756 if (line != NULL) {
757 PyFile_WriteObject(line, f, Py_PRINT_RAW);
758 Py_DECREF(line);
759 }
760
761 if (text != NULL) {
762 print_error_text(f, offset, text);
763 Py_DECREF(text);
764 }
765
766 /* Can't be bothered to check all those
767 PyFile_WriteString() calls */
768 if (PyErr_Occurred())
769 err = -1;
770 }
771 }
772 if (err) {
773 /* Don't do anything else */
774 }
775 else {
776 PyObject* moduleName;
777 char* className;
778 _Py_IDENTIFIER(__module__);
779 assert(PyExceptionClass_Check(type));
780 className = PyExceptionClass_Name(type);
781 if (className != NULL) {
782 char *dot = strrchr(className, '.');
783 if (dot != NULL)
784 className = dot+1;
785 }
786
787 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
788 if (moduleName == NULL || !PyUnicode_Check(moduleName))
789 {
790 Py_XDECREF(moduleName);
791 err = PyFile_WriteString("<unknown>", f);
792 }
793 else {
794 if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins))
795 {
796 err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW);
797 err += PyFile_WriteString(".", f);
798 }
799 Py_DECREF(moduleName);
800 }
801 if (err == 0) {
802 if (className == NULL)
803 err = PyFile_WriteString("<unknown>", f);
804 else
805 err = PyFile_WriteString(className, f);
806 }
807 }
808 if (err == 0 && (value != Py_None)) {
809 PyObject *s = PyObject_Str(value);
810 /* only print colon if the str() of the
811 object is not the empty string
812 */
813 if (s == NULL) {
814 PyErr_Clear();
815 err = -1;
816 PyFile_WriteString(": <exception str() failed>", f);
817 }
818 else if (!PyUnicode_Check(s) ||
819 PyUnicode_GetLength(s) != 0)
820 err = PyFile_WriteString(": ", f);
821 if (err == 0)
822 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
823 Py_XDECREF(s);
824 }
825 /* try to write a newline in any case */
826 if (err < 0) {
827 PyErr_Clear();
828 }
829 err += PyFile_WriteString("\n", f);
830 Py_XDECREF(tb);
831 Py_DECREF(value);
832 /* If an error happened here, don't show it.
833 XXX This is wrong, but too many callers rely on this behavior. */
834 if (err != 0)
835 PyErr_Clear();
836 }
837
838 static const char cause_message[] =
839 "\nThe above exception was the direct cause "
840 "of the following exception:\n\n";
841
842 static const char context_message[] =
843 "\nDuring handling of the above exception, "
844 "another exception occurred:\n\n";
845
846 static void
print_exception_recursive(PyObject * f,PyObject * value,PyObject * seen)847 print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
848 {
849 int err = 0, res;
850 PyObject *cause, *context;
851
852 if (seen != NULL) {
853 /* Exception chaining */
854 PyObject *value_id = PyLong_FromVoidPtr(value);
855 if (value_id == NULL || PySet_Add(seen, value_id) == -1)
856 PyErr_Clear();
857 else if (PyExceptionInstance_Check(value)) {
858 PyObject *check_id = NULL;
859 cause = PyException_GetCause(value);
860 context = PyException_GetContext(value);
861 if (cause) {
862 check_id = PyLong_FromVoidPtr(cause);
863 if (check_id == NULL) {
864 res = -1;
865 } else {
866 res = PySet_Contains(seen, check_id);
867 Py_DECREF(check_id);
868 }
869 if (res == -1)
870 PyErr_Clear();
871 if (res == 0) {
872 print_exception_recursive(
873 f, cause, seen);
874 err |= PyFile_WriteString(
875 cause_message, f);
876 }
877 }
878 else if (context &&
879 !((PyBaseExceptionObject *)value)->suppress_context) {
880 check_id = PyLong_FromVoidPtr(context);
881 if (check_id == NULL) {
882 res = -1;
883 } else {
884 res = PySet_Contains(seen, check_id);
885 Py_DECREF(check_id);
886 }
887 if (res == -1)
888 PyErr_Clear();
889 if (res == 0) {
890 print_exception_recursive(
891 f, context, seen);
892 err |= PyFile_WriteString(
893 context_message, f);
894 }
895 }
896 Py_XDECREF(context);
897 Py_XDECREF(cause);
898 }
899 Py_XDECREF(value_id);
900 }
901 print_exception(f, value);
902 if (err != 0)
903 PyErr_Clear();
904 }
905
906 void
PyErr_Display(PyObject * exception,PyObject * value,PyObject * tb)907 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
908 {
909 PyObject *seen;
910 PyObject *f = _PySys_GetObjectId(&PyId_stderr);
911 if (PyExceptionInstance_Check(value)
912 && tb != NULL && PyTraceBack_Check(tb)) {
913 /* Put the traceback on the exception, otherwise it won't get
914 displayed. See issue #18776. */
915 PyObject *cur_tb = PyException_GetTraceback(value);
916 if (cur_tb == NULL)
917 PyException_SetTraceback(value, tb);
918 else
919 Py_DECREF(cur_tb);
920 }
921 if (f == Py_None) {
922 /* pass */
923 }
924 else if (f == NULL) {
925 _PyObject_Dump(value);
926 fprintf(stderr, "lost sys.stderr\n");
927 }
928 else {
929 /* We choose to ignore seen being possibly NULL, and report
930 at least the main exception (it could be a MemoryError).
931 */
932 seen = PySet_New(NULL);
933 if (seen == NULL)
934 PyErr_Clear();
935 print_exception_recursive(f, value, seen);
936 Py_XDECREF(seen);
937 }
938 }
939
940 PyObject *
PyRun_StringFlags(const char * str,int start,PyObject * globals,PyObject * locals,PyCompilerFlags * flags)941 PyRun_StringFlags(const char *str, int start, PyObject *globals,
942 PyObject *locals, PyCompilerFlags *flags)
943 {
944 PyObject *ret = NULL;
945 mod_ty mod;
946 PyArena *arena;
947 PyObject *filename;
948
949 filename = _PyUnicode_FromId(&PyId_string); /* borrowed */
950 if (filename == NULL)
951 return NULL;
952
953 arena = PyArena_New();
954 if (arena == NULL)
955 return NULL;
956
957 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
958 if (mod != NULL)
959 ret = run_mod(mod, filename, globals, locals, flags, arena);
960 PyArena_Free(arena);
961 return ret;
962 }
963
964 PyObject *
PyRun_FileExFlags(FILE * fp,const char * filename_str,int start,PyObject * globals,PyObject * locals,int closeit,PyCompilerFlags * flags)965 PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
966 PyObject *locals, int closeit, PyCompilerFlags *flags)
967 {
968 PyObject *ret = NULL;
969 mod_ty mod;
970 PyArena *arena = NULL;
971 PyObject *filename;
972
973 filename = PyUnicode_DecodeFSDefault(filename_str);
974 if (filename == NULL)
975 goto exit;
976
977 arena = PyArena_New();
978 if (arena == NULL)
979 goto exit;
980
981 mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
982 flags, NULL, arena);
983 if (closeit)
984 fclose(fp);
985 if (mod == NULL) {
986 goto exit;
987 }
988 ret = run_mod(mod, filename, globals, locals, flags, arena);
989
990 exit:
991 Py_XDECREF(filename);
992 if (arena != NULL)
993 PyArena_Free(arena);
994 return ret;
995 }
996
997 static void
flush_io(void)998 flush_io(void)
999 {
1000 PyObject *f, *r;
1001 PyObject *type, *value, *traceback;
1002
1003 /* Save the current exception */
1004 PyErr_Fetch(&type, &value, &traceback);
1005
1006 f = _PySys_GetObjectId(&PyId_stderr);
1007 if (f != NULL) {
1008 r = _PyObject_CallMethodId(f, &PyId_flush, NULL);
1009 if (r)
1010 Py_DECREF(r);
1011 else
1012 PyErr_Clear();
1013 }
1014 f = _PySys_GetObjectId(&PyId_stdout);
1015 if (f != NULL) {
1016 r = _PyObject_CallMethodId(f, &PyId_flush, NULL);
1017 if (r)
1018 Py_DECREF(r);
1019 else
1020 PyErr_Clear();
1021 }
1022
1023 PyErr_Restore(type, value, traceback);
1024 }
1025
1026 static PyObject *
run_mod(mod_ty mod,PyObject * filename,PyObject * globals,PyObject * locals,PyCompilerFlags * flags,PyArena * arena)1027 run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
1028 PyCompilerFlags *flags, PyArena *arena)
1029 {
1030 PyCodeObject *co;
1031 PyObject *v;
1032 co = PyAST_CompileObject(mod, filename, flags, -1, arena);
1033 if (co == NULL)
1034 return NULL;
1035 v = PyEval_EvalCode((PyObject*)co, globals, locals);
1036 Py_DECREF(co);
1037 return v;
1038 }
1039
1040 static PyObject *
run_pyc_file(FILE * fp,const char * filename,PyObject * globals,PyObject * locals,PyCompilerFlags * flags)1041 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1042 PyObject *locals, PyCompilerFlags *flags)
1043 {
1044 PyCodeObject *co;
1045 PyObject *v;
1046 long magic;
1047 long PyImport_GetMagicNumber(void);
1048
1049 magic = PyMarshal_ReadLongFromFile(fp);
1050 if (magic != PyImport_GetMagicNumber()) {
1051 if (!PyErr_Occurred())
1052 PyErr_SetString(PyExc_RuntimeError,
1053 "Bad magic number in .pyc file");
1054 goto error;
1055 }
1056 /* Skip the rest of the header. */
1057 (void) PyMarshal_ReadLongFromFile(fp);
1058 (void) PyMarshal_ReadLongFromFile(fp);
1059 (void) PyMarshal_ReadLongFromFile(fp);
1060 if (PyErr_Occurred()) {
1061 goto error;
1062 }
1063 v = PyMarshal_ReadLastObjectFromFile(fp);
1064 if (v == NULL || !PyCode_Check(v)) {
1065 Py_XDECREF(v);
1066 PyErr_SetString(PyExc_RuntimeError,
1067 "Bad code object in .pyc file");
1068 goto error;
1069 }
1070 fclose(fp);
1071 co = (PyCodeObject *)v;
1072 v = PyEval_EvalCode((PyObject*)co, globals, locals);
1073 if (v && flags)
1074 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1075 Py_DECREF(co);
1076 return v;
1077 error:
1078 fclose(fp);
1079 return NULL;
1080 }
1081
1082 PyObject *
Py_CompileStringObject(const char * str,PyObject * filename,int start,PyCompilerFlags * flags,int optimize)1083 Py_CompileStringObject(const char *str, PyObject *filename, int start,
1084 PyCompilerFlags *flags, int optimize)
1085 {
1086 PyCodeObject *co;
1087 mod_ty mod;
1088 PyArena *arena = PyArena_New();
1089 if (arena == NULL)
1090 return NULL;
1091
1092 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
1093 if (mod == NULL) {
1094 PyArena_Free(arena);
1095 return NULL;
1096 }
1097 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1098 PyObject *result = PyAST_mod2obj(mod);
1099 PyArena_Free(arena);
1100 return result;
1101 }
1102 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
1103 PyArena_Free(arena);
1104 return (PyObject *)co;
1105 }
1106
1107 PyObject *
Py_CompileStringExFlags(const char * str,const char * filename_str,int start,PyCompilerFlags * flags,int optimize)1108 Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
1109 PyCompilerFlags *flags, int optimize)
1110 {
1111 PyObject *filename, *co;
1112 filename = PyUnicode_DecodeFSDefault(filename_str);
1113 if (filename == NULL)
1114 return NULL;
1115 co = Py_CompileStringObject(str, filename, start, flags, optimize);
1116 Py_DECREF(filename);
1117 return co;
1118 }
1119
1120 /* For use in Py_LIMITED_API */
1121 #undef Py_CompileString
1122 PyObject *
PyCompileString(const char * str,const char * filename,int start)1123 PyCompileString(const char *str, const char *filename, int start)
1124 {
1125 return Py_CompileStringFlags(str, filename, start, NULL);
1126 }
1127
1128 struct symtable *
Py_SymtableStringObject(const char * str,PyObject * filename,int start)1129 Py_SymtableStringObject(const char *str, PyObject *filename, int start)
1130 {
1131 struct symtable *st;
1132 mod_ty mod;
1133 PyCompilerFlags flags;
1134 PyArena *arena;
1135
1136 arena = PyArena_New();
1137 if (arena == NULL)
1138 return NULL;
1139
1140 flags.cf_flags = 0;
1141 mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena);
1142 if (mod == NULL) {
1143 PyArena_Free(arena);
1144 return NULL;
1145 }
1146 st = PySymtable_BuildObject(mod, filename, 0);
1147 PyArena_Free(arena);
1148 return st;
1149 }
1150
1151 struct symtable *
Py_SymtableString(const char * str,const char * filename_str,int start)1152 Py_SymtableString(const char *str, const char *filename_str, int start)
1153 {
1154 PyObject *filename;
1155 struct symtable *st;
1156
1157 filename = PyUnicode_DecodeFSDefault(filename_str);
1158 if (filename == NULL)
1159 return NULL;
1160 st = Py_SymtableStringObject(str, filename, start);
1161 Py_DECREF(filename);
1162 return st;
1163 }
1164
1165 /* Preferred access to parser is through AST. */
1166 mod_ty
PyParser_ASTFromStringObject(const char * s,PyObject * filename,int start,PyCompilerFlags * flags,PyArena * arena)1167 PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
1168 PyCompilerFlags *flags, PyArena *arena)
1169 {
1170 mod_ty mod;
1171 PyCompilerFlags localflags;
1172 perrdetail err;
1173 int iflags = PARSER_FLAGS(flags);
1174
1175 node *n = PyParser_ParseStringObject(s, filename,
1176 &_PyParser_Grammar, start, &err,
1177 &iflags);
1178 if (flags == NULL) {
1179 localflags.cf_flags = 0;
1180 flags = &localflags;
1181 }
1182 if (n) {
1183 flags->cf_flags |= iflags & PyCF_MASK;
1184 mod = PyAST_FromNodeObject(n, flags, filename, arena);
1185 PyNode_Free(n);
1186 }
1187 else {
1188 err_input(&err);
1189 mod = NULL;
1190 }
1191 err_free(&err);
1192 return mod;
1193 }
1194
1195 mod_ty
PyParser_ASTFromString(const char * s,const char * filename_str,int start,PyCompilerFlags * flags,PyArena * arena)1196 PyParser_ASTFromString(const char *s, const char *filename_str, int start,
1197 PyCompilerFlags *flags, PyArena *arena)
1198 {
1199 PyObject *filename;
1200 mod_ty mod;
1201 filename = PyUnicode_DecodeFSDefault(filename_str);
1202 if (filename == NULL)
1203 return NULL;
1204 mod = PyParser_ASTFromStringObject(s, filename, start, flags, arena);
1205 Py_DECREF(filename);
1206 return mod;
1207 }
1208
1209 mod_ty
PyParser_ASTFromFileObject(FILE * fp,PyObject * filename,const char * enc,int start,const char * ps1,const char * ps2,PyCompilerFlags * flags,int * errcode,PyArena * arena)1210 PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc,
1211 int start, const char *ps1,
1212 const char *ps2, PyCompilerFlags *flags, int *errcode,
1213 PyArena *arena)
1214 {
1215 mod_ty mod;
1216 PyCompilerFlags localflags;
1217 perrdetail err;
1218 int iflags = PARSER_FLAGS(flags);
1219
1220 node *n = PyParser_ParseFileObject(fp, filename, enc,
1221 &_PyParser_Grammar,
1222 start, ps1, ps2, &err, &iflags);
1223 if (flags == NULL) {
1224 localflags.cf_flags = 0;
1225 flags = &localflags;
1226 }
1227 if (n) {
1228 flags->cf_flags |= iflags & PyCF_MASK;
1229 mod = PyAST_FromNodeObject(n, flags, filename, arena);
1230 PyNode_Free(n);
1231 }
1232 else {
1233 err_input(&err);
1234 if (errcode)
1235 *errcode = err.error;
1236 mod = NULL;
1237 }
1238 err_free(&err);
1239 return mod;
1240 }
1241
1242 mod_ty
PyParser_ASTFromFile(FILE * fp,const char * filename_str,const char * enc,int start,const char * ps1,const char * ps2,PyCompilerFlags * flags,int * errcode,PyArena * arena)1243 PyParser_ASTFromFile(FILE *fp, const char *filename_str, const char* enc,
1244 int start, const char *ps1,
1245 const char *ps2, PyCompilerFlags *flags, int *errcode,
1246 PyArena *arena)
1247 {
1248 mod_ty mod;
1249 PyObject *filename;
1250 filename = PyUnicode_DecodeFSDefault(filename_str);
1251 if (filename == NULL)
1252 return NULL;
1253 mod = PyParser_ASTFromFileObject(fp, filename, enc, start, ps1, ps2,
1254 flags, errcode, arena);
1255 Py_DECREF(filename);
1256 return mod;
1257 }
1258
1259 /* Simplified interface to parsefile -- return node or set exception */
1260
1261 node *
PyParser_SimpleParseFileFlags(FILE * fp,const char * filename,int start,int flags)1262 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1263 {
1264 perrdetail err;
1265 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1266 &_PyParser_Grammar,
1267 start, NULL, NULL, &err, flags);
1268 if (n == NULL)
1269 err_input(&err);
1270 err_free(&err);
1271
1272 return n;
1273 }
1274
1275 /* Simplified interface to parsestring -- return node or set exception */
1276
1277 node *
PyParser_SimpleParseStringFlags(const char * str,int start,int flags)1278 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1279 {
1280 perrdetail err;
1281 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1282 start, &err, flags);
1283 if (n == NULL)
1284 err_input(&err);
1285 err_free(&err);
1286 return n;
1287 }
1288
1289 node *
PyParser_SimpleParseStringFlagsFilename(const char * str,const char * filename,int start,int flags)1290 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1291 int start, int flags)
1292 {
1293 perrdetail err;
1294 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1295 &_PyParser_Grammar, start, &err, flags);
1296 if (n == NULL)
1297 err_input(&err);
1298 err_free(&err);
1299 return n;
1300 }
1301
1302 node *
PyParser_SimpleParseStringFilename(const char * str,const char * filename,int start)1303 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1304 {
1305 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1306 }
1307
1308 /* May want to move a more generalized form of this to parsetok.c or
1309 even parser modules. */
1310
1311 void
PyParser_ClearError(perrdetail * err)1312 PyParser_ClearError(perrdetail *err)
1313 {
1314 err_free(err);
1315 }
1316
1317 void
PyParser_SetError(perrdetail * err)1318 PyParser_SetError(perrdetail *err)
1319 {
1320 err_input(err);
1321 }
1322
1323 static void
err_free(perrdetail * err)1324 err_free(perrdetail *err)
1325 {
1326 Py_CLEAR(err->filename);
1327 }
1328
1329 /* Set the error appropriate to the given input error code (see errcode.h) */
1330
1331 static void
err_input(perrdetail * err)1332 err_input(perrdetail *err)
1333 {
1334 PyObject *v, *w, *errtype, *errtext;
1335 PyObject *msg_obj = NULL;
1336 const char *msg = NULL;
1337 int offset = err->offset;
1338
1339 errtype = PyExc_SyntaxError;
1340 switch (err->error) {
1341 case E_ERROR:
1342 goto cleanup;
1343 case E_SYNTAX:
1344 errtype = PyExc_IndentationError;
1345 if (err->expected == INDENT)
1346 msg = "expected an indented block";
1347 else if (err->token == INDENT)
1348 msg = "unexpected indent";
1349 else if (err->token == DEDENT)
1350 msg = "unexpected unindent";
1351 else if (err->expected == NOTEQUAL) {
1352 errtype = PyExc_SyntaxError;
1353 msg = "with Barry as BDFL, use '<>' instead of '!='";
1354 }
1355 else {
1356 errtype = PyExc_SyntaxError;
1357 msg = "invalid syntax";
1358 }
1359 break;
1360 case E_TOKEN:
1361 msg = "invalid token";
1362 break;
1363 case E_EOFS:
1364 msg = "EOF while scanning triple-quoted string literal";
1365 break;
1366 case E_EOLS:
1367 msg = "EOL while scanning string literal";
1368 break;
1369 case E_INTR:
1370 if (!PyErr_Occurred())
1371 PyErr_SetNone(PyExc_KeyboardInterrupt);
1372 goto cleanup;
1373 case E_NOMEM:
1374 PyErr_NoMemory();
1375 goto cleanup;
1376 case E_EOF:
1377 msg = "unexpected EOF while parsing";
1378 break;
1379 case E_TABSPACE:
1380 errtype = PyExc_TabError;
1381 msg = "inconsistent use of tabs and spaces in indentation";
1382 break;
1383 case E_OVERFLOW:
1384 msg = "expression too long";
1385 break;
1386 case E_DEDENT:
1387 errtype = PyExc_IndentationError;
1388 msg = "unindent does not match any outer indentation level";
1389 break;
1390 case E_TOODEEP:
1391 errtype = PyExc_IndentationError;
1392 msg = "too many levels of indentation";
1393 break;
1394 case E_DECODE: {
1395 PyObject *type, *value, *tb;
1396 PyErr_Fetch(&type, &value, &tb);
1397 msg = "unknown decode error";
1398 if (value != NULL)
1399 msg_obj = PyObject_Str(value);
1400 Py_XDECREF(type);
1401 Py_XDECREF(value);
1402 Py_XDECREF(tb);
1403 break;
1404 }
1405 case E_LINECONT:
1406 msg = "unexpected character after line continuation character";
1407 break;
1408
1409 case E_IDENTIFIER:
1410 msg = "invalid character in identifier";
1411 break;
1412 case E_BADSINGLE:
1413 msg = "multiple statements found while compiling a single statement";
1414 break;
1415 default:
1416 fprintf(stderr, "error=%d\n", err->error);
1417 msg = "unknown parsing error";
1418 break;
1419 }
1420 /* err->text may not be UTF-8 in case of decoding errors.
1421 Explicitly convert to an object. */
1422 if (!err->text) {
1423 errtext = Py_None;
1424 Py_INCREF(Py_None);
1425 } else {
1426 errtext = PyUnicode_DecodeUTF8(err->text, err->offset,
1427 "replace");
1428 if (errtext != NULL) {
1429 Py_ssize_t len = strlen(err->text);
1430 offset = (int)PyUnicode_GET_LENGTH(errtext);
1431 if (len != err->offset) {
1432 Py_DECREF(errtext);
1433 errtext = PyUnicode_DecodeUTF8(err->text, len,
1434 "replace");
1435 }
1436 }
1437 }
1438 v = Py_BuildValue("(OiiN)", err->filename,
1439 err->lineno, offset, errtext);
1440 if (v != NULL) {
1441 if (msg_obj)
1442 w = Py_BuildValue("(OO)", msg_obj, v);
1443 else
1444 w = Py_BuildValue("(sO)", msg, v);
1445 } else
1446 w = NULL;
1447 Py_XDECREF(v);
1448 PyErr_SetObject(errtype, w);
1449 Py_XDECREF(w);
1450 cleanup:
1451 Py_XDECREF(msg_obj);
1452 if (err->text != NULL) {
1453 PyObject_FREE(err->text);
1454 err->text = NULL;
1455 }
1456 }
1457
1458
1459 #if defined(USE_STACKCHECK)
1460 #if defined(WIN32) && defined(_MSC_VER)
1461
1462 /* Stack checking for Microsoft C */
1463
1464 #include <malloc.h>
1465 #include <excpt.h>
1466
1467 /*
1468 * Return non-zero when we run out of memory on the stack; zero otherwise.
1469 */
1470 int
PyOS_CheckStack(void)1471 PyOS_CheckStack(void)
1472 {
1473 __try {
1474 /* alloca throws a stack overflow exception if there's
1475 not enough space left on the stack */
1476 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1477 return 0;
1478 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1479 EXCEPTION_EXECUTE_HANDLER :
1480 EXCEPTION_CONTINUE_SEARCH) {
1481 int errcode = _resetstkoflw();
1482 if (errcode == 0)
1483 {
1484 Py_FatalError("Could not reset the stack!");
1485 }
1486 }
1487 return 1;
1488 }
1489
1490 #endif /* WIN32 && _MSC_VER */
1491
1492 /* Alternate implementations can be added here... */
1493
1494 #endif /* USE_STACKCHECK */
1495
1496 /* Deprecated C API functions still provided for binary compatibility */
1497
1498 #undef PyParser_SimpleParseFile
1499 PyAPI_FUNC(node *)
PyParser_SimpleParseFile(FILE * fp,const char * filename,int start)1500 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1501 {
1502 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1503 }
1504
1505 #undef PyParser_SimpleParseString
1506 PyAPI_FUNC(node *)
PyParser_SimpleParseString(const char * str,int start)1507 PyParser_SimpleParseString(const char *str, int start)
1508 {
1509 return PyParser_SimpleParseStringFlags(str, start, 0);
1510 }
1511
1512 #undef PyRun_AnyFile
1513 PyAPI_FUNC(int)
PyRun_AnyFile(FILE * fp,const char * name)1514 PyRun_AnyFile(FILE *fp, const char *name)
1515 {
1516 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1517 }
1518
1519 #undef PyRun_AnyFileEx
1520 PyAPI_FUNC(int)
PyRun_AnyFileEx(FILE * fp,const char * name,int closeit)1521 PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1522 {
1523 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1524 }
1525
1526 #undef PyRun_AnyFileFlags
1527 PyAPI_FUNC(int)
PyRun_AnyFileFlags(FILE * fp,const char * name,PyCompilerFlags * flags)1528 PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1529 {
1530 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1531 }
1532
1533 #undef PyRun_File
1534 PyAPI_FUNC(PyObject *)
PyRun_File(FILE * fp,const char * p,int s,PyObject * g,PyObject * l)1535 PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1536 {
1537 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1538 }
1539
1540 #undef PyRun_FileEx
1541 PyAPI_FUNC(PyObject *)
PyRun_FileEx(FILE * fp,const char * p,int s,PyObject * g,PyObject * l,int c)1542 PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1543 {
1544 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1545 }
1546
1547 #undef PyRun_FileFlags
1548 PyAPI_FUNC(PyObject *)
PyRun_FileFlags(FILE * fp,const char * p,int s,PyObject * g,PyObject * l,PyCompilerFlags * flags)1549 PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1550 PyCompilerFlags *flags)
1551 {
1552 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1553 }
1554
1555 #undef PyRun_SimpleFile
1556 PyAPI_FUNC(int)
PyRun_SimpleFile(FILE * f,const char * p)1557 PyRun_SimpleFile(FILE *f, const char *p)
1558 {
1559 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1560 }
1561
1562 #undef PyRun_SimpleFileEx
1563 PyAPI_FUNC(int)
PyRun_SimpleFileEx(FILE * f,const char * p,int c)1564 PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1565 {
1566 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1567 }
1568
1569
1570 #undef PyRun_String
1571 PyAPI_FUNC(PyObject *)
PyRun_String(const char * str,int s,PyObject * g,PyObject * l)1572 PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1573 {
1574 return PyRun_StringFlags(str, s, g, l, NULL);
1575 }
1576
1577 #undef PyRun_SimpleString
1578 PyAPI_FUNC(int)
PyRun_SimpleString(const char * s)1579 PyRun_SimpleString(const char *s)
1580 {
1581 return PyRun_SimpleStringFlags(s, NULL);
1582 }
1583
1584 #undef Py_CompileString
1585 PyAPI_FUNC(PyObject *)
Py_CompileString(const char * str,const char * p,int s)1586 Py_CompileString(const char *str, const char *p, int s)
1587 {
1588 return Py_CompileStringExFlags(str, p, s, NULL, -1);
1589 }
1590
1591 #undef Py_CompileStringFlags
1592 PyAPI_FUNC(PyObject *)
Py_CompileStringFlags(const char * str,const char * p,int s,PyCompilerFlags * flags)1593 Py_CompileStringFlags(const char *str, const char *p, int s,
1594 PyCompilerFlags *flags)
1595 {
1596 return Py_CompileStringExFlags(str, p, s, flags, -1);
1597 }
1598
1599 #undef PyRun_InteractiveOne
1600 PyAPI_FUNC(int)
PyRun_InteractiveOne(FILE * f,const char * p)1601 PyRun_InteractiveOne(FILE *f, const char *p)
1602 {
1603 return PyRun_InteractiveOneFlags(f, p, NULL);
1604 }
1605
1606 #undef PyRun_InteractiveLoop
1607 PyAPI_FUNC(int)
PyRun_InteractiveLoop(FILE * f,const char * p)1608 PyRun_InteractiveLoop(FILE *f, const char *p)
1609 {
1610 return PyRun_InteractiveLoopFlags(f, p, NULL);
1611 }
1612
1613 #ifdef __cplusplus
1614 }
1615 #endif
1616