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