summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2009-12-28 08:34:58 (GMT)
committerGeorg Brandl <georg@python.org>2009-12-28 08:34:58 (GMT)
commit740cdc3a9f9593ee1fe86d751c70b8505d6a19c9 (patch)
tree4c34f7bec409dfb040cba93c2064b37bb71b2b95 /Python
parent02e7dfde639498064b137be7cb850c4229f0c8fb (diff)
downloadcpython-740cdc3a9f9593ee1fe86d751c70b8505d6a19c9.zip
cpython-740cdc3a9f9593ee1fe86d751c70b8505d6a19c9.tar.gz
cpython-740cdc3a9f9593ee1fe86d751c70b8505d6a19c9.tar.bz2
#7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Diffstat (limited to 'Python')
-rw-r--r--Python/errors.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/Python/errors.c b/Python/errors.c
index 9f040ad..2d47779 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -604,6 +604,40 @@ PyErr_NewException(char *name, PyObject *base, PyObject *dict)
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