Mercurial > cpython-lru_cache-weakref
diff 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 |
line wrap: on
line diff
--- a/Python/errors.c Mon Dec 28 08:09:32 2009 +0000 +++ b/Python/errors.c Mon Dec 28 08:34:58 2009 +0000 @@ -604,6 +604,40 @@ return result; } + +/* Create an exception with docstring */ +PyObject * +PyErr_NewExceptionWithDoc(char *name, char *doc, PyObject *base, PyObject *dict) +{ + int result; + PyObject *ret = NULL; + PyObject *mydict = NULL; /* points to the dict only if we create it */ + PyObject *docobj; + + if (dict == NULL) { + dict = mydict = PyDict_New(); + if (dict == NULL) { + return NULL; + } + } + + if (doc != NULL) { + docobj = PyString_FromString(doc); + if (docobj == NULL) + goto failure; + result = PyDict_SetItemString(dict, "__doc__", docobj); + Py_DECREF(docobj); + if (result < 0) + goto failure; + } + + ret = PyErr_NewException(name, base, dict); + failure: + Py_XDECREF(mydict); + return ret; +} + + /* Call when an exception has occurred but there is no way for Python to handle it. Examples: exception in __del__ or during GC. */ void
