Mercurial > cpython-lru_cache-weakref
comparison Python/errors.c @ 57961:cf39c5e462f9 legacy-trunk
#7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Mon, 28 Dec 2009 08:34:58 +0000 |
| parents | 921e569d1215 |
| children | fd25030223b0 |
comparison
equal
deleted
inserted
replaced
| 57959:fe0459dc9889 | 57961:cf39c5e462f9 |
|---|---|
| 602 Py_XDECREF(classname); | 602 Py_XDECREF(classname); |
| 603 Py_XDECREF(modulename); | 603 Py_XDECREF(modulename); |
| 604 return result; | 604 return result; |
| 605 } | 605 } |
| 606 | 606 |
| 607 | |
| 608 /* Create an exception with docstring */ | |
| 609 PyObject * | |
| 610 PyErr_NewExceptionWithDoc(char *name, char *doc, PyObject *base, PyObject *dict) | |
| 611 { | |
| 612 int result; | |
| 613 PyObject *ret = NULL; | |
| 614 PyObject *mydict = NULL; /* points to the dict only if we create it */ | |
| 615 PyObject *docobj; | |
| 616 | |
| 617 if (dict == NULL) { | |
| 618 dict = mydict = PyDict_New(); | |
| 619 if (dict == NULL) { | |
| 620 return NULL; | |
| 621 } | |
| 622 } | |
| 623 | |
| 624 if (doc != NULL) { | |
| 625 docobj = PyString_FromString(doc); | |
| 626 if (docobj == NULL) | |
| 627 goto failure; | |
| 628 result = PyDict_SetItemString(dict, "__doc__", docobj); | |
| 629 Py_DECREF(docobj); | |
| 630 if (result < 0) | |
| 631 goto failure; | |
| 632 } | |
| 633 | |
| 634 ret = PyErr_NewException(name, base, dict); | |
| 635 failure: | |
| 636 Py_XDECREF(mydict); | |
| 637 return ret; | |
| 638 } | |
| 639 | |
| 640 | |
| 607 /* Call when an exception has occurred but there is no way for Python | 641 /* Call when an exception has occurred but there is no way for Python |
| 608 to handle it. Examples: exception in __del__ or during GC. */ | 642 to handle it. Examples: exception in __del__ or during GC. */ |
| 609 void | 643 void |
| 610 PyErr_WriteUnraisable(PyObject *obj) | 644 PyErr_WriteUnraisable(PyObject *obj) |
| 611 { | 645 { |
