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