summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-10-08 19:31:52 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-10-08 19:31:52 (GMT)
commite2bd2a718602bf6d6d607fc9a0b7574a18874847 (patch)
tree6c5a51a532610fb6717267f2de9661b80405c6e7 /Python
parent0ddbf4795f40d2d3386dc56ffa264b50a015f6c9 (diff)
downloadcpython-e2bd2a718602bf6d6d607fc9a0b7574a18874847.zip
cpython-e2bd2a718602bf6d6d607fc9a0b7574a18874847.tar.gz
cpython-e2bd2a718602bf6d6d607fc9a0b7574a18874847.tar.bz2
Issue #21715: Extracted shared complicated code in the _io module to new
_PyErr_ChainExceptions() function.
Diffstat (limited to 'Python')
-rw-r--r--Python/errors.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/Python/errors.c b/Python/errors.c
index 996292a..a980481 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -384,6 +384,30 @@ PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback)
Py_XDECREF(oldtraceback);
}
+/* Like PyErr_Restore(), but if an exception is already set,
+ set the context associated with it.
+ */
+void
+_PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb)
+{
+ if (exc == NULL)
+ return;
+
+ if (PyErr_Occurred()) {
+ PyObject *exc2, *val2, *tb2;
+ PyErr_Fetch(&exc2, &val2, &tb2);
+ PyErr_NormalizeException(&exc, &val, &tb);
+ Py_DECREF(exc);
+ Py_XDECREF(tb);
+ PyErr_NormalizeException(&exc2, &val2, &tb2);
+ PyException_SetContext(val2, val);
+ PyErr_Restore(exc2, val2, tb2);
+ }
+ else {
+ PyErr_Restore(exc, val, tb);
+ }
+}
+
/* Convenience functions to set a type error exception and return 0 */
int