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