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