From 467ab194fc6189d9f7310c89937c51abeac56839 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 21 Oct 2016 17:09:17 +0300 Subject: 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] --- Objects/genobject.c | 32 ++------------------------------ 1 file changed, 2 insertions(+), 30 deletions(-) (limited to 'Objects/genobject.c') diff --git a/Objects/genobject.c b/Objects/genobject.c index 7a1e9fd6ab..7bcf016c34 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -118,33 +118,6 @@ gen_dealloc(PyGenObject *gen) PyObject_GC_Del(gen); } -static void -gen_chain_runtime_error(const char *msg) -{ - PyObject *exc, *val, *val2, *tb; - - /* TODO: This about rewriting using _PyErr_ChainExceptions. */ - - PyErr_Fetch(&exc, &val, &tb); - PyErr_NormalizeException(&exc, &val, &tb); - if (tb != NULL) { - PyException_SetTraceback(val, tb); - } - - Py_DECREF(exc); - Py_XDECREF(tb); - - PyErr_SetString(PyExc_RuntimeError, msg); - 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); -} - static PyObject * gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing) { @@ -276,8 +249,7 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing) else if PyAsyncGen_CheckExact(gen) { msg = "async generator raised StopIteration"; } - /* Raise a RuntimeError */ - gen_chain_runtime_error(msg); + _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg); } else { /* `gen` is an ordinary generator without @@ -309,7 +281,7 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing) raise a RuntimeError. */ const char *msg = "async generator raised StopAsyncIteration"; - gen_chain_runtime_error(msg); + _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg); } if (!result || f->f_stacktop == NULL) { -- cgit v1.2.1