• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (bootstrap,
6  * pythonrun, shutdown)
7  */
8 
9 /* TODO: Cull includes following phase split */
10 
11 #include <stdbool.h>
12 
13 #include "Python.h"
14 
15 #include "pycore_ast.h"           // PyAST_mod2obj()
16 #include "pycore_ceval.h"         // _Py_EnterRecursiveCall()
17 #include "pycore_compile.h"       // _PyAST_Compile()
18 #include "pycore_interp.h"        // PyInterpreterState.importlib
19 #include "pycore_object.h"        // _PyDebug_PrintTotalRefs()
20 #include "pycore_parser.h"        // _PyParser_ASTFromString()
21 #include "pycore_pyerrors.h"      // _PyErr_GetRaisedException()
22 #include "pycore_pylifecycle.h"   // _Py_FdIsInteractive()
23 #include "pycore_pystate.h"       // _PyInterpreterState_GET()
24 #include "pycore_pythonrun.h"     // export _PyRun_InteractiveLoopObject()
25 #include "pycore_sysmodule.h"     // _PySys_Audit()
26 #include "pycore_traceback.h"     // _PyTraceBack_Print()
27 
28 #include "errcode.h"              // E_EOF
29 #include "marshal.h"              // PyMarshal_ReadLongFromFile()
30 
31 #ifdef MS_WINDOWS
32 #  include "malloc.h"             // alloca()
33 #endif
34 
35 #ifdef MS_WINDOWS
36 #  undef BYTE
37 #  include "windows.h"
38 #endif
39 
40 /* Forward */
41 static void flush_io(void);
42 static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
43                           PyCompilerFlags *, PyArena *, PyObject*, int);
44 static PyObject *run_pyc_file(FILE *, PyObject *, PyObject *,
45                               PyCompilerFlags *);
46 static int PyRun_InteractiveOneObjectEx(FILE *, PyObject *, PyCompilerFlags *);
47 static PyObject* pyrun_file(FILE *fp, PyObject *filename, int start,
48                             PyObject *globals, PyObject *locals, int closeit,
49                             PyCompilerFlags *flags);
50 static PyObject *
51 _PyRun_StringFlagsWithName(const char *str, PyObject* name, int start,
52                            PyObject *globals, PyObject *locals, PyCompilerFlags *flags,
53                            int generate_new_source);
54 
55 int
_PyRun_AnyFileObject(FILE * fp,PyObject * filename,int closeit,PyCompilerFlags * flags)56 _PyRun_AnyFileObject(FILE *fp, PyObject *filename, int closeit,
57                      PyCompilerFlags *flags)
58 {
59     int decref_filename = 0;
60     if (filename == NULL) {
61         filename = PyUnicode_FromString("???");
62         if (filename == NULL) {
63             PyErr_Print();
64             return -1;
65         }
66         decref_filename = 1;
67     }
68 
69     int res;
70     if (_Py_FdIsInteractive(fp, filename)) {
71         res = _PyRun_InteractiveLoopObject(fp, filename, flags);
72         if (closeit) {
73             fclose(fp);
74         }
75     }
76     else {
77         res = _PyRun_SimpleFileObject(fp, filename, closeit, flags);
78     }
79 
80     if (decref_filename) {
81         Py_DECREF(filename);
82     }
83     return res;
84 }
85 
86 int
PyRun_AnyFileExFlags(FILE * fp,const char * filename,int closeit,PyCompilerFlags * flags)87 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
88                      PyCompilerFlags *flags)
89 {
90     PyObject *filename_obj = NULL;
91     if (filename != NULL) {
92         filename_obj = PyUnicode_DecodeFSDefault(filename);
93         if (filename_obj == NULL) {
94             PyErr_Print();
95             return -1;
96         }
97     }
98     int res = _PyRun_AnyFileObject(fp, filename_obj, closeit, flags);
99     Py_XDECREF(filename_obj);
100     return res;
101 }
102 
103 
104 int
_PyRun_InteractiveLoopObject(FILE * fp,PyObject * filename,PyCompilerFlags * flags)105 _PyRun_InteractiveLoopObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
106 {
107     PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
108     if (flags == NULL) {
109         flags = &local_flags;
110     }
111 
112     PyThreadState *tstate = _PyThreadState_GET();
113     PyObject *v = _PySys_GetAttr(tstate, &_Py_ID(ps1));
114     if (v == NULL) {
115         _PySys_SetAttr(&_Py_ID(ps1), v = PyUnicode_FromString(">>> "));
116         Py_XDECREF(v);
117     }
118     v = _PySys_GetAttr(tstate, &_Py_ID(ps2));
119     if (v == NULL) {
120         _PySys_SetAttr(&_Py_ID(ps2), v = PyUnicode_FromString("... "));
121         Py_XDECREF(v);
122     }
123 
124 #ifdef Py_REF_DEBUG
125     int show_ref_count = _Py_GetConfig()->show_ref_count;
126 #endif
127     int err = 0;
128     int ret;
129     int nomem_count = 0;
130     do {
131         ret = PyRun_InteractiveOneObjectEx(fp, filename, flags);
132         if (ret == -1 && PyErr_Occurred()) {
133             /* Prevent an endless loop after multiple consecutive MemoryErrors
134              * while still allowing an interactive command to fail with a
135              * MemoryError. */
136             if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
137                 if (++nomem_count > 16) {
138                     PyErr_Clear();
139                     err = -1;
140                     break;
141                 }
142             } else {
143                 nomem_count = 0;
144             }
145             PyErr_Print();
146             flush_io();
147         } else {
148             nomem_count = 0;
149         }
150 #ifdef Py_REF_DEBUG
151         if (show_ref_count) {
152             _PyDebug_PrintTotalRefs();
153         }
154 #endif
155     } while (ret != E_EOF);
156     return err;
157 }
158 
159 
160 int
PyRun_InteractiveLoopFlags(FILE * fp,const char * filename,PyCompilerFlags * flags)161 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
162 {
163     PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
164     if (filename_obj == NULL) {
165         PyErr_Print();
166         return -1;
167     }
168 
169     int err = _PyRun_InteractiveLoopObject(fp, filename_obj, flags);
170     Py_DECREF(filename_obj);
171     return err;
172 
173 }
174 
175 
176 // Call _PyParser_ASTFromFile() with sys.stdin.encoding, sys.ps1 and sys.ps2
177 static int
pyrun_one_parse_ast(FILE * fp,PyObject * filename,PyCompilerFlags * flags,PyArena * arena,mod_ty * pmod,PyObject ** interactive_src)178 pyrun_one_parse_ast(FILE *fp, PyObject *filename,
179                     PyCompilerFlags *flags, PyArena *arena,
180                     mod_ty *pmod, PyObject** interactive_src)
181 {
182     PyThreadState *tstate = _PyThreadState_GET();
183 
184     // Get sys.stdin.encoding (as UTF-8)
185     PyObject *attr;  // borrowed ref
186     PyObject *encoding_obj = NULL;
187     const char *encoding = NULL;
188     if (fp == stdin) {
189         attr = _PySys_GetAttr(tstate, &_Py_ID(stdin));
190         if (attr && attr != Py_None) {
191             encoding_obj = PyObject_GetAttr(attr, &_Py_ID(encoding));
192             if (encoding_obj) {
193                 encoding = PyUnicode_AsUTF8(encoding_obj);
194                 if (!encoding) {
195                     PyErr_Clear();
196                 }
197             }
198         }
199     }
200 
201     // Get sys.ps1 (as UTF-8)
202     attr = _PySys_GetAttr(tstate, &_Py_ID(ps1));
203     PyObject *ps1_obj = NULL;
204     const char *ps1 = "";
205     if (attr != NULL) {
206         ps1_obj = PyObject_Str(attr);
207         if (ps1_obj == NULL) {
208             PyErr_Clear();
209         }
210         else if (PyUnicode_Check(ps1_obj)) {
211             ps1 = PyUnicode_AsUTF8(ps1_obj);
212             if (ps1 == NULL) {
213                 PyErr_Clear();
214                 ps1 = "";
215             }
216         }
217     }
218 
219     // Get sys.ps2 (as UTF-8)
220     attr = _PySys_GetAttr(tstate, &_Py_ID(ps2));
221     PyObject *ps2_obj = NULL;
222     const char *ps2 = "";
223     if (attr != NULL) {
224         ps2_obj = PyObject_Str(attr);
225         if (ps2_obj == NULL) {
226             PyErr_Clear();
227         }
228         else if (PyUnicode_Check(ps2_obj)) {
229             ps2 = PyUnicode_AsUTF8(ps2_obj);
230             if (ps2 == NULL) {
231                 PyErr_Clear();
232                 ps2 = "";
233             }
234         }
235     }
236 
237     int errcode = 0;
238     *pmod = _PyParser_InteractiveASTFromFile(fp, filename, encoding,
239                                              Py_single_input, ps1, ps2,
240                                              flags, &errcode, interactive_src, arena);
241     Py_XDECREF(ps1_obj);
242     Py_XDECREF(ps2_obj);
243     Py_XDECREF(encoding_obj);
244 
245     if (*pmod == NULL) {
246         if (errcode == E_EOF) {
247             PyErr_Clear();
248             return E_EOF;
249         }
250         return -1;
251     }
252     return 0;
253 }
254 
255 
256 /* A PyRun_InteractiveOneObject() auxiliary function that does not print the
257  * error on failure. */
258 static int
PyRun_InteractiveOneObjectEx(FILE * fp,PyObject * filename,PyCompilerFlags * flags)259 PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
260                              PyCompilerFlags *flags)
261 {
262     PyArena *arena = _PyArena_New();
263     if (arena == NULL) {
264         return -1;
265     }
266 
267     mod_ty mod;
268     PyObject *interactive_src;
269     int parse_res = pyrun_one_parse_ast(fp, filename, flags, arena, &mod, &interactive_src);
270     if (parse_res != 0) {
271         _PyArena_Free(arena);
272         return parse_res;
273     }
274 
275     PyObject *main_module = PyImport_AddModuleRef("__main__");
276     if (main_module == NULL) {
277         _PyArena_Free(arena);
278         return -1;
279     }
280     PyObject *main_dict = PyModule_GetDict(main_module);  // borrowed ref
281 
282     PyObject *res = run_mod(mod, filename, main_dict, main_dict, flags, arena, interactive_src, 1);
283     Py_INCREF(interactive_src);
284     _PyArena_Free(arena);
285     Py_DECREF(main_module);
286     if (res == NULL) {
287         PyThreadState *tstate = _PyThreadState_GET();
288         PyObject *exc = _PyErr_GetRaisedException(tstate);
289         if (PyType_IsSubtype(Py_TYPE(exc),
290                              (PyTypeObject *) PyExc_SyntaxError))
291         {
292             /* fix "text" attribute */
293             assert(interactive_src != NULL);
294             PyObject *xs = PyUnicode_Splitlines(interactive_src, 1);
295             if (xs == NULL) {
296                 goto error;
297             }
298             PyObject *exc_lineno = PyObject_GetAttr(exc, &_Py_ID(lineno));
299             if (exc_lineno == NULL) {
300                 Py_DECREF(xs);
301                 goto error;
302             }
303             int n = PyLong_AsInt(exc_lineno);
304             Py_DECREF(exc_lineno);
305             if (n <= 0 || n > PyList_GET_SIZE(xs)) {
306                 Py_DECREF(xs);
307                 goto error;
308             }
309             PyObject *line = PyList_GET_ITEM(xs, n - 1);
310             PyObject_SetAttr(exc, &_Py_ID(text), line);
311             Py_DECREF(xs);
312         }
313 error:
314         Py_DECREF(interactive_src);
315         _PyErr_SetRaisedException(tstate, exc);
316         return -1;
317     }
318     Py_DECREF(interactive_src);
319     Py_DECREF(res);
320 
321     flush_io();
322     return 0;
323 }
324 
325 int
PyRun_InteractiveOneObject(FILE * fp,PyObject * filename,PyCompilerFlags * flags)326 PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
327 {
328     int res;
329 
330     res = PyRun_InteractiveOneObjectEx(fp, filename, flags);
331     if (res == -1) {
332         PyErr_Print();
333         flush_io();
334     }
335     return res;
336 }
337 
338 int
PyRun_InteractiveOneFlags(FILE * fp,const char * filename_str,PyCompilerFlags * flags)339 PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
340 {
341     PyObject *filename;
342     int res;
343 
344     filename = PyUnicode_DecodeFSDefault(filename_str);
345     if (filename == NULL) {
346         PyErr_Print();
347         return -1;
348     }
349     res = PyRun_InteractiveOneObject(fp, filename, flags);
350     Py_DECREF(filename);
351     return res;
352 }
353 
354 
355 /* Check whether a file maybe a pyc file: Look at the extension,
356    the file type, and, if we may close it, at the first few bytes. */
357 
358 static int
maybe_pyc_file(FILE * fp,PyObject * filename,int closeit)359 maybe_pyc_file(FILE *fp, PyObject *filename, int closeit)
360 {
361     PyObject *ext = PyUnicode_FromString(".pyc");
362     if (ext == NULL) {
363         return -1;
364     }
365     Py_ssize_t endswith = PyUnicode_Tailmatch(filename, ext, 0, PY_SSIZE_T_MAX, +1);
366     Py_DECREF(ext);
367     if (endswith) {
368         return 1;
369     }
370 
371     /* Only look into the file if we are allowed to close it, since
372        it then should also be seekable. */
373     if (!closeit) {
374         return 0;
375     }
376 
377     /* Read only two bytes of the magic. If the file was opened in
378        text mode, the bytes 3 and 4 of the magic (\r\n) might not
379        be read as they are on disk. */
380     unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
381     unsigned char buf[2];
382     /* Mess:  In case of -x, the stream is NOT at its start now,
383        and ungetc() was used to push back the first newline,
384        which makes the current stream position formally undefined,
385        and a x-platform nightmare.
386        Unfortunately, we have no direct way to know whether -x
387        was specified.  So we use a terrible hack:  if the current
388        stream position is not 0, we assume -x was specified, and
389        give up.  Bug 132850 on SourceForge spells out the
390        hopelessness of trying anything else (fseek and ftell
391        don't work predictably x-platform for text-mode files).
392     */
393     int ispyc = 0;
394     if (ftell(fp) == 0) {
395         if (fread(buf, 1, 2, fp) == 2 &&
396             ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
397             ispyc = 1;
398         rewind(fp);
399     }
400     return ispyc;
401 }
402 
403 
404 static int
set_main_loader(PyObject * d,PyObject * filename,const char * loader_name)405 set_main_loader(PyObject *d, PyObject *filename, const char *loader_name)
406 {
407     PyInterpreterState *interp = _PyInterpreterState_GET();
408     PyObject *loader_type = _PyImport_GetImportlibExternalLoader(interp,
409                                                                  loader_name);
410     if (loader_type == NULL) {
411         return -1;
412     }
413 
414     PyObject *loader = PyObject_CallFunction(loader_type,
415                                              "sO", "__main__", filename);
416     Py_DECREF(loader_type);
417     if (loader == NULL) {
418         return -1;
419     }
420 
421     if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
422         Py_DECREF(loader);
423         return -1;
424     }
425     Py_DECREF(loader);
426     return 0;
427 }
428 
429 
430 int
_PyRun_SimpleFileObject(FILE * fp,PyObject * filename,int closeit,PyCompilerFlags * flags)431 _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
432                         PyCompilerFlags *flags)
433 {
434     int ret = -1;
435 
436     PyObject *main_module = PyImport_AddModuleRef("__main__");
437     if (main_module == NULL)
438         return -1;
439     PyObject *dict = PyModule_GetDict(main_module);  // borrowed ref
440 
441     int set_file_name = 0;
442     int has_file = PyDict_ContainsString(dict, "__file__");
443     if (has_file < 0) {
444         goto done;
445     }
446     if (!has_file) {
447         if (PyDict_SetItemString(dict, "__file__", filename) < 0) {
448             goto done;
449         }
450         if (PyDict_SetItemString(dict, "__cached__", Py_None) < 0) {
451             goto done;
452         }
453         set_file_name = 1;
454     }
455 
456     int pyc = maybe_pyc_file(fp, filename, closeit);
457     if (pyc < 0) {
458         goto done;
459     }
460 
461     PyObject *v;
462     if (pyc) {
463         FILE *pyc_fp;
464         /* Try to run a pyc file. First, re-open in binary */
465         if (closeit) {
466             fclose(fp);
467         }
468 
469         pyc_fp = _Py_fopen_obj(filename, "rb");
470         if (pyc_fp == NULL) {
471             fprintf(stderr, "python: Can't reopen .pyc file\n");
472             goto done;
473         }
474 
475         if (set_main_loader(dict, filename, "SourcelessFileLoader") < 0) {
476             fprintf(stderr, "python: failed to set __main__.__loader__\n");
477             ret = -1;
478             fclose(pyc_fp);
479             goto done;
480         }
481         v = run_pyc_file(pyc_fp, dict, dict, flags);
482     } else {
483         /* When running from stdin, leave __main__.__loader__ alone */
484         if ((!PyUnicode_Check(filename) || !PyUnicode_EqualToUTF8(filename, "<stdin>")) &&
485             set_main_loader(dict, filename, "SourceFileLoader") < 0) {
486             fprintf(stderr, "python: failed to set __main__.__loader__\n");
487             ret = -1;
488             goto done;
489         }
490         v = pyrun_file(fp, filename, Py_file_input, dict, dict,
491                        closeit, flags);
492     }
493     flush_io();
494     if (v == NULL) {
495         Py_CLEAR(main_module);
496         PyErr_Print();
497         goto done;
498     }
499     Py_DECREF(v);
500     ret = 0;
501 
502   done:
503     if (set_file_name) {
504         if (PyDict_PopString(dict, "__file__", NULL) < 0) {
505             PyErr_Print();
506         }
507         if (PyDict_PopString(dict, "__cached__", NULL) < 0) {
508             PyErr_Print();
509         }
510     }
511     Py_XDECREF(main_module);
512     return ret;
513 }
514 
515 
516 int
PyRun_SimpleFileExFlags(FILE * fp,const char * filename,int closeit,PyCompilerFlags * flags)517 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
518                         PyCompilerFlags *flags)
519 {
520     PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
521     if (filename_obj == NULL) {
522         return -1;
523     }
524     int res = _PyRun_SimpleFileObject(fp, filename_obj, closeit, flags);
525     Py_DECREF(filename_obj);
526     return res;
527 }
528 
529 
530 int
_PyRun_SimpleStringFlagsWithName(const char * command,const char * name,PyCompilerFlags * flags)531 _PyRun_SimpleStringFlagsWithName(const char *command, const char* name, PyCompilerFlags *flags) {
532     PyObject *main_module = PyImport_AddModuleRef("__main__");
533     if (main_module == NULL) {
534         return -1;
535     }
536     PyObject *dict = PyModule_GetDict(main_module);  // borrowed ref
537 
538     PyObject *res = NULL;
539     if (name == NULL) {
540         res = PyRun_StringFlags(command, Py_file_input, dict, dict, flags);
541     } else {
542         PyObject* the_name = PyUnicode_FromString(name);
543         if (!the_name) {
544             PyErr_Print();
545             return -1;
546         }
547         res = _PyRun_StringFlagsWithName(command, the_name, Py_file_input, dict, dict, flags, 0);
548         Py_DECREF(the_name);
549     }
550     Py_DECREF(main_module);
551     if (res == NULL) {
552         PyErr_Print();
553         return -1;
554     }
555 
556     Py_DECREF(res);
557     return 0;
558 }
559 
560 int
PyRun_SimpleStringFlags(const char * command,PyCompilerFlags * flags)561 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
562 {
563     return _PyRun_SimpleStringFlagsWithName(command, NULL, flags);
564 }
565 
566 static int
parse_exit_code(PyObject * code,int * exitcode_p)567 parse_exit_code(PyObject *code, int *exitcode_p)
568 {
569     if (PyLong_Check(code)) {
570         // gh-125842: Use a long long to avoid an overflow error when `long`
571         // is 32-bit. We still truncate the result to an int.
572         int exitcode = (int)PyLong_AsLongLong(code);
573         if (exitcode == -1 && PyErr_Occurred()) {
574             // On overflow or other error, clear the exception and use -1
575             // as the exit code to match historical Python behavior.
576             PyErr_Clear();
577             *exitcode_p = -1;
578             return 1;
579         }
580         *exitcode_p = exitcode;
581         return 1;
582     }
583     else if (code == Py_None) {
584         *exitcode_p = 0;
585         return 1;
586     }
587     return 0;
588 }
589 
590 int
_Py_HandleSystemExit(int * exitcode_p)591 _Py_HandleSystemExit(int *exitcode_p)
592 {
593     int inspect = _Py_GetConfig()->inspect;
594     if (inspect) {
595         /* Don't exit if -i flag was given. This flag is set to 0
596          * when entering interactive mode for inspecting. */
597         return 0;
598     }
599 
600     if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
601         return 0;
602     }
603 
604     fflush(stdout);
605 
606     PyObject *exc = PyErr_GetRaisedException();
607     assert(exc != NULL && PyExceptionInstance_Check(exc));
608 
609     PyObject *code = PyObject_GetAttr(exc, &_Py_ID(code));
610     if (code == NULL) {
611         // If the exception has no 'code' attribute, print the exception below
612         PyErr_Clear();
613     }
614     else if (parse_exit_code(code, exitcode_p)) {
615         Py_DECREF(code);
616         Py_CLEAR(exc);
617         return 1;
618     }
619     else {
620         // If code is not an int or None, print it below
621         Py_SETREF(exc, code);
622     }
623 
624     PyThreadState *tstate = _PyThreadState_GET();
625     PyObject *sys_stderr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
626     if (sys_stderr != NULL && sys_stderr != Py_None) {
627         if (PyFile_WriteObject(exc, sys_stderr, Py_PRINT_RAW) < 0) {
628             PyErr_Clear();
629         }
630     }
631     else {
632         if (PyObject_Print(exc, stderr, Py_PRINT_RAW) < 0) {
633             PyErr_Clear();
634         }
635         fflush(stderr);
636     }
637     PySys_WriteStderr("\n");
638     Py_CLEAR(exc);
639     *exitcode_p = 1;
640     return 1;
641 }
642 
643 
644 static void
handle_system_exit(void)645 handle_system_exit(void)
646 {
647     int exitcode;
648     if (_Py_HandleSystemExit(&exitcode)) {
649         Py_Exit(exitcode);
650     }
651 }
652 
653 
654 static void
_PyErr_PrintEx(PyThreadState * tstate,int set_sys_last_vars)655 _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
656 {
657     PyObject *typ = NULL, *tb = NULL;
658     handle_system_exit();
659 
660     PyObject *exc = _PyErr_GetRaisedException(tstate);
661     if (exc == NULL) {
662         goto done;
663     }
664     assert(PyExceptionInstance_Check(exc));
665     typ = Py_NewRef(Py_TYPE(exc));
666     tb = PyException_GetTraceback(exc);
667     if (tb == NULL) {
668         tb = Py_NewRef(Py_None);
669     }
670 
671     if (set_sys_last_vars) {
672         if (_PySys_SetAttr(&_Py_ID(last_exc), exc) < 0) {
673             _PyErr_Clear(tstate);
674         }
675         /* Legacy version: */
676         if (_PySys_SetAttr(&_Py_ID(last_type), typ) < 0) {
677             _PyErr_Clear(tstate);
678         }
679         if (_PySys_SetAttr(&_Py_ID(last_value), exc) < 0) {
680             _PyErr_Clear(tstate);
681         }
682         if (_PySys_SetAttr(&_Py_ID(last_traceback), tb) < 0) {
683             _PyErr_Clear(tstate);
684         }
685     }
686     PyObject *hook = _PySys_GetAttr(tstate, &_Py_ID(excepthook));
687     if (_PySys_Audit(tstate, "sys.excepthook", "OOOO", hook ? hook : Py_None,
688                      typ, exc, tb) < 0) {
689         if (PyErr_ExceptionMatches(PyExc_RuntimeError)) {
690             PyErr_Clear();
691             goto done;
692         }
693         PyErr_FormatUnraisable("Exception ignored in audit hook");
694     }
695     if (hook) {
696         PyObject* args[3] = {typ, exc, tb};
697         PyObject *result = PyObject_Vectorcall(hook, args, 3, NULL);
698         if (result == NULL) {
699             handle_system_exit();
700 
701             PyObject *exc2 = _PyErr_GetRaisedException(tstate);
702             assert(exc2 && PyExceptionInstance_Check(exc2));
703             fflush(stdout);
704             PySys_WriteStderr("Error in sys.excepthook:\n");
705             PyErr_DisplayException(exc2);
706             PySys_WriteStderr("\nOriginal exception was:\n");
707             PyErr_DisplayException(exc);
708             Py_DECREF(exc2);
709         }
710         else {
711             Py_DECREF(result);
712         }
713     }
714     else {
715         PySys_WriteStderr("sys.excepthook is missing\n");
716         PyErr_DisplayException(exc);
717     }
718 
719 done:
720     Py_XDECREF(typ);
721     Py_XDECREF(exc);
722     Py_XDECREF(tb);
723 }
724 
725 void
_PyErr_Print(PyThreadState * tstate)726 _PyErr_Print(PyThreadState *tstate)
727 {
728     _PyErr_PrintEx(tstate, 1);
729 }
730 
731 void
PyErr_PrintEx(int set_sys_last_vars)732 PyErr_PrintEx(int set_sys_last_vars)
733 {
734     PyThreadState *tstate = _PyThreadState_GET();
735     _PyErr_PrintEx(tstate, set_sys_last_vars);
736 }
737 
738 void
PyErr_Print(void)739 PyErr_Print(void)
740 {
741     PyErr_PrintEx(1);
742 }
743 
744 struct exception_print_context
745 {
746     PyObject *file;
747     PyObject *seen;            // Prevent cycles in recursion
748 };
749 
750 static int
print_exception_invalid_type(struct exception_print_context * ctx,PyObject * value)751 print_exception_invalid_type(struct exception_print_context *ctx,
752                              PyObject *value)
753 {
754     PyObject *f = ctx->file;
755     const char *const msg = "TypeError: print_exception(): Exception expected "
756                             "for value, ";
757     if (PyFile_WriteString(msg, f) < 0) {
758         return -1;
759     }
760     if (PyFile_WriteString(Py_TYPE(value)->tp_name, f) < 0) {
761         return -1;
762     }
763     if (PyFile_WriteString(" found\n", f) < 0) {
764         return -1;
765     }
766     return 0;
767 }
768 
769 static int
print_exception_traceback(struct exception_print_context * ctx,PyObject * value)770 print_exception_traceback(struct exception_print_context *ctx, PyObject *value)
771 {
772     PyObject *f = ctx->file;
773     int err = 0;
774 
775     PyObject *tb = PyException_GetTraceback(value);
776     if (tb && tb != Py_None) {
777         const char *header = EXCEPTION_TB_HEADER;
778         err = _PyTraceBack_Print(tb, header, f);
779     }
780     Py_XDECREF(tb);
781     return err;
782 }
783 
784 static int
print_exception_file_and_line(struct exception_print_context * ctx,PyObject ** value_p)785 print_exception_file_and_line(struct exception_print_context *ctx,
786                               PyObject **value_p)
787 {
788     PyObject *f = ctx->file;
789 
790     PyObject *tmp;
791     int res = PyObject_GetOptionalAttr(*value_p, &_Py_ID(print_file_and_line), &tmp);
792     if (res <= 0) {
793         if (res < 0) {
794             PyErr_Clear();
795         }
796         return 0;
797     }
798     Py_DECREF(tmp);
799 
800     PyObject *filename = NULL;
801     Py_ssize_t lineno = 0;
802     PyObject* v = PyObject_GetAttr(*value_p, &_Py_ID(filename));
803     if (!v) {
804         return -1;
805     }
806     if (v == Py_None) {
807         Py_DECREF(v);
808         _Py_DECLARE_STR(anon_string, "<string>");
809         filename = Py_NewRef(&_Py_STR(anon_string));
810     }
811     else {
812         filename = v;
813     }
814 
815     PyObject *line = PyUnicode_FromFormat("  File \"%S\", line %zd\n",
816                                           filename, lineno);
817     Py_DECREF(filename);
818     if (line == NULL) {
819         goto error;
820     }
821     if (PyFile_WriteObject(line, f, Py_PRINT_RAW) < 0) {
822         goto error;
823     }
824     Py_CLEAR(line);
825 
826     assert(!PyErr_Occurred());
827     return 0;
828 
829 error:
830     Py_XDECREF(line);
831     return -1;
832 }
833 
834 /* Prints the message line: module.qualname[: str(exc)] */
835 static int
print_exception_message(struct exception_print_context * ctx,PyObject * type,PyObject * value)836 print_exception_message(struct exception_print_context *ctx, PyObject *type,
837                         PyObject *value)
838 {
839     PyObject *f = ctx->file;
840 
841     if (PyErr_GivenExceptionMatches(value, PyExc_MemoryError)) {
842         // The Python APIs in this function require allocating memory
843         // for various objects. If we're out of memory, we can't do that,
844         return -1;
845     }
846 
847     assert(PyExceptionClass_Check(type));
848 
849     PyObject *modulename = PyObject_GetAttr(type, &_Py_ID(__module__));
850     if (modulename == NULL || !PyUnicode_Check(modulename)) {
851         Py_XDECREF(modulename);
852         PyErr_Clear();
853         if (PyFile_WriteString("<unknown>.", f) < 0) {
854             return -1;
855         }
856     }
857     else {
858         if (!_PyUnicode_Equal(modulename, &_Py_ID(builtins)) &&
859             !_PyUnicode_Equal(modulename, &_Py_ID(__main__)))
860         {
861             int res = PyFile_WriteObject(modulename, f, Py_PRINT_RAW);
862             Py_DECREF(modulename);
863             if (res < 0) {
864                 return -1;
865             }
866             if (PyFile_WriteString(".", f) < 0) {
867                 return -1;
868             }
869         }
870         else {
871             Py_DECREF(modulename);
872         }
873     }
874 
875     PyObject *qualname = PyType_GetQualName((PyTypeObject *)type);
876     if (qualname == NULL || !PyUnicode_Check(qualname)) {
877         Py_XDECREF(qualname);
878         PyErr_Clear();
879         if (PyFile_WriteString("<unknown>", f) < 0) {
880             return -1;
881         }
882     }
883     else {
884         int res = PyFile_WriteObject(qualname, f, Py_PRINT_RAW);
885         Py_DECREF(qualname);
886         if (res < 0) {
887             return -1;
888         }
889     }
890 
891     if (Py_IsNone(value)) {
892         return 0;
893     }
894 
895     PyObject *s = PyObject_Str(value);
896     if (s == NULL) {
897         PyErr_Clear();
898         if (PyFile_WriteString(": <exception str() failed>", f) < 0) {
899             return -1;
900         }
901     }
902     else {
903         /* only print colon if the str() of the
904            object is not the empty string
905         */
906         if (!PyUnicode_Check(s) || PyUnicode_GetLength(s) != 0) {
907             if (PyFile_WriteString(": ", f) < 0) {
908                 Py_DECREF(s);
909                 return -1;
910             }
911         }
912         int res = PyFile_WriteObject(s, f, Py_PRINT_RAW);
913         Py_DECREF(s);
914         if (res < 0) {
915             return -1;
916         }
917     }
918 
919     return 0;
920 }
921 
922 static int
print_exception(struct exception_print_context * ctx,PyObject * value)923 print_exception(struct exception_print_context *ctx, PyObject *value)
924 {
925     PyObject *f = ctx->file;
926 
927     if (!PyExceptionInstance_Check(value)) {
928         return print_exception_invalid_type(ctx, value);
929     }
930 
931     Py_INCREF(value);
932     fflush(stdout);
933 
934     if (print_exception_traceback(ctx, value) < 0) {
935         goto error;
936     }
937 
938     /* grab the type and notes now because value can change below */
939     PyObject *type = (PyObject *) Py_TYPE(value);
940 
941     if (print_exception_file_and_line(ctx, &value) < 0) {
942         goto error;
943     }
944     if (print_exception_message(ctx, type, value) < 0) {
945         goto error;
946     }
947     if (PyFile_WriteString("\n", f) < 0) {
948         goto error;
949     }
950     Py_DECREF(value);
951     assert(!PyErr_Occurred());
952     return 0;
953 error:
954     Py_DECREF(value);
955     return -1;
956 }
957 
958 static const char cause_message[] =
959     "The above exception was the direct cause "
960     "of the following exception:\n";
961 
962 static const char context_message[] =
963     "During handling of the above exception, "
964     "another exception occurred:\n";
965 
966 static int
967 print_exception_recursive(struct exception_print_context*, PyObject*);
968 
969 static int
print_chained(struct exception_print_context * ctx,PyObject * value,const char * message,const char * tag)970 print_chained(struct exception_print_context* ctx, PyObject *value,
971               const char * message, const char *tag)
972 {
973     PyObject *f = ctx->file;
974     if (_Py_EnterRecursiveCall(" in print_chained")) {
975         return -1;
976     }
977     int res = print_exception_recursive(ctx, value);
978     _Py_LeaveRecursiveCall();
979     if (res < 0) {
980         return -1;
981     }
982 
983     if (PyFile_WriteString("\n", f) < 0) {
984         return -1;
985     }
986     if (PyFile_WriteString(message, f) < 0) {
987         return -1;
988     }
989     if (PyFile_WriteString("\n", f) < 0) {
990         return -1;
991     }
992     return 0;
993 }
994 
995 /* Return true if value is in seen or there was a lookup error.
996  * Return false if lookup succeeded and the item was not found.
997  * We suppress errors because this makes us err on the side of
998  * under-printing which is better than over-printing irregular
999  * exceptions (e.g., unhashable ones).
1000  */
1001 static bool
print_exception_seen_lookup(struct exception_print_context * ctx,PyObject * value)1002 print_exception_seen_lookup(struct exception_print_context *ctx,
1003                             PyObject *value)
1004 {
1005     PyObject *check_id = PyLong_FromVoidPtr(value);
1006     if (check_id == NULL) {
1007         PyErr_Clear();
1008         return true;
1009     }
1010 
1011     int in_seen = PySet_Contains(ctx->seen, check_id);
1012     Py_DECREF(check_id);
1013     if (in_seen == -1) {
1014         PyErr_Clear();
1015         return true;
1016     }
1017 
1018     if (in_seen == 1) {
1019         /* value is in seen */
1020         return true;
1021     }
1022     return false;
1023 }
1024 
1025 static int
print_exception_cause_and_context(struct exception_print_context * ctx,PyObject * value)1026 print_exception_cause_and_context(struct exception_print_context *ctx,
1027                                   PyObject *value)
1028 {
1029     PyObject *value_id = PyLong_FromVoidPtr(value);
1030     if (value_id == NULL || PySet_Add(ctx->seen, value_id) == -1) {
1031         PyErr_Clear();
1032         Py_XDECREF(value_id);
1033         return 0;
1034     }
1035     Py_DECREF(value_id);
1036 
1037     if (!PyExceptionInstance_Check(value)) {
1038         return 0;
1039     }
1040 
1041     PyObject *cause = PyException_GetCause(value);
1042     if (cause) {
1043         int err = 0;
1044         if (!print_exception_seen_lookup(ctx, cause)) {
1045             err = print_chained(ctx, cause, cause_message, "cause");
1046         }
1047         Py_DECREF(cause);
1048         return err;
1049     }
1050     if (((PyBaseExceptionObject *)value)->suppress_context) {
1051         return 0;
1052     }
1053     PyObject *context = PyException_GetContext(value);
1054     if (context) {
1055         int err = 0;
1056         if (!print_exception_seen_lookup(ctx, context)) {
1057             err = print_chained(ctx, context, context_message, "context");
1058         }
1059         Py_DECREF(context);
1060         return err;
1061     }
1062     return 0;
1063 }
1064 
1065 static int
print_exception_recursive(struct exception_print_context * ctx,PyObject * value)1066 print_exception_recursive(struct exception_print_context *ctx, PyObject *value)
1067 {
1068     if (_Py_EnterRecursiveCall(" in print_exception_recursive")) {
1069         return -1;
1070     }
1071     if (ctx->seen != NULL) {
1072         /* Exception chaining */
1073         if (print_exception_cause_and_context(ctx, value) < 0) {
1074             goto error;
1075         }
1076     }
1077     if (print_exception(ctx, value) < 0) {
1078         goto error;
1079     }
1080     assert(!PyErr_Occurred());
1081 
1082     _Py_LeaveRecursiveCall();
1083     return 0;
1084 error:
1085     _Py_LeaveRecursiveCall();
1086     return -1;
1087 }
1088 
1089 void
_PyErr_Display(PyObject * file,PyObject * unused,PyObject * value,PyObject * tb)1090 _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
1091 {
1092     assert(value != NULL);
1093     assert(file != NULL && file != Py_None);
1094     if (PyExceptionInstance_Check(value)
1095         && tb != NULL && PyTraceBack_Check(tb)) {
1096         /* Put the traceback on the exception, otherwise it won't get
1097            displayed.  See issue #18776. */
1098         PyObject *cur_tb = PyException_GetTraceback(value);
1099         if (cur_tb == NULL) {
1100             PyException_SetTraceback(value, tb);
1101         }
1102         else {
1103             Py_DECREF(cur_tb);
1104         }
1105     }
1106 
1107     int unhandled_keyboard_interrupt = _PyRuntime.signals.unhandled_keyboard_interrupt;
1108 
1109     // Try first with the stdlib traceback module
1110     PyObject *traceback_module = PyImport_ImportModule("traceback");
1111 
1112     if (traceback_module == NULL) {
1113         goto fallback;
1114     }
1115 
1116     PyObject *print_exception_fn = PyObject_GetAttrString(traceback_module, "_print_exception_bltin");
1117 
1118     if (print_exception_fn == NULL || !PyCallable_Check(print_exception_fn)) {
1119         Py_DECREF(traceback_module);
1120         goto fallback;
1121     }
1122 
1123     PyObject* result = PyObject_CallOneArg(print_exception_fn, value);
1124 
1125     Py_DECREF(traceback_module);
1126     Py_XDECREF(print_exception_fn);
1127     if (result) {
1128         Py_DECREF(result);
1129         _PyRuntime.signals.unhandled_keyboard_interrupt = unhandled_keyboard_interrupt;
1130         return;
1131     }
1132 fallback:
1133     _PyRuntime.signals.unhandled_keyboard_interrupt = unhandled_keyboard_interrupt;
1134 #ifdef Py_DEBUG
1135      if (PyErr_Occurred()) {
1136          PyErr_FormatUnraisable(
1137              "Exception ignored in the internal traceback machinery");
1138      }
1139 #endif
1140     PyErr_Clear();
1141     struct exception_print_context ctx;
1142     ctx.file = file;
1143 
1144     /* We choose to ignore seen being possibly NULL, and report
1145        at least the main exception (it could be a MemoryError).
1146     */
1147     ctx.seen = PySet_New(NULL);
1148     if (ctx.seen == NULL) {
1149         PyErr_Clear();
1150     }
1151     if (print_exception_recursive(&ctx, value) < 0) {
1152         PyErr_Clear();
1153         _PyObject_Dump(value);
1154         fprintf(stderr, "lost sys.stderr\n");
1155     }
1156     Py_XDECREF(ctx.seen);
1157 
1158     /* Call file.flush() */
1159     if (_PyFile_Flush(file) < 0) {
1160         /* Silently ignore file.flush() error */
1161         PyErr_Clear();
1162     }
1163 }
1164 
1165 void
PyErr_Display(PyObject * unused,PyObject * value,PyObject * tb)1166 PyErr_Display(PyObject *unused, PyObject *value, PyObject *tb)
1167 {
1168     PyThreadState *tstate = _PyThreadState_GET();
1169     PyObject *file = _PySys_GetAttr(tstate, &_Py_ID(stderr));
1170     if (file == NULL) {
1171         _PyObject_Dump(value);
1172         fprintf(stderr, "lost sys.stderr\n");
1173         return;
1174     }
1175     if (file == Py_None) {
1176         return;
1177     }
1178     Py_INCREF(file);
1179     _PyErr_Display(file, NULL, value, tb);
1180     Py_DECREF(file);
1181 }
1182 
_PyErr_DisplayException(PyObject * file,PyObject * exc)1183 void _PyErr_DisplayException(PyObject *file, PyObject *exc)
1184 {
1185     _PyErr_Display(file, NULL, exc, NULL);
1186 }
1187 
PyErr_DisplayException(PyObject * exc)1188 void PyErr_DisplayException(PyObject *exc)
1189 {
1190     PyErr_Display(NULL, exc, NULL);
1191 }
1192 
1193 static PyObject *
_PyRun_StringFlagsWithName(const char * str,PyObject * name,int start,PyObject * globals,PyObject * locals,PyCompilerFlags * flags,int generate_new_source)1194 _PyRun_StringFlagsWithName(const char *str, PyObject* name, int start,
1195                            PyObject *globals, PyObject *locals, PyCompilerFlags *flags,
1196                            int generate_new_source)
1197 {
1198     PyObject *ret = NULL;
1199     mod_ty mod;
1200     PyArena *arena;
1201 
1202     arena = _PyArena_New();
1203     if (arena == NULL)
1204         return NULL;
1205 
1206     PyObject* source = NULL;
1207     _Py_DECLARE_STR(anon_string, "<string>");
1208 
1209     if (name) {
1210         source = PyUnicode_FromString(str);
1211         if (!source) {
1212             PyErr_Clear();
1213         }
1214     } else {
1215         name = &_Py_STR(anon_string);
1216     }
1217 
1218     mod = _PyParser_ASTFromString(str, name, start, flags, arena);
1219 
1220    if (mod != NULL) {
1221         ret = run_mod(mod, name, globals, locals, flags, arena, source, generate_new_source);
1222     }
1223     Py_XDECREF(source);
1224     _PyArena_Free(arena);
1225     return ret;
1226 }
1227 
1228 
1229 PyObject *
PyRun_StringFlags(const char * str,int start,PyObject * globals,PyObject * locals,PyCompilerFlags * flags)1230 PyRun_StringFlags(const char *str, int start, PyObject *globals,
1231                      PyObject *locals, PyCompilerFlags *flags) {
1232 
1233     return _PyRun_StringFlagsWithName(str, NULL, start, globals, locals, flags, 0);
1234 }
1235 
1236 static PyObject *
pyrun_file(FILE * fp,PyObject * filename,int start,PyObject * globals,PyObject * locals,int closeit,PyCompilerFlags * flags)1237 pyrun_file(FILE *fp, PyObject *filename, int start, PyObject *globals,
1238            PyObject *locals, int closeit, PyCompilerFlags *flags)
1239 {
1240     PyArena *arena = _PyArena_New();
1241     if (arena == NULL) {
1242         return NULL;
1243     }
1244 
1245     mod_ty mod;
1246     mod = _PyParser_ASTFromFile(fp, filename, NULL, start, NULL, NULL,
1247                                 flags, NULL, arena);
1248 
1249     if (closeit) {
1250         fclose(fp);
1251     }
1252 
1253     PyObject *ret;
1254     if (mod != NULL) {
1255         ret = run_mod(mod, filename, globals, locals, flags, arena, NULL, 0);
1256     }
1257     else {
1258         ret = NULL;
1259     }
1260     _PyArena_Free(arena);
1261 
1262     return ret;
1263 }
1264 
1265 
1266 PyObject *
PyRun_FileExFlags(FILE * fp,const char * filename,int start,PyObject * globals,PyObject * locals,int closeit,PyCompilerFlags * flags)1267 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1268                   PyObject *locals, int closeit, PyCompilerFlags *flags)
1269 {
1270     PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
1271     if (filename_obj == NULL) {
1272         return NULL;
1273     }
1274 
1275     PyObject *res = pyrun_file(fp, filename_obj, start, globals,
1276                                locals, closeit, flags);
1277     Py_DECREF(filename_obj);
1278     return res;
1279 
1280 }
1281 
1282 static void
flush_io_stream(PyThreadState * tstate,PyObject * name)1283 flush_io_stream(PyThreadState *tstate, PyObject *name)
1284 {
1285     PyObject *f = _PySys_GetAttr(tstate, name);
1286     if (f != NULL) {
1287         if (_PyFile_Flush(f) < 0) {
1288             PyErr_Clear();
1289         }
1290     }
1291 }
1292 
1293 static void
flush_io(void)1294 flush_io(void)
1295 {
1296     PyThreadState *tstate = _PyThreadState_GET();
1297     PyObject *exc = _PyErr_GetRaisedException(tstate);
1298     flush_io_stream(tstate, &_Py_ID(stderr));
1299     flush_io_stream(tstate, &_Py_ID(stdout));
1300     _PyErr_SetRaisedException(tstate, exc);
1301 }
1302 
1303 static PyObject *
run_eval_code_obj(PyThreadState * tstate,PyCodeObject * co,PyObject * globals,PyObject * locals)1304 run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, PyObject *locals)
1305 {
1306     PyObject *v;
1307     /*
1308      * We explicitly re-initialize _Py_UnhandledKeyboardInterrupt every eval
1309      * _just in case_ someone is calling into an embedded Python where they
1310      * don't care about an uncaught KeyboardInterrupt exception (why didn't they
1311      * leave config.install_signal_handlers set to 0?!?) but then later call
1312      * Py_Main() itself (which _checks_ this flag and dies with a signal after
1313      * its interpreter exits).  We don't want a previous embedded interpreter's
1314      * uncaught exception to trigger an unexplained signal exit from a future
1315      * Py_Main() based one.
1316      */
1317     // XXX Isn't this dealt with by the move to _PyRuntimeState?
1318     _PyRuntime.signals.unhandled_keyboard_interrupt = 0;
1319 
1320     /* Set globals['__builtins__'] if it doesn't exist */
1321     if (!globals || !PyDict_Check(globals)) {
1322         PyErr_SetString(PyExc_SystemError, "globals must be a real dict");
1323         return NULL;
1324     }
1325     int has_builtins = PyDict_ContainsString(globals, "__builtins__");
1326     if (has_builtins < 0) {
1327         return NULL;
1328     }
1329     if (!has_builtins) {
1330         if (PyDict_SetItemString(globals, "__builtins__",
1331                                  tstate->interp->builtins) < 0)
1332         {
1333             return NULL;
1334         }
1335     }
1336 
1337     v = PyEval_EvalCode((PyObject*)co, globals, locals);
1338     if (!v && _PyErr_Occurred(tstate) == PyExc_KeyboardInterrupt) {
1339         _PyRuntime.signals.unhandled_keyboard_interrupt = 1;
1340     }
1341     return v;
1342 }
1343 
1344 static PyObject *
run_mod(mod_ty mod,PyObject * filename,PyObject * globals,PyObject * locals,PyCompilerFlags * flags,PyArena * arena,PyObject * interactive_src,int generate_new_source)1345 run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
1346             PyCompilerFlags *flags, PyArena *arena, PyObject* interactive_src,
1347             int generate_new_source)
1348 {
1349     PyThreadState *tstate = _PyThreadState_GET();
1350     PyObject* interactive_filename = filename;
1351     if (interactive_src) {
1352         PyInterpreterState *interp = tstate->interp;
1353         if (generate_new_source) {
1354             interactive_filename = PyUnicode_FromFormat(
1355                 "%U-%d", filename, interp->_interactive_src_count++);
1356         } else {
1357             Py_INCREF(interactive_filename);
1358         }
1359         if (interactive_filename == NULL) {
1360             return NULL;
1361         }
1362     }
1363 
1364     PyCodeObject *co = _PyAST_Compile(mod, interactive_filename, flags, -1, arena);
1365     if (co == NULL) {
1366         if (interactive_src) {
1367             Py_DECREF(interactive_filename);
1368         }
1369         return NULL;
1370     }
1371 
1372     if (interactive_src) {
1373         PyObject *linecache_module = PyImport_ImportModule("linecache");
1374 
1375         if (linecache_module == NULL) {
1376             Py_DECREF(co);
1377             Py_DECREF(interactive_filename);
1378             return NULL;
1379         }
1380 
1381         PyObject *print_tb_func = PyObject_GetAttrString(linecache_module, "_register_code");
1382 
1383         if (print_tb_func == NULL) {
1384             Py_DECREF(co);
1385             Py_DECREF(interactive_filename);
1386             Py_DECREF(linecache_module);
1387             return NULL;
1388         }
1389 
1390         if (!PyCallable_Check(print_tb_func)) {
1391             Py_DECREF(co);
1392             Py_DECREF(interactive_filename);
1393             Py_DECREF(linecache_module);
1394             Py_DECREF(print_tb_func);
1395             PyErr_SetString(PyExc_ValueError, "linecache._register_code is not callable");
1396             return NULL;
1397         }
1398 
1399         PyObject* result = PyObject_CallFunction(
1400             print_tb_func, "OOO",
1401             interactive_filename,
1402             interactive_src,
1403             filename
1404         );
1405 
1406         Py_DECREF(interactive_filename);
1407 
1408         Py_DECREF(linecache_module);
1409         Py_XDECREF(print_tb_func);
1410         Py_XDECREF(result);
1411         if (!result) {
1412             Py_DECREF(co);
1413             return NULL;
1414         }
1415     }
1416 
1417     if (_PySys_Audit(tstate, "exec", "O", co) < 0) {
1418         Py_DECREF(co);
1419         return NULL;
1420     }
1421 
1422     PyObject *v = run_eval_code_obj(tstate, co, globals, locals);
1423     Py_DECREF(co);
1424     return v;
1425 }
1426 
1427 static PyObject *
run_pyc_file(FILE * fp,PyObject * globals,PyObject * locals,PyCompilerFlags * flags)1428 run_pyc_file(FILE *fp, PyObject *globals, PyObject *locals,
1429              PyCompilerFlags *flags)
1430 {
1431     PyThreadState *tstate = _PyThreadState_GET();
1432     PyCodeObject *co;
1433     PyObject *v;
1434     long magic;
1435     long PyImport_GetMagicNumber(void);
1436 
1437     magic = PyMarshal_ReadLongFromFile(fp);
1438     if (magic != PyImport_GetMagicNumber()) {
1439         if (!PyErr_Occurred())
1440             PyErr_SetString(PyExc_RuntimeError,
1441                        "Bad magic number in .pyc file");
1442         goto error;
1443     }
1444     /* Skip the rest of the header. */
1445     (void) PyMarshal_ReadLongFromFile(fp);
1446     (void) PyMarshal_ReadLongFromFile(fp);
1447     (void) PyMarshal_ReadLongFromFile(fp);
1448     if (PyErr_Occurred()) {
1449         goto error;
1450     }
1451     v = PyMarshal_ReadLastObjectFromFile(fp);
1452     if (v == NULL || !PyCode_Check(v)) {
1453         Py_XDECREF(v);
1454         PyErr_SetString(PyExc_RuntimeError,
1455                    "Bad code object in .pyc file");
1456         goto error;
1457     }
1458     fclose(fp);
1459     co = (PyCodeObject *)v;
1460     v = run_eval_code_obj(tstate, co, globals, locals);
1461     if (v && flags)
1462         flags->cf_flags |= (co->co_flags & PyCF_MASK);
1463     Py_DECREF(co);
1464     return v;
1465 error:
1466     fclose(fp);
1467     return NULL;
1468 }
1469 
1470 PyObject *
Py_CompileStringObject(const char * str,PyObject * filename,int start,PyCompilerFlags * flags,int optimize)1471 Py_CompileStringObject(const char *str, PyObject *filename, int start,
1472                        PyCompilerFlags *flags, int optimize)
1473 {
1474     PyCodeObject *co;
1475     mod_ty mod;
1476     PyArena *arena = _PyArena_New();
1477     if (arena == NULL)
1478         return NULL;
1479 
1480     mod = _PyParser_ASTFromString(str, filename, start, flags, arena);
1481     if (mod == NULL) {
1482         _PyArena_Free(arena);
1483         return NULL;
1484     }
1485     if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1486         if ((flags->cf_flags & PyCF_OPTIMIZED_AST) == PyCF_OPTIMIZED_AST) {
1487             if (_PyCompile_AstOptimize(mod, filename, flags, optimize, arena) < 0) {
1488                 return NULL;
1489             }
1490         }
1491         PyObject *result = PyAST_mod2obj(mod);
1492         _PyArena_Free(arena);
1493         return result;
1494     }
1495     co = _PyAST_Compile(mod, filename, flags, optimize, arena);
1496     _PyArena_Free(arena);
1497     return (PyObject *)co;
1498 }
1499 
1500 PyObject *
Py_CompileStringExFlags(const char * str,const char * filename_str,int start,PyCompilerFlags * flags,int optimize)1501 Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
1502                         PyCompilerFlags *flags, int optimize)
1503 {
1504     PyObject *filename, *co;
1505     filename = PyUnicode_DecodeFSDefault(filename_str);
1506     if (filename == NULL)
1507         return NULL;
1508     co = Py_CompileStringObject(str, filename, start, flags, optimize);
1509     Py_DECREF(filename);
1510     return co;
1511 }
1512 
1513 const char *
_Py_SourceAsString(PyObject * cmd,const char * funcname,const char * what,PyCompilerFlags * cf,PyObject ** cmd_copy)1514 _Py_SourceAsString(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
1515 {
1516     const char *str;
1517     Py_ssize_t size;
1518     Py_buffer view;
1519 
1520     *cmd_copy = NULL;
1521     if (PyUnicode_Check(cmd)) {
1522         cf->cf_flags |= PyCF_IGNORE_COOKIE;
1523         str = PyUnicode_AsUTF8AndSize(cmd, &size);
1524         if (str == NULL)
1525             return NULL;
1526     }
1527     else if (PyBytes_Check(cmd)) {
1528         str = PyBytes_AS_STRING(cmd);
1529         size = PyBytes_GET_SIZE(cmd);
1530     }
1531     else if (PyByteArray_Check(cmd)) {
1532         str = PyByteArray_AS_STRING(cmd);
1533         size = PyByteArray_GET_SIZE(cmd);
1534     }
1535     else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
1536         /* Copy to NUL-terminated buffer. */
1537         *cmd_copy = PyBytes_FromStringAndSize(
1538             (const char *)view.buf, view.len);
1539         PyBuffer_Release(&view);
1540         if (*cmd_copy == NULL) {
1541             return NULL;
1542         }
1543         str = PyBytes_AS_STRING(*cmd_copy);
1544         size = PyBytes_GET_SIZE(*cmd_copy);
1545     }
1546     else {
1547         PyErr_Format(PyExc_TypeError,
1548             "%s() arg 1 must be a %s object",
1549             funcname, what);
1550         return NULL;
1551     }
1552 
1553     if (strlen(str) != (size_t)size) {
1554         PyErr_SetString(PyExc_SyntaxError,
1555             "source code string cannot contain null bytes");
1556         Py_CLEAR(*cmd_copy);
1557         return NULL;
1558     }
1559     return str;
1560 }
1561 
1562 #if defined(USE_STACKCHECK)
1563 #if defined(WIN32) && defined(_MSC_VER)
1564 
1565 /* Stack checking for Microsoft C */
1566 
1567 #include <malloc.h>
1568 #include <excpt.h>
1569 
1570 /*
1571  * Return non-zero when we run out of memory on the stack; zero otherwise.
1572  */
1573 int
PyOS_CheckStack(void)1574 PyOS_CheckStack(void)
1575 {
1576     __try {
1577         /* alloca throws a stack overflow exception if there's
1578            not enough space left on the stack */
1579         alloca(PYOS_STACK_MARGIN * sizeof(void*));
1580         return 0;
1581     } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1582                     EXCEPTION_EXECUTE_HANDLER :
1583             EXCEPTION_CONTINUE_SEARCH) {
1584         int errcode = _resetstkoflw();
1585         if (errcode == 0)
1586         {
1587             Py_FatalError("Could not reset the stack!");
1588         }
1589     }
1590     return 1;
1591 }
1592 
1593 #endif /* WIN32 && _MSC_VER */
1594 
1595 /* Alternate implementations can be added here... */
1596 
1597 #endif /* USE_STACKCHECK */
1598 
1599 /* Deprecated C API functions still provided for binary compatibility */
1600 
1601 #undef PyRun_AnyFile
1602 PyAPI_FUNC(int)
PyRun_AnyFile(FILE * fp,const char * name)1603 PyRun_AnyFile(FILE *fp, const char *name)
1604 {
1605     return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1606 }
1607 
1608 #undef PyRun_AnyFileEx
1609 PyAPI_FUNC(int)
PyRun_AnyFileEx(FILE * fp,const char * name,int closeit)1610 PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1611 {
1612     return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1613 }
1614 
1615 #undef PyRun_AnyFileFlags
1616 PyAPI_FUNC(int)
PyRun_AnyFileFlags(FILE * fp,const char * name,PyCompilerFlags * flags)1617 PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1618 {
1619     return PyRun_AnyFileExFlags(fp, name, 0, flags);
1620 }
1621 
1622 #undef PyRun_File
1623 PyAPI_FUNC(PyObject *)
PyRun_File(FILE * fp,const char * p,int s,PyObject * g,PyObject * l)1624 PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1625 {
1626     return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1627 }
1628 
1629 #undef PyRun_FileEx
1630 PyAPI_FUNC(PyObject *)
PyRun_FileEx(FILE * fp,const char * p,int s,PyObject * g,PyObject * l,int c)1631 PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1632 {
1633     return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1634 }
1635 
1636 #undef PyRun_FileFlags
1637 PyAPI_FUNC(PyObject *)
PyRun_FileFlags(FILE * fp,const char * p,int s,PyObject * g,PyObject * l,PyCompilerFlags * flags)1638 PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1639                 PyCompilerFlags *flags)
1640 {
1641     return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1642 }
1643 
1644 #undef PyRun_SimpleFile
1645 PyAPI_FUNC(int)
PyRun_SimpleFile(FILE * f,const char * p)1646 PyRun_SimpleFile(FILE *f, const char *p)
1647 {
1648     return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1649 }
1650 
1651 #undef PyRun_SimpleFileEx
1652 PyAPI_FUNC(int)
PyRun_SimpleFileEx(FILE * f,const char * p,int c)1653 PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1654 {
1655     return PyRun_SimpleFileExFlags(f, p, c, NULL);
1656 }
1657 
1658 
1659 #undef PyRun_String
1660 PyAPI_FUNC(PyObject *)
PyRun_String(const char * str,int s,PyObject * g,PyObject * l)1661 PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1662 {
1663     return PyRun_StringFlags(str, s, g, l, NULL);
1664 }
1665 
1666 #undef PyRun_SimpleString
1667 PyAPI_FUNC(int)
PyRun_SimpleString(const char * s)1668 PyRun_SimpleString(const char *s)
1669 {
1670     return PyRun_SimpleStringFlags(s, NULL);
1671 }
1672 
1673 #undef Py_CompileString
1674 PyAPI_FUNC(PyObject *)
Py_CompileString(const char * str,const char * p,int s)1675 Py_CompileString(const char *str, const char *p, int s)
1676 {
1677     return Py_CompileStringExFlags(str, p, s, NULL, -1);
1678 }
1679 
1680 #undef Py_CompileStringFlags
1681 PyAPI_FUNC(PyObject *)
Py_CompileStringFlags(const char * str,const char * p,int s,PyCompilerFlags * flags)1682 Py_CompileStringFlags(const char *str, const char *p, int s,
1683                       PyCompilerFlags *flags)
1684 {
1685     return Py_CompileStringExFlags(str, p, s, flags, -1);
1686 }
1687 
1688 #undef PyRun_InteractiveOne
1689 PyAPI_FUNC(int)
PyRun_InteractiveOne(FILE * f,const char * p)1690 PyRun_InteractiveOne(FILE *f, const char *p)
1691 {
1692     return PyRun_InteractiveOneFlags(f, p, NULL);
1693 }
1694 
1695 #undef PyRun_InteractiveLoop
1696 PyAPI_FUNC(int)
PyRun_InteractiveLoop(FILE * f,const char * p)1697 PyRun_InteractiveLoop(FILE *f, const char *p)
1698 {
1699     return PyRun_InteractiveLoopFlags(f, p, NULL);
1700 }
1701