cpython: 9af21752ea2a (original) (raw)
Mercurial > cpython
changeset 92881:9af21752ea2a 3.4
Issue #21715: Extracted shared complicated code in the _io module to new _PyErr_ChainExceptions() function. [#21715]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Wed, 08 Oct 2014 22:31:52 +0300 |
parents | 5433ef907e4f |
children | 8b1ac1a3d007 f0e06514d67f |
files | Include/pyerrors.h Modules/_io/_iomodule.c Modules/_io/bufferedio.c Modules/_io/textio.c Python/errors.c |
diffstat | 5 files changed, 33 insertions(+), 42 deletions(-)[+] [-] Include/pyerrors.h 4 Modules/_io/_iomodule.c 15 Modules/_io/bufferedio.c 16 Modules/_io/textio.c 16 Python/errors.c 24 |
line wrap: on
line diff
--- a/Include/pyerrors.h +++ b/Include/pyerrors.h @@ -123,7 +123,9 @@ PyAPI_FUNC(void) PyException_SetCause(Py /* Context manipulation (PEP 3134) */ PyAPI_FUNC(PyObject *) PyException_GetContext(PyObject *); PyAPI_FUNC(void) PyException_SetContext(PyObject *, PyObject *); - +#ifndef Py_LIMITED_API +PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject ); +#endif / */
--- a/Modules/_io/_iomodule.c +++ b/Modules/_io/_iomodule.c @@ -468,19 +468,8 @@ io_open(PyObject *self, PyObject *args, PyObject *exc, *val, *tb, *close_result; PyErr_Fetch(&exc, &val, &tb); close_result = _PyObject_CallMethodId(result, &PyId_close, NULL);
if (close_result != NULL) {[](#l2.7)
Py_DECREF(close_result);[](#l2.8)
PyErr_Restore(exc, val, tb);[](#l2.9)
} else {[](#l2.10)
PyObject *exc2, *val2, *tb2;[](#l2.11)
PyErr_Fetch(&exc2, &val2, &tb2);[](#l2.12)
PyErr_NormalizeException(&exc, &val, &tb);[](#l2.13)
Py_XDECREF(exc);[](#l2.14)
Py_XDECREF(tb);[](#l2.15)
PyErr_NormalizeException(&exc2, &val2, &tb2);[](#l2.16)
PyException_SetContext(val2, val);[](#l2.17)
PyErr_Restore(exc2, val2, tb2);[](#l2.18)
}[](#l2.19)
_PyErr_ChainExceptions(exc, val, tb);[](#l2.20)
} Py_XDECREF(modeobj);Py_XDECREF(close_result);[](#l2.21) Py_DECREF(result);[](#l2.22)
--- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -543,20 +543,8 @@ buffered_close(buffered *self, PyObject } if (exc != NULL) {
if (res != NULL) {[](#l3.7)
Py_CLEAR(res);[](#l3.8)
PyErr_Restore(exc, val, tb);[](#l3.9)
}[](#l3.10)
else {[](#l3.11)
PyObject *exc2, *val2, *tb2;[](#l3.12)
PyErr_Fetch(&exc2, &val2, &tb2);[](#l3.13)
PyErr_NormalizeException(&exc, &val, &tb);[](#l3.14)
Py_DECREF(exc);[](#l3.15)
Py_XDECREF(tb);[](#l3.16)
PyErr_NormalizeException(&exc2, &val2, &tb2);[](#l3.17)
PyException_SetContext(val2, val);[](#l3.18)
PyErr_Restore(exc2, val2, tb2);[](#l3.19)
}[](#l3.20)
--- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -2608,20 +2608,8 @@ textiowrapper_close(textio *self, PyObje res = _PyObject_CallMethodId(self->buffer, &PyId_close, NULL); if (exc != NULL) {
if (res != NULL) {[](#l4.7)
Py_CLEAR(res);[](#l4.8)
PyErr_Restore(exc, val, tb);[](#l4.9)
}[](#l4.10)
else {[](#l4.11)
PyObject *exc2, *val2, *tb2;[](#l4.12)
PyErr_Fetch(&exc2, &val2, &tb2);[](#l4.13)
PyErr_NormalizeException(&exc, &val, &tb);[](#l4.14)
Py_DECREF(exc);[](#l4.15)
Py_XDECREF(tb);[](#l4.16)
PyErr_NormalizeException(&exc2, &val2, &tb2);[](#l4.17)
PyException_SetContext(val2, val);[](#l4.18)
PyErr_Restore(exc2, val2, tb2);[](#l4.19)
}[](#l4.20)
_PyErr_ChainExceptions(exc, val, tb);[](#l4.21)
}Py_CLEAR(res);[](#l4.22) }[](#l4.23) return res;[](#l4.24)
--- a/Python/errors.c +++ b/Python/errors.c @@ -384,6 +384,30 @@ PyErr_SetExcInfo(PyObject p_type, PyObj 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;[](#l5.14)
- if (PyErr_Occurred()) {
PyObject *exc2, *val2, *tb2;[](#l5.17)
PyErr_Fetch(&exc2, &val2, &tb2);[](#l5.18)
PyErr_NormalizeException(&exc, &val, &tb);[](#l5.19)
Py_DECREF(exc);[](#l5.20)
Py_XDECREF(tb);[](#l5.21)
PyErr_NormalizeException(&exc2, &val2, &tb2);[](#l5.22)
PyException_SetContext(val2, val);[](#l5.23)
PyErr_Restore(exc2, val2, tb2);[](#l5.24)
- }
- else {
PyErr_Restore(exc, val, tb);[](#l5.27)
- }
+} + /* Convenience functions to set a type error exception and return 0 */ int