diff options
author | Georg Brandl <georg@python.org> | 2009-12-28 08:41:01 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2009-12-28 08:41:01 (GMT) |
commit | 1e28a27f845a59513ad3bebbd6ae19f399071bbd (patch) | |
tree | 22f39f9b43f61a56fbcca9327b968685159e7f00 /Python | |
parent | 127d47092a98aa9b668dceb03c7b64d9c4c44adc (diff) | |
download | cpython-1e28a27f845a59513ad3bebbd6ae19f399071bbd.zip cpython-1e28a27f845a59513ad3bebbd6ae19f399071bbd.tar.gz cpython-1e28a27f845a59513ad3bebbd6ae19f399071bbd.tar.bz2 |
Merged revisions 77088 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r77088 | georg.brandl | 2009-12-28 09:34:58 +0100 (Mo, 28 Dez 2009) | 1 line
#7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
........
Diffstat (limited to 'Python')
-rw-r--r-- | Python/errors.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Python/errors.c b/Python/errors.c index 2169a1a..42e0e6f 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -693,6 +693,41 @@ PyErr_NewException(const char *name, PyObject *base, PyObject *dict) return result; } + +/* Create an exception with docstring */ +PyObject * +PyErr_NewExceptionWithDoc(const char *name, const 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 = PyUnicode_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 |