GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cpython/Python/errors.c at main · python/cpython Expand file tree/ Copy patherrors.cMore file actions 2101 lines (1848 loc) · 57.8 KB / Copy patherrors.cFile metadata and controls2101 lines (1848 loc) · 57.8 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /* Error handling */ #include "Python.h" #include "pycore_audit.h" // _PySys_Audit() #include "pycore_call.h" // _PyObject_CallNoArgs() #include "pycore_ceval.h" // _Py_ReachedRecursionLimitWithMargin() #include "pycore_fileutils.h" // _PyFile_Flush #include "pycore_initconfig.h" // _PyStatus_ERR() #include "pycore_pyerrors.h" // _PyErr_Format() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_runtime.h" // _Py_ID() #include "pycore_structseq.h" // _PyStructSequence_FiniBuiltin() #include "pycore_traceback.h" // _PyTraceBack_FromFrame() #include "pycore_unicodeobject.h" // _PyUnicode_Equal() #include "../Parser/tokenizer/tokenizer.h" #ifdef MS_WINDOWS # include <windows.h> # include <winbase.h> # include <stdlib.h> // _sys_nerr #endif void _PyErr_SetRaisedException(PyThreadState *tstate, PyObject *exc) { PyObject *old_exc = tstate->current_exception; tstate->current_exception = exc; Py_XDECREF(old_exc); } static PyObject* _PyErr_CreateException(PyObject *exception_type, PyObject *value) { PyObject *exc; if (value == NULL || value == Py_None) { exc = _PyObject_CallNoArgs(exception_type); } else if (PyTuple_Check(value)) { exc = PyObject_Call(exception_type, value, NULL); } else { exc = PyObject_CallOneArg(exception_type, value); } if (exc != NULL && !PyExceptionInstance_Check(exc)) { PyErr_Format(PyExc_TypeError, "calling %R should have returned an instance of " "BaseException, not %s", exception_type, Py_TYPE(exc)->tp_name); Py_CLEAR(exc); } return exc; } void _PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *traceback) { if (type == NULL) { assert(value == NULL); assert(traceback == NULL); _PyErr_SetRaisedException(tstate, NULL); return; } assert(PyExceptionClass_Check(type)); if (value != NULL && type == (PyObject *)Py_TYPE(value)) { /* Already normalized */ #ifdef Py_DEBUG PyObject *tb = PyException_GetTraceback(value); assert(tb != Py_None); Py_XDECREF(tb); #endif } else { PyObject *exc = _PyErr_CreateException(type, value); Py_XDECREF(value); if (exc == NULL) { Py_DECREF(type); Py_XDECREF(traceback); return; } value = exc; } assert(PyExceptionInstance_Check(value)); if (traceback != NULL) { if (PyException_SetTraceback(value, traceback) < 0) { Py_DECREF(traceback); Py_DECREF(value); Py_DECREF(type); return; } Py_DECREF(traceback); } _PyErr_SetRaisedException(tstate, value); Py_DECREF(type); } void PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback) { PyThreadState *tstate = _PyThreadState_GET(); _PyErr_Restore(tstate, type, value, traceback); } void PyErr_SetRaisedException(PyObject *exc) { PyThreadState *tstate = _PyThreadState_GET(); _PyErr_SetRaisedException(tstate, exc); } _PyErr_StackItem * _PyErr_GetTopmostException(PyThreadState *tstate) { _PyErr_StackItem *exc_info = tstate->exc_info; assert(exc_info); while (exc_info->exc_value == NULL && exc_info->previous_item != NULL) { exc_info = exc_info->previous_item; } assert(!Py_IsNone(exc_info->exc_value)); return exc_info; } static PyObject * get_normalization_failure_note(PyThreadState *tstate, PyObject *exception, PyObject *value) { PyObject *args = PyObject_Repr(value); if (args == NULL) { _PyErr_Clear(tstate); args = PyUnicode_FromFormat("<unknown>"); } PyObject *note; const char *tpname = ((PyTypeObject*)exception)->tp_name; if (args == NULL) { _PyErr_Clear(tstate); note = PyUnicode_FromFormat("Normalization failed: type=%s", tpname); } else { note = PyUnicode_FromFormat("Normalization failed: type=%s args=%S", tpname, args); Py_DECREF(args); } return note; } void _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value) { PyObject *exc_value; if (exception != NULL && !PyExceptionClass_Check(exception)) { _PyErr_Format(tstate, PyExc_SystemError, "_PyErr_SetObject: " "exception %R is not a BaseException subclass", exception); return; } /* Normalize the exception */ int is_subclass = 0; if (value != NULL && PyExceptionInstance_Check(value)) { is_subclass = PyObject_IsSubclass((PyObject *)Py_TYPE(value), exception); if (is_subclass < 0) { return; } } Py_XINCREF(value); if (!is_subclass) { /* We must normalize the value right now */ /* Issue #23571: functions must not be called with an exception set */ _PyErr_Clear(tstate); PyObject *fixed_value = _PyErr_CreateException(exception, value); if (fixed_value == NULL) { PyObject *exc = _PyErr_GetRaisedException(tstate); assert(PyExceptionInstance_Check(exc)); PyObject *note = get_normalization_failure_note(tstate, exception, value); Py_XDECREF(value); if (note != NULL) { /* ignore errors in _PyException_AddNote - they will be overwritten below */ _PyException_AddNote(exc, note); Py_DECREF(note); } _PyErr_SetRaisedException(tstate, exc); return; } Py_XSETREF(value, fixed_value); } exc_value = _PyErr_GetTopmostException(tstate)->exc_value; if (exc_value != NULL && exc_value != Py_None) { /* Implicit exception chaining */ Py_INCREF(exc_value); /* Avoid creating new reference cycles through the context chain, while taking care not to hang on pre-existing ones. This is O(chain length) but context chains are usually very short. Sensitive readers may try to inline the call to PyException_GetContext. */ if (exc_value != value) { PyObject *o = exc_value, *context; PyObject *slow_o = o; /* Floyd's cycle detection algo */ int slow_update_toggle = 0; while ((context = PyException_GetContext(o))) { Py_DECREF(context); if (context == value) { PyException_SetContext(o, NULL); break; } o = context; if (o == slow_o) { /* pre-existing cycle - all exceptions on the path were visited and checked. */ break; } if (slow_update_toggle) { slow_o = PyException_GetContext(slow_o); Py_DECREF(slow_o); } slow_update_toggle = !slow_update_toggle; } PyException_SetContext(value, exc_value); } else { Py_DECREF(exc_value); } } assert(value != NULL); assert(PyExceptionInstance_Check(value)); _PyErr_SetRaisedException(tstate, value); } void PyErr_SetObject(PyObject *exception, PyObject *value) { PyThreadState *tstate = _PyThreadState_GET(); _PyErr_SetObject(tstate, exception, value); } /* Set a key error with the specified argument. This function should be used to * raise a KeyError with an argument instead of PyErr_SetObject(PyExc_KeyError, * arg) which has a special behavior. PyErr_SetObject() unpacks arg if it's a * tuple, and it uses arg instead of creating a new exception if arg is an * exception. * * If an exception is already set, override the exception. */ void _PyErr_SetKeyError(PyObject *arg) { PyThreadState *tstate = _PyThreadState_GET(); // PyObject_CallOneArg() must not be called with an exception set, // otherwise _Py_CheckFunctionResult() can fail if the function returned // a result with an excception set. _PyErr_Clear(tstate); PyObject *exc = PyObject_CallOneArg(PyExc_KeyError, arg); if (!exc) { /* caller will expect error to be set anyway */ return; } _PyErr_SetObject(tstate, (PyObject*)Py_TYPE(exc), exc); Py_DECREF(exc); } void _PyErr_SetNone(PyThreadState *tstate, PyObject *exception) { _PyErr_SetObject(tstate, exception, (PyObject *)NULL); } void PyErr_SetNone(PyObject *exception) { PyThreadState *tstate = _PyThreadState_GET(); _PyErr_SetNone(tstate, exception); } void _PyErr_SetString(PyThreadState *tstate, PyObject *exception, const char *string) { PyObject *value = PyUnicode_FromString(string); if (value != NULL) { _PyErr_SetObject(tstate, exception, value); Py_DECREF(value); } } void PyErr_SetString(PyObject *exception, const char *string) { PyThreadState *tstate = _PyThreadState_GET(); _PyErr_SetString(tstate, exception, string); } void _PyErr_SetLocaleString(PyObject *exception, const char *string) { PyObject *value = PyUnicode_DecodeLocale(string, "surrogateescape"); if (value != NULL) { PyErr_SetObject(exception, value); Py_DECREF(value); } } PyObject* _Py_HOT_FUNCTION PyErr_Occurred(void) { /* The caller must hold a thread state. */ _Py_AssertHoldsTstate(); PyThreadState *tstate = _PyThreadState_GET(); return _PyErr_Occurred(tstate); } int PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc) { if (err == NULL || exc == NULL) { /* maybe caused by "import exceptions" that failed early on */ return 0; } if (PyTuple_Check(exc)) { PyThreadState *tstate = _PyThreadState_GET(); if (_Py_ReachedRecursionLimitWithMargin(tstate, 2)) { PyObject *exc_value = _PyErr_GetRaisedException(tstate); _PyErr_SetString(tstate, PyExc_RecursionError, "maximum recursion depth exceeded while " "checking exception tuple"); PyErr_FormatUnraisable("Exception ignored while " "checking exception tuple"); _PyErr_SetRaisedException(tstate, exc_value); return 0; } int res = 0; Py_ssize_t n = PyTuple_GET_SIZE(exc); for (Py_ssize_t i = 0; i < n; i++) { /* Test recursively */ if (PyErr_GivenExceptionMatches(err, PyTuple_GET_ITEM(exc, i))) { res = 1; break; } } return res; } /* err might be an instance, so check its class. */ if (PyExceptionInstance_Check(err)) err = PyExceptionInstance_Class(err); if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) { return PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc); } return err == exc; } int _PyErr_ExceptionMatches(PyThreadState *tstate, PyObject *exc) { return PyErr_GivenExceptionMatches(_PyErr_Occurred(tstate), exc); } int PyErr_ExceptionMatches(PyObject *exc) { PyThreadState *tstate = _PyThreadState_GET(); return _PyErr_ExceptionMatches(tstate, exc); } #ifndef Py_NORMALIZE_RECURSION_LIMIT #define Py_NORMALIZE_RECURSION_LIMIT 32 #endif /* Used in many places to normalize a raised exception, including in eval_code2(), do_raise(), and PyErr_Print() XXX: should PyErr_NormalizeException() also call PyException_SetTraceback() with the resulting value and tb? */ void _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc, PyObject **val, PyObject **tb) { int recursion_depth = 0; tstate->recursion_headroom++; PyObject *type, *value, *initial_tb; restart: type = *exc; if (type == NULL) { /* There was no exception, so nothing to do. */ tstate->recursion_headroom--; return; } value = *val; /* If PyErr_SetNone() was used, the value will have been actually set to NULL. */ if (!value) { value = Py_NewRef(Py_None); } /* Normalize the exception so that if the type is a class, the value will be an instance. */ if (PyExceptionClass_Check(type)) { PyObject *inclass = NULL; int is_subclass = 0; if (PyExceptionInstance_Check(value)) { inclass = PyExceptionInstance_Class(value); is_subclass = PyObject_IsSubclass(inclass, type); if (is_subclass < 0) { goto error; } } /* If the value was not an instance, or is not an instance whose class is (or is derived from) type, then use the value as an argument to instantiation of the type class. */ if (!is_subclass) { PyObject *fixed_value = _PyErr_CreateException(type, value); if (fixed_value == NULL) { goto error; } Py_SETREF(value, fixed_value); } /* If the class of the instance doesn't exactly match the class of the type, believe the instance. */ else if (inclass != type) { Py_SETREF(type, Py_NewRef(inclass)); } } *exc = type; *val = value; tstate->recursion_headroom--; return; error: Py_DECREF(type); Py_DECREF(value); recursion_depth++; if (recursion_depth == Py_NORMALIZE_RECURSION_LIMIT) { _PyErr_SetString(tstate, PyExc_RecursionError, "maximum recursion depth exceeded " "while normalizing an exception"); } /* If the new exception doesn't set a traceback and the old exception had a traceback, use the old traceback for the new exception. It's better than nothing. */ initial_tb = *tb; _PyErr_Fetch(tstate, exc, val, tb); assert(*exc != NULL); if (initial_tb != NULL) { if (*tb == NULL) *tb = initial_tb; else Py_DECREF(initial_tb); } /* Abort when Py_NORMALIZE_RECURSION_LIMIT has been exceeded, and the corresponding RecursionError could not be normalized, and the MemoryError raised when normalize this RecursionError could not be normalized. */ if (recursion_depth >= Py_NORMALIZE_RECURSION_LIMIT + 2) { if (PyErr_GivenExceptionMatches(*exc, PyExc_MemoryError)) { Py_FatalError("Cannot recover from MemoryErrors " "while normalizing exceptions."); } else { Py_FatalError("Cannot recover from the recursive normalization " "of an exception."); } } goto restart; } void PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) { PyThreadState *tstate = _PyThreadState_GET(); _PyErr_NormalizeException(tstate, exc, val, tb); } PyObject * _PyErr_GetRaisedException(PyThreadState *tstate) { PyObject *exc = tstate->current_exception; tstate->current_exception = NULL; return exc; } PyObject * PyErr_GetRaisedException(void) { PyThreadState *tstate = _PyThreadState_GET(); return _PyErr_GetRaisedException(tstate); } void _PyErr_Fetch(PyThreadState *tstate, PyObject **p_type, PyObject **p_value, PyObject **p_traceback) { PyObject *exc = _PyErr_GetRaisedException(tstate); *p_value = exc; if (exc == NULL) { *p_type = NULL; *p_traceback = NULL; } else { *p_type = Py_NewRef(Py_TYPE(exc)); *p_traceback = PyException_GetTraceback(exc); } } void PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) { PyThreadState *tstate = _PyThreadState_GET(); _PyErr_Fetch(tstate, p_type, p_value, p_traceback); } void _PyErr_Clear(PyThreadState *tstate) { _PyErr_Restore(tstate, NULL, NULL, NULL); } void PyErr_Clear(void) { PyThreadState *tstate = _PyThreadState_GET(); _PyErr_Clear(tstate); } static PyObject* get_exc_type(PyObject *exc_value) /* returns a strong ref */ { if (exc_value == NULL || exc_value == Py_None) { return Py_None; } else { assert(PyExceptionInstance_Check(exc_value)); PyObject *type = PyExceptionInstance_Class(exc_value); assert(type != NULL); return Py_NewRef(type); } } static PyObject* get_exc_traceback(PyObject *exc_value) /* returns a strong ref */ { if (exc_value == NULL || exc_value == Py_None) { return Py_None; } else { assert(PyExceptionInstance_Check(exc_value)); PyObject *tb = PyException_GetTraceback(exc_value); return tb ? tb : Py_None; } } void _PyErr_GetExcInfo(PyThreadState *tstate, PyObject **p_type, PyObject **p_value, PyObject **p_traceback) { _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); *p_type = get_exc_type(exc_info->exc_value); *p_value = Py_XNewRef(exc_info->exc_value); *p_traceback = get_exc_traceback(exc_info->exc_value); } PyObject* _PyErr_GetHandledException(PyThreadState *tstate) { _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); PyObject *exc = exc_info->exc_value; if (exc == NULL || exc == Py_None) { return NULL; } return Py_NewRef(exc); } PyObject* PyErr_GetHandledException(void) { PyThreadState *tstate = _PyThreadState_GET(); return _PyErr_GetHandledException(tstate); } void _PyErr_SetHandledException(PyThreadState *tstate, PyObject *exc) { Py_XSETREF(tstate->exc_info->exc_value, Py_XNewRef(exc == Py_None ? NULL : exc)); } void PyErr_SetHandledException(PyObject *exc) { PyThreadState *tstate = _PyThreadState_GET(); _PyErr_SetHandledException(tstate, exc); } void PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) { PyThreadState *tstate = _PyThreadState_GET(); _PyErr_GetExcInfo(tstate, p_type, p_value, p_traceback); } void PyErr_SetExcInfo(PyObject *type, PyObject *value, PyObject *traceback) { PyErr_SetHandledException(value); Py_XDECREF(value); /* These args are no longer used, but we still need to steal a ref */ Py_XDECREF(type); Py_XDECREF(traceback); } PyObject* _PyErr_StackItemToExcInfoTuple(_PyErr_StackItem *err_info) { PyObject *exc_value = err_info->exc_value; assert(exc_value == NULL || exc_value == Py_None || PyExceptionInstance_Check(exc_value)); PyObject *ret = PyTuple_New(3); if (ret == NULL) { return NULL; } PyObject *exc_type = get_exc_type(exc_value); PyObject *exc_traceback = get_exc_traceback(exc_value); PyTuple_SET_ITEM(ret, 0, exc_type ? exc_type : Py_None); PyTuple_SET_ITEM(ret, 1, exc_value ? Py_NewRef(exc_value) : Py_None); PyTuple_SET_ITEM(ret, 2, exc_traceback ? exc_traceback : Py_None); return ret; } /* Like PyErr_Restore(), but if an exception is already set, set the context associated with it. The caller is responsible for ensuring that this call won't create any cycles in the exception context chain. */ void _PyErr_ChainExceptions(PyObject *typ, PyObject *val, PyObject *tb) { if (typ == NULL) return; PyThreadState *tstate = _PyThreadState_GET(); if (!PyExceptionClass_Check(typ)) { _PyErr_Format(tstate, PyExc_SystemError, "_PyErr_ChainExceptions: " "exception %R is not a BaseException subclass", typ); return; } if (_PyErr_Occurred(tstate)) { _PyErr_NormalizeException(tstate, &typ, &val, &tb); if (tb != NULL) { PyException_SetTraceback(val, tb); Py_DECREF(tb); } Py_DECREF(typ); PyObject *exc2 = _PyErr_GetRaisedException(tstate); PyException_SetContext(exc2, val); _PyErr_SetRaisedException(tstate, exc2); } else { _PyErr_Restore(tstate, typ, val, tb); } } /* Like PyErr_SetRaisedException(), but if an exception is already set, set the context associated with it. The caller is responsible for ensuring that this call won't create any cycles in the exception context chain. */ void _PyErr_ChainExceptions1Tstate(PyThreadState *tstate, PyObject *exc) { if (exc == NULL) { return; } if (_PyErr_Occurred(tstate)) { PyObject *exc2 = _PyErr_GetRaisedException(tstate); PyException_SetContext(exc2, exc); _PyErr_SetRaisedException(tstate, exc2); } else { _PyErr_SetRaisedException(tstate, exc); } } void _PyErr_ChainExceptions1(PyObject *exc) { PyThreadState *tstate = _PyThreadState_GET(); _PyErr_ChainExceptions1Tstate(tstate, exc); } /* If the current thread is handling an exception (exc_info is ), set this exception as the context of the current raised exception. This function can only be called when _PyErr_Occurred() is true. Also, this function won't create any cycles in the exception context chain to the extent that _PyErr_SetObject ensures this. */ void _PyErr_ChainStackItem(void) { PyThreadState *tstate = _PyThreadState_GET(); assert(_PyErr_Occurred(tstate)); _PyErr_StackItem *exc_info = tstate->exc_info; if (exc_info->exc_value == NULL || exc_info->exc_value == Py_None) { return; } PyObject *exc = _PyErr_GetRaisedException(tstate); /* _PyErr_SetObject sets the context from PyThreadState. */ _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(exc), exc); Py_DECREF(exc); // since _PyErr_Occurred was true } static PyObject * _PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception, const char *format, va_list vargs) { assert(_PyErr_Occurred(tstate)); PyObject *exc = _PyErr_GetRaisedException(tstate); assert(!_PyErr_Occurred(tstate)); _PyErr_FormatV(tstate, exception, format, vargs); PyObject *exc2 = _PyErr_GetRaisedException(tstate); PyException_SetCause(exc2, Py_NewRef(exc)); PyException_SetContext(exc2, Py_NewRef(exc)); Py_DECREF(exc); _PyErr_SetRaisedException(tstate, exc2); return NULL; } PyObject * _PyErr_FormatFromCauseTstate(PyThreadState *tstate, PyObject *exception, const char *format, ...) { va_list vargs; va_start(vargs, format); _PyErr_FormatVFromCause(tstate, exception, format, vargs); va_end(vargs); return NULL; } PyObject * _PyErr_FormatFromCause(PyObject *exception, const char *format, ...) { PyThreadState *tstate = _PyThreadState_GET(); va_list vargs; va_start(vargs, format); _PyErr_FormatVFromCause(tstate, exception, format, vargs); va_end(vargs); return NULL; } /* Convenience functions to set a type error exception and return 0 */ int PyErr_BadArgument(void) { PyThreadState *tstate = _PyThreadState_GET(); _PyErr_SetString(tstate, PyExc_TypeError, "bad argument type for built-in operation"); return 0; } PyObject * PyErr_NoMemory(void) { PyThreadState *tstate = _PyThreadState_GET(); return _PyErr_NoMemory(tstate); } PyObject * PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject) { return PyErr_SetFromErrnoWithFilenameObjects(exc, filenameObject, NULL); } PyObject * PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2) { PyThreadState *tstate = _PyThreadState_GET(); PyObject *message; PyObject *v, *args; int i = errno; #ifdef MS_WINDOWS WCHAR *s_buf = NULL; #endif /* Unix/Windows */ #ifdef EINTR if (i == EINTR && PyErr_CheckSignals()) return NULL; #endif #ifndef MS_WINDOWS if (i != 0) { message = _Py_strerror(i); } else { /* Sometimes errno didn't get set */ message = PyUnicode_FromString("Error"); } #else if (i == 0) message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */ else { /* Note that the Win32 errors do not lineup with the errno error. So if the error is in the MSVC error table, we use it, otherwise we assume it really _is_ a Win32 error code */ if (i > 0 && i < _sys_nerr) { message = PyUnicode_FromString(_sys_errlist[i]); } else { int len = FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, /* no message source */ i, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ (LPWSTR) &s_buf, 0, /* size not used */ NULL); /* no args */ if (len==0) { /* Only ever seen this in out-of-mem situations */ s_buf = NULL; message = PyUnicode_FromFormat("Windows Error 0x%x", i); } else { /* remove trailing cr/lf and dots */ while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) s_buf[--len] = L'\0'; message = PyUnicode_FromWideChar(s_buf, len); } } } #endif /* Unix/Windows */ if (message == NULL) { #ifdef MS_WINDOWS LocalFree(s_buf); #endif return NULL; } if (filenameObject != NULL) { if (filenameObject2 != NULL) args = Py_BuildValue("(iOOiO)", i, message, filenameObject, 0, filenameObject2); else args = Py_BuildValue("(iOO)", i, message, filenameObject); } else { assert(filenameObject2 == NULL); args = Py_BuildValue("(iO)", i, message); } Py_DECREF(message); if (args != NULL) { v = PyObject_Call(exc, args, NULL); Py_DECREF(args); if (v != NULL) { _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v); Py_DECREF(v); } } #ifdef MS_WINDOWS LocalFree(s_buf); #endif return NULL; } PyObject * PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename) { PyObject *name = NULL; if (filename) { int i = errno; name = PyUnicode_DecodeFSDefault(filename); if (name == NULL) { return NULL; } errno = i; } PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL); Py_XDECREF(name); return result; } PyObject * PyErr_SetFromErrno(PyObject *exc) { return PyErr_SetFromErrnoWithFilenameObjects(exc, NULL, NULL); } #ifdef MS_WINDOWS /* Windows specific error code handling */ PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( PyObject *exc, int ierr, PyObject *filenameObject) { return PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, ierr, filenameObject, NULL); } PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects( PyObject *exc, int ierr, PyObject *filenameObject, PyObject *filenameObject2) { PyThreadState *tstate = _PyThreadState_GET(); int len; WCHAR *s_buf = NULL; /* Free via LocalFree */ PyObject *message; PyObject *args, *v; DWORD err = (DWORD)ierr; if (err==0) { err = GetLastError(); } len = FormatMessageW( /* Error API error */ FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, /* no message source */ err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ (LPWSTR) &s_buf, 0, /* size not used */ NULL); /* no args */ if (len==0) { /* Only seen this in out of mem situations */ message = PyUnicode_FromFormat("Windows Error 0x%x", err); s_buf = NULL; } else { /* remove trailing cr/lf and dots */ while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) s_buf[--len] = L'\0'; message = PyUnicode_FromWideChar(s_buf, len); } if (message == NULL) { LocalFree(s_buf); return NULL; |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |