diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-10-21 14:09:17 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-10-21 14:09:17 (GMT) |
commit | 467ab194fc6189d9f7310c89937c51abeac56839 (patch) | |
tree | ac2397959d646b3656fbf4759f4e6cb9d82e4ae6 /Objects | |
parent | b0426cd8c4ecaa19cff05b4860da1c06531a77c1 (diff) | |
download | cpython-467ab194fc6189d9f7310c89937c51abeac56839.zip cpython-467ab194fc6189d9f7310c89937c51abeac56839.tar.gz cpython-467ab194fc6189d9f7310c89937c51abeac56839.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 'Objects')
-rw-r--r-- | Objects/abstract.c | 22 | ||||
-rw-r--r-- | Objects/genobject.c | 32 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 7 |
3 files changed, 14 insertions, 47 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 747eda0..f9afece 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -2198,20 +2198,18 @@ _Py_CheckFunctionResult(PyObject *func, PyObject *result, const char *where) } else { if (err_occurred) { - PyObject *exc, *val, *tb; - PyErr_Fetch(&exc, &val, &tb); - Py_DECREF(result); - if (func) - PyErr_Format(PyExc_SystemError, - "%R returned a result with an error set", - func); - else - PyErr_Format(PyExc_SystemError, - "%s returned a result with an error set", - where); - _PyErr_ChainExceptions(exc, val, tb); + if (func) { + _PyErr_FormatFromCause(PyExc_SystemError, + "%R returned a result with an error set", + func); + } + else { + _PyErr_FormatFromCause(PyExc_SystemError, + "%s returned a result with an error set", + where); + } #ifdef Py_DEBUG /* Ensure that the bug is caught in debug mode */ Py_FatalError("a function returned a result with an error set"); diff --git a/Objects/genobject.c b/Objects/genobject.c index 7a1e9fd..7bcf016 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) { diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index adeec8c..80e6cf2 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -3835,13 +3835,10 @@ PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) Py_FileSystemDefaultEncodeErrors); #ifdef MS_WINDOWS if (!res && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { - PyObject *exc, *val, *tb; - PyErr_Fetch(&exc, &val, &tb); - PyErr_Format(PyExc_RuntimeError, - "filesystem path bytes were not correctly encoded with '%s'. " \ + _PyErr_FormatFromCause(PyExc_RuntimeError, + "filesystem path bytes were not correctly encoded with '%s'. " "Please report this at http://bugs.python.org/issue27781", Py_FileSystemDefaultEncoding); - _PyErr_ChainExceptions(exc, val, tb); } #endif return res; |