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/Objects/exceptions.c at 3.12 · python/cpython Expand file tree/ Copy pathexceptions.cMore file actions 3862 lines (3341 loc) · 108 KB / Copy pathexceptions.cFile metadata and controls3862 lines (3341 loc) · 108 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 /* * New exceptions.c written in Iceland by Richard Jones and Georg Brandl. * * Thanks go to Tim Peters and Michael Hudson for debugging. */ #define PY_SSIZE_T_CLEAN #include <Python.h> #include <stdbool.h> #include "pycore_ceval.h" // _Py_EnterRecursiveCall #include "pycore_pyerrors.h" // struct _PyErr_SetRaisedException #include "pycore_exceptions.h" // struct _Py_exc_state #include "pycore_initconfig.h" #include "pycore_object.h" #include "structmember.h" // PyMemberDef #include "osdefs.h" // SEP /* Compatibility aliases */ PyObject *PyExc_EnvironmentError = NULL; // borrowed ref PyObject *PyExc_IOError = NULL; // borrowed ref #ifdef MS_WINDOWS PyObject *PyExc_WindowsError = NULL; // borrowed ref #endif static struct _Py_exc_state* get_exc_state(void) { PyInterpreterState *interp = _PyInterpreterState_GET(); return &interp->exc_state; } /* NOTE: If the exception class hierarchy changes, don't forget to update * Lib/test/exception_hierarchy.txt */ /* * BaseException */ static PyObject * BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyBaseExceptionObject *self; self = (PyBaseExceptionObject *)type->tp_alloc(type, 0); if (!self) return NULL; /* the dict is created on the fly in PyObject_GenericSetAttr */ self->dict = NULL; self->notes = NULL; self->traceback = self->cause = self->context = NULL; self->suppress_context = 0; if (args) { self->args = Py_NewRef(args); return (PyObject *)self; } self->args = PyTuple_New(0); if (!self->args) { Py_DECREF(self); return NULL; } return (PyObject *)self; } static int BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds) { if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) return -1; Py_XSETREF(self->args, Py_NewRef(args)); return 0; } static int BaseException_clear(PyBaseExceptionObject *self) { Py_CLEAR(self->dict); Py_CLEAR(self->args); Py_CLEAR(self->notes); Py_CLEAR(self->traceback); Py_CLEAR(self->cause); Py_CLEAR(self->context); return 0; } static void BaseException_dealloc(PyBaseExceptionObject *self) { PyObject_GC_UnTrack(self); // bpo-44348: The trashcan mechanism prevents stack overflow when deleting // long chains of exceptions. For example, exceptions can be chained // through the __context__ attributes or the __traceback__ attribute. Py_TRASHCAN_BEGIN(self, BaseException_dealloc) BaseException_clear(self); Py_TYPE(self)->tp_free((PyObject *)self); Py_TRASHCAN_END } static int BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg) { Py_VISIT(self->dict); Py_VISIT(self->args); Py_VISIT(self->notes); Py_VISIT(self->traceback); Py_VISIT(self->cause); Py_VISIT(self->context); return 0; } static PyObject * BaseException_str(PyBaseExceptionObject *self) { switch (PyTuple_GET_SIZE(self->args)) { case 0: return PyUnicode_FromString(""); case 1: return PyObject_Str(PyTuple_GET_ITEM(self->args, 0)); default: return PyObject_Str(self->args); } } static PyObject * BaseException_repr(PyBaseExceptionObject *self) { const char *name = _PyType_Name(Py_TYPE(self)); if (PyTuple_GET_SIZE(self->args) == 1) return PyUnicode_FromFormat("%s(%R)", name, PyTuple_GET_ITEM(self->args, 0)); else return PyUnicode_FromFormat("%s%R", name, self->args); } /* Pickling support */ static PyObject * BaseException_reduce(PyBaseExceptionObject *self, PyObject *Py_UNUSED(ignored)) { if (self->args && self->dict) return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict); else return PyTuple_Pack(2, Py_TYPE(self), self->args); } /* * Needed for backward compatibility, since exceptions used to store * all their attributes in the __dict__. Code is taken from cPickle's * load_build function. */ static PyObject * BaseException_setstate(PyObject *self, PyObject *state) { PyObject *d_key, *d_value; Py_ssize_t i = 0; if (state != Py_None) { if (!PyDict_Check(state)) { PyErr_SetString(PyExc_TypeError, "state is not a dictionary"); return NULL; } while (PyDict_Next(state, &i, &d_key, &d_value)) { Py_INCREF(d_key); Py_INCREF(d_value); int res = PyObject_SetAttr(self, d_key, d_value); Py_DECREF(d_value); Py_DECREF(d_key); if (res < 0) { return NULL; } } } Py_RETURN_NONE; } static PyObject * BaseException_with_traceback(PyObject *self, PyObject *tb) { if (PyException_SetTraceback(self, tb)) return NULL; return Py_NewRef(self); } PyDoc_STRVAR(with_traceback_doc, "Exception.with_traceback(tb) --\n\ set self.__traceback__ to tb and return self."); static inline PyBaseExceptionObject* _PyBaseExceptionObject_cast(PyObject *exc) { assert(PyExceptionInstance_Check(exc)); return (PyBaseExceptionObject *)exc; } static PyObject * BaseException_add_note(PyObject *self, PyObject *note) { if (!PyUnicode_Check(note)) { PyErr_Format(PyExc_TypeError, "note must be a str, not '%s'", Py_TYPE(note)->tp_name); return NULL; } PyObject *notes; if (_PyObject_LookupAttr(self, &_Py_ID(__notes__), ¬es) < 0) { return NULL; } if (notes == NULL) { notes = PyList_New(0); if (notes == NULL) { return NULL; } if (PyObject_SetAttr(self, &_Py_ID(__notes__), notes) < 0) { Py_DECREF(notes); return NULL; } } else if (!PyList_Check(notes)) { Py_DECREF(notes); PyErr_SetString(PyExc_TypeError, "Cannot add note: __notes__ is not a list"); return NULL; } if (PyList_Append(notes, note) < 0) { Py_DECREF(notes); return NULL; } Py_DECREF(notes); Py_RETURN_NONE; } PyDoc_STRVAR(add_note_doc, "Exception.add_note(note) --\n\ add a note to the exception"); static PyMethodDef BaseException_methods[] = { {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS }, {"__setstate__", (PyCFunction)BaseException_setstate, METH_O }, {"with_traceback", (PyCFunction)BaseException_with_traceback, METH_O, with_traceback_doc}, {"add_note", (PyCFunction)BaseException_add_note, METH_O, add_note_doc}, {NULL, NULL, 0, NULL}, }; static PyObject * BaseException_get_args(PyBaseExceptionObject *self, void *Py_UNUSED(ignored)) { if (self->args == NULL) { Py_RETURN_NONE; } return Py_NewRef(self->args); } static int BaseException_set_args(PyBaseExceptionObject *self, PyObject *val, void *Py_UNUSED(ignored)) { PyObject *seq; if (val == NULL) { PyErr_SetString(PyExc_TypeError, "args may not be deleted"); return -1; } seq = PySequence_Tuple(val); if (!seq) return -1; Py_XSETREF(self->args, seq); return 0; } static PyObject * BaseException_get_tb(PyBaseExceptionObject *self, void *Py_UNUSED(ignored)) { if (self->traceback == NULL) { Py_RETURN_NONE; } return Py_NewRef(self->traceback); } static int BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb, void *Py_UNUSED(ignored)) { if (tb == NULL) { PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted"); return -1; } if (PyTraceBack_Check(tb)) { Py_XSETREF(self->traceback, Py_NewRef(tb)); } else if (tb == Py_None) { Py_CLEAR(self->traceback); } else { PyErr_SetString(PyExc_TypeError, "__traceback__ must be a traceback or None"); return -1; } return 0; } static PyObject * BaseException_get_context(PyObject *self, void *Py_UNUSED(ignored)) { PyObject *res = PyException_GetContext(self); if (res) return res; /* new reference already returned above */ Py_RETURN_NONE; } static int BaseException_set_context(PyObject *self, PyObject *arg, void *Py_UNUSED(ignored)) { if (arg == NULL) { PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted"); return -1; } else if (arg == Py_None) { arg = NULL; } else if (!PyExceptionInstance_Check(arg)) { PyErr_SetString(PyExc_TypeError, "exception context must be None " "or derive from BaseException"); return -1; } else { /* PyException_SetContext steals this reference */ Py_INCREF(arg); } PyException_SetContext(self, arg); return 0; } static PyObject * BaseException_get_cause(PyObject *self, void *Py_UNUSED(ignored)) { PyObject *res = PyException_GetCause(self); if (res) return res; /* new reference already returned above */ Py_RETURN_NONE; } static int BaseException_set_cause(PyObject *self, PyObject *arg, void *Py_UNUSED(ignored)) { if (arg == NULL) { PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted"); return -1; } else if (arg == Py_None) { arg = NULL; } else if (!PyExceptionInstance_Check(arg)) { PyErr_SetString(PyExc_TypeError, "exception cause must be None " "or derive from BaseException"); return -1; } else { /* PyException_SetCause steals this reference */ Py_INCREF(arg); } PyException_SetCause(self, arg); return 0; } static PyGetSetDef BaseException_getset[] = { {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, {"args", (getter)BaseException_get_args, (setter)BaseException_set_args}, {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb}, {"__context__", BaseException_get_context, BaseException_set_context, PyDoc_STR("exception context")}, {"__cause__", BaseException_get_cause, BaseException_set_cause, PyDoc_STR("exception cause")}, {NULL}, }; PyObject * PyException_GetTraceback(PyObject *self) { PyBaseExceptionObject *base_self = _PyBaseExceptionObject_cast(self); return Py_XNewRef(base_self->traceback); } int PyException_SetTraceback(PyObject *self, PyObject *tb) { return BaseException_set_tb(_PyBaseExceptionObject_cast(self), tb, NULL); } PyObject * PyException_GetCause(PyObject *self) { PyObject *cause = _PyBaseExceptionObject_cast(self)->cause; return Py_XNewRef(cause); } /* Steals a reference to cause */ void PyException_SetCause(PyObject *self, PyObject *cause) { PyBaseExceptionObject *base_self = _PyBaseExceptionObject_cast(self); base_self->suppress_context = 1; Py_XSETREF(base_self->cause, cause); } PyObject * PyException_GetContext(PyObject *self) { PyObject *context = _PyBaseExceptionObject_cast(self)->context; return Py_XNewRef(context); } /* Steals a reference to context */ void PyException_SetContext(PyObject *self, PyObject *context) { Py_XSETREF(_PyBaseExceptionObject_cast(self)->context, context); } PyObject * PyException_GetArgs(PyObject *self) { PyObject *args = _PyBaseExceptionObject_cast(self)->args; return Py_NewRef(args); } void PyException_SetArgs(PyObject *self, PyObject *args) { Py_INCREF(args); Py_XSETREF(_PyBaseExceptionObject_cast(self)->args, args); } const char * PyExceptionClass_Name(PyObject *ob) { assert(PyExceptionClass_Check(ob)); return ((PyTypeObject*)ob)->tp_name; } static struct PyMemberDef BaseException_members[] = { {"__suppress_context__", T_BOOL, offsetof(PyBaseExceptionObject, suppress_context)}, {NULL} }; static PyTypeObject _PyExc_BaseException = { PyVarObject_HEAD_INIT(NULL, 0) "BaseException", /*tp_name*/ sizeof(PyBaseExceptionObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ (destructor)BaseException_dealloc, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_as_async*/ (reprfunc)BaseException_repr, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash */ 0, /*tp_call*/ (reprfunc)BaseException_str, /*tp_str*/ PyObject_GenericGetAttr, /*tp_getattro*/ PyObject_GenericSetAttr, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/ PyDoc_STR("Common base class for all exceptions"), /* tp_doc */ (traverseproc)BaseException_traverse, /* tp_traverse */ (inquiry)BaseException_clear, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ BaseException_methods, /* tp_methods */ BaseException_members, /* tp_members */ BaseException_getset, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */ (initproc)BaseException_init, /* tp_init */ 0, /* tp_alloc */ BaseException_new, /* tp_new */ }; /* the CPython API expects exceptions to be (PyObject *) - both a hold-over from the previous implementation and also allowing Python objects to be used in the API */ PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException; /* note these macros omit the last semicolon so the macro invocation may * include it and not look strange. */ #define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \ static PyTypeObject _PyExc_ ## EXCNAME = { \ PyVarObject_HEAD_INIT(NULL, 0) \ # EXCNAME, \ sizeof(PyBaseExceptionObject), \ 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, \ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \ (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \ 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \ (initproc)BaseException_init, 0, BaseException_new,\ }; \ PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME #define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \ static PyTypeObject _PyExc_ ## EXCNAME = { \ PyVarObject_HEAD_INIT(NULL, 0) \ # EXCNAME, \ sizeof(Py ## EXCSTORE ## Object), \ 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, \ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \ (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \ 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \ (initproc)EXCSTORE ## _init, 0, 0, \ }; \ PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME #define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCNEW, \ EXCMETHODS, EXCMEMBERS, EXCGETSET, \ EXCSTR, EXCDOC) \ static PyTypeObject _PyExc_ ## EXCNAME = { \ PyVarObject_HEAD_INIT(NULL, 0) \ # EXCNAME, \ sizeof(Py ## EXCSTORE ## Object), 0, \ (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ (reprfunc)EXCSTR, 0, 0, 0, \ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \ (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \ EXCMEMBERS, EXCGETSET, &_ ## EXCBASE, \ 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \ (initproc)EXCSTORE ## _init, 0, EXCNEW,\ }; \ PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME /* * Exception extends BaseException */ SimpleExtendsException(PyExc_BaseException, Exception, "Common base class for all non-exit exceptions."); /* * TypeError extends Exception */ SimpleExtendsException(PyExc_Exception, TypeError, "Inappropriate argument type."); /* * StopAsyncIteration extends Exception */ SimpleExtendsException(PyExc_Exception, StopAsyncIteration, "Signal the end from iterator.__anext__()."); /* * StopIteration extends Exception */ static PyMemberDef StopIteration_members[] = { {"value", T_OBJECT, offsetof(PyStopIterationObject, value), 0, PyDoc_STR("generator return value")}, {NULL} /* Sentinel */ }; static int StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds) { Py_ssize_t size = PyTuple_GET_SIZE(args); PyObject *value; if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) return -1; Py_CLEAR(self->value); if (size > 0) value = PyTuple_GET_ITEM(args, 0); else value = Py_None; self->value = Py_NewRef(value); return 0; } static int StopIteration_clear(PyStopIterationObject *self) { Py_CLEAR(self->value); return BaseException_clear((PyBaseExceptionObject *)self); } static void StopIteration_dealloc(PyStopIterationObject *self) { PyObject_GC_UnTrack(self); StopIteration_clear(self); Py_TYPE(self)->tp_free((PyObject *)self); } static int StopIteration_traverse(PyStopIterationObject *self, visitproc visit, void *arg) { Py_VISIT(self->value); return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); } ComplexExtendsException(PyExc_Exception, StopIteration, StopIteration, 0, 0, StopIteration_members, 0, 0, "Signal the end from iterator.__next__()."); /* * GeneratorExit extends BaseException */ SimpleExtendsException(PyExc_BaseException, GeneratorExit, "Request that a generator exit."); /* * SystemExit extends BaseException */ static int SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds) { Py_ssize_t size = PyTuple_GET_SIZE(args); if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) return -1; if (size == 0) return 0; if (size == 1) { Py_XSETREF(self->code, Py_NewRef(PyTuple_GET_ITEM(args, 0))); } else { /* size > 1 */ Py_XSETREF(self->code, Py_NewRef(args)); } return 0; } static int SystemExit_clear(PySystemExitObject *self) { Py_CLEAR(self->code); return BaseException_clear((PyBaseExceptionObject *)self); } static void SystemExit_dealloc(PySystemExitObject *self) { _PyObject_GC_UNTRACK(self); SystemExit_clear(self); Py_TYPE(self)->tp_free((PyObject *)self); } static int SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg) { Py_VISIT(self->code); return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); } static PyMemberDef SystemExit_members[] = { {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0, PyDoc_STR("exception code")}, {NULL} /* Sentinel */ }; ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit, 0, 0, SystemExit_members, 0, 0, "Request to exit from the interpreter."); /* * BaseExceptionGroup extends BaseException * ExceptionGroup extends BaseExceptionGroup and Exception */ static inline PyBaseExceptionGroupObject* _PyBaseExceptionGroupObject_cast(PyObject *exc) { assert(_PyBaseExceptionGroup_Check(exc)); return (PyBaseExceptionGroupObject *)exc; } static PyObject * BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { struct _Py_exc_state *state = get_exc_state(); PyTypeObject *PyExc_ExceptionGroup = (PyTypeObject*)state->PyExc_ExceptionGroup; PyObject *message = NULL; PyObject *exceptions = NULL; if (!PyArg_ParseTuple(args, "UO:BaseExceptionGroup.__new__", &message, &exceptions)) { return NULL; } if (!PySequence_Check(exceptions)) { PyErr_SetString( PyExc_TypeError, "second argument (exceptions) must be a sequence"); return NULL; } exceptions = PySequence_Tuple(exceptions); if (!exceptions) { return NULL; } /* We are now holding a ref to the exceptions tuple */ Py_ssize_t numexcs = PyTuple_GET_SIZE(exceptions); if (numexcs == 0) { PyErr_SetString( PyExc_ValueError, "second argument (exceptions) must be a non-empty sequence"); goto error; } bool nested_base_exceptions = false; for (Py_ssize_t i = 0; i < numexcs; i++) { PyObject *exc = PyTuple_GET_ITEM(exceptions, i); if (!exc) { goto error; } if (!PyExceptionInstance_Check(exc)) { PyErr_Format( PyExc_ValueError, "Item %d of second argument (exceptions) is not an exception", i); goto error; } int is_nonbase_exception = PyObject_IsInstance(exc, PyExc_Exception); if (is_nonbase_exception < 0) { goto error; } else if (is_nonbase_exception == 0) { nested_base_exceptions = true; } } PyTypeObject *cls = type; if (cls == PyExc_ExceptionGroup) { if (nested_base_exceptions) { PyErr_SetString(PyExc_TypeError, "Cannot nest BaseExceptions in an ExceptionGroup"); goto error; } } else if (cls == (PyTypeObject*)PyExc_BaseExceptionGroup) { if (!nested_base_exceptions) { /* All nested exceptions are Exception subclasses, * wrap them in an ExceptionGroup */ cls = PyExc_ExceptionGroup; } } else { /* user-defined subclass */ if (nested_base_exceptions) { int nonbase = PyObject_IsSubclass((PyObject*)cls, PyExc_Exception); if (nonbase == -1) { goto error; } else if (nonbase == 1) { PyErr_Format(PyExc_TypeError, "Cannot nest BaseExceptions in '%.200s'", cls->tp_name); goto error; } } } if (!cls) { /* Don't crash during interpreter shutdown * (PyExc_ExceptionGroup may have been cleared) */ cls = (PyTypeObject*)PyExc_BaseExceptionGroup; } PyBaseExceptionGroupObject *self = _PyBaseExceptionGroupObject_cast(BaseException_new(cls, args, kwds)); if (!self) { goto error; } self->msg = Py_NewRef(message); self->excs = exceptions; return (PyObject*)self; error: Py_DECREF(exceptions); return NULL; } PyObject * _PyExc_CreateExceptionGroup(const char *msg_str, PyObject *excs) { PyObject *msg = PyUnicode_FromString(msg_str); if (!msg) { return NULL; } PyObject *args = PyTuple_Pack(2, msg, excs); Py_DECREF(msg); if (!args) { return NULL; } PyObject *result = PyObject_CallObject(PyExc_BaseExceptionGroup, args); Py_DECREF(args); return result; } static int BaseExceptionGroup_init(PyBaseExceptionGroupObject *self, PyObject *args, PyObject *kwds) { if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) { return -1; } if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) { return -1; } return 0; } static int BaseExceptionGroup_clear(PyBaseExceptionGroupObject *self) { Py_CLEAR(self->msg); Py_CLEAR(self->excs); return BaseException_clear((PyBaseExceptionObject *)self); } static void BaseExceptionGroup_dealloc(PyBaseExceptionGroupObject *self) { _PyObject_GC_UNTRACK(self); BaseExceptionGroup_clear(self); Py_TYPE(self)->tp_free((PyObject *)self); } static int BaseExceptionGroup_traverse(PyBaseExceptionGroupObject *self, visitproc visit, void *arg) { Py_VISIT(self->msg); Py_VISIT(self->excs); return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); } static PyObject * BaseExceptionGroup_str(PyBaseExceptionGroupObject *self) { assert(self->msg); assert(PyUnicode_Check(self->msg)); assert(PyTuple_CheckExact(self->excs)); Py_ssize_t num_excs = PyTuple_Size(self->excs); return PyUnicode_FromFormat( "%S (%zd sub-exception%s)", self->msg, num_excs, num_excs > 1 ? "s" : ""); } static PyObject * BaseExceptionGroup_derive(PyObject *self_, PyObject *args) { PyBaseExceptionGroupObject *self = _PyBaseExceptionGroupObject_cast(self_); PyObject *excs = NULL; if (!PyArg_ParseTuple(args, "O", &excs)) { return NULL; } PyObject *init_args = PyTuple_Pack(2, self->msg, excs); if (!init_args) { return NULL; } PyObject *eg = PyObject_CallObject( PyExc_BaseExceptionGroup, init_args); Py_DECREF(init_args); return eg; } static int exceptiongroup_subset( PyBaseExceptionGroupObject *_orig, PyObject *excs, PyObject **result) { /* Sets *result to an ExceptionGroup wrapping excs with metadata from * _orig. If excs is empty, sets *result to NULL. * Returns 0 on success and -1 on error. * This function is used by split() to construct the match/rest parts, * so excs is the matching or non-matching sub-sequence of orig->excs * (this function does not verify that it is a subsequence). */ PyObject *orig = (PyObject *)_orig; *result = NULL; Py_ssize_t num_excs = PySequence_Size(excs); if (num_excs < 0) { return -1; } else if (num_excs == 0) { return 0; } PyObject *eg = PyObject_CallMethod( orig, "derive", "(O)", excs); if (!eg) { return -1; } if (!_PyBaseExceptionGroup_Check(eg)) { PyErr_SetString(PyExc_TypeError, "derive must return an instance of BaseExceptionGroup"); goto error; } /* Now we hold a reference to the new eg */ PyObject *tb = PyException_GetTraceback(orig); if (tb) { int res = PyException_SetTraceback(eg, tb); Py_DECREF(tb); if (res < 0) { goto error; } } PyException_SetContext(eg, PyException_GetContext(orig)); PyException_SetCause(eg, PyException_GetCause(orig)); PyObject *notes; if (_PyObject_LookupAttr(orig, &_Py_ID(__notes__), ¬es) < 0) { goto error; } if (notes) { if (PySequence_Check(notes)) { /* Make a copy so the parts have independent notes lists. */ PyObject *notes_copy = PySequence_List(notes); Py_DECREF(notes); if (notes_copy == NULL) { goto error; } int res = PyObject_SetAttr(eg, &_Py_ID(__notes__), notes_copy); Py_DECREF(notes_copy); if (res < 0) { goto error; } } else { /* __notes__ is supposed to be a list, and split() is not a * good place to report earlier user errors, so we just ignore * notes of non-sequence type. */ Py_DECREF(notes); } } *result = eg; return 0; error: Py_DECREF(eg); return -1; } typedef enum { /* Exception type or tuple of thereof */ EXCEPTION_GROUP_MATCH_BY_TYPE = 0, /* A PyFunction returning True for matching exceptions */ EXCEPTION_GROUP_MATCH_BY_PREDICATE = 1, /* A set of the IDs of leaf exceptions to include in the result. * This matcher type is used internally by the interpreter * to construct reraised exceptions. */ EXCEPTION_GROUP_MATCH_INSTANCE_IDS = 2 } _exceptiongroup_split_matcher_type; static int get_matcher_type(PyObject *value, _exceptiongroup_split_matcher_type *type) { assert(value); if (PyFunction_Check(value)) { *type = EXCEPTION_GROUP_MATCH_BY_PREDICATE; return 0; } if (PyExceptionClass_Check(value)) { |
| 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. |