summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-10-21 14:10:42 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-10-21 14:10:42 (GMT)
commit14ab2776321525ac82d0ae170a5e1ab911c914c0 (patch)
treea740811e0c4169a523cf9c878927f72c3283d646 /Python
parentc4189a04a8d5fef512db33302f405c9e6170c369 (diff)
parent467ab194fc6189d9f7310c89937c51abeac56839 (diff)
downloadcpython-14ab2776321525ac82d0ae170a5e1ab911c914c0.zip
cpython-14ab2776321525ac82d0ae170a5e1ab911c914c0.tar.gz
cpython-14ab2776321525ac82d0ae170a5e1ab911c914c0.tar.bz2
Issue #28410: Added _PyErr_FormatFromCause() -- the helper for raising
new exception with setting current exception as __cause__. _PyErr_FormatFromCause(exception, format, args...) is equivalent to Python raise exception(format % args) from sys.exc_info()[1]
Diffstat (limited to 'Python')
-rw-r--r--Python/errors.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/Python/errors.c b/Python/errors.c
index 12bde28..918f4df 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -401,6 +401,47 @@ _PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb)
}
}
+static PyObject *
+_PyErr_FormatVFromCause(PyObject *exception, const char *format, va_list vargs)
+{
+ PyObject *exc, *val, *val2, *tb;
+
+ assert(PyErr_Occurred());
+ PyErr_Fetch(&exc, &val, &tb);
+ PyErr_NormalizeException(&exc, &val, &tb);
+ if (tb != NULL) {
+ PyException_SetTraceback(val, tb);
+ Py_DECREF(tb);
+ }
+ Py_DECREF(exc);
+ assert(!PyErr_Occurred());
+
+ PyErr_FormatV(exception, format, vargs);
+
+ PyErr_Fetch(&exc, &val2, &tb);
+ PyErr_NormalizeException(&exc, &val2, &tb);
+ Py_INCREF(val);
+ PyException_SetCause(val2, val);
+ PyException_SetContext(val2, val);
+ PyErr_Restore(exc, val2, tb);
+
+ return NULL;
+}
+
+PyObject *
+_PyErr_FormatFromCause(PyObject *exception, const char *format, ...)
+{
+ va_list vargs;
+#ifdef HAVE_STDARG_PROTOTYPES
+ va_start(vargs, format);
+#else
+ va_start(vargs);
+#endif
+ _PyErr_FormatVFromCause(exception, format, vargs);
+ va_end(vargs);
+ return NULL;
+}
+
/* Convenience functions to set a type error exception and return 0 */
int