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