]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #23781: Add private helper function _PyErr_ReplaceException() that
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 30 Mar 2015 06:48:42 +0000 (09:48 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 30 Mar 2015 06:48:42 +0000 (09:48 +0300)
corresponds _PyErr_ChainExceptions() in Python 3 to help porting patches
from Python 3.

Include/pyerrors.h
Modules/_io/_iomodule.c
Modules/_io/bufferedio.c
Modules/_io/textio.c
Python/errors.c

index dbe3bfa5f2d73914a138803cf0e948e9868efd1c..2ef205ea4bcdb33f1ca532bc1eb3bab07cd278b6 100644 (file)
@@ -91,6 +91,7 @@ PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, PyObject *);
 PyAPI_FUNC(int) PyErr_GivenExceptionMatches(PyObject *, PyObject *);
 PyAPI_FUNC(int) PyErr_ExceptionMatches(PyObject *);
 PyAPI_FUNC(void) PyErr_NormalizeException(PyObject**, PyObject**, PyObject**);
+PyAPI_FUNC(void) _PyErr_ReplaceException(PyObject *, PyObject *, PyObject *);
 
 /* */
 
index 29db1642c43a8a954c1dcc0ffcd007d8fabbb4c8..04c444552d943602717c02df7a1d87de5d1a58bc 100644 (file)
@@ -529,14 +529,8 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
         PyObject *exc, *val, *tb, *close_result;
         PyErr_Fetch(&exc, &val, &tb);
         close_result = PyObject_CallMethod(result, "close", NULL);
-        if (close_result != NULL) {
-            Py_DECREF(close_result);
-            PyErr_Restore(exc, val, tb);
-        } else {
-            Py_XDECREF(exc);
-            Py_XDECREF(val);
-            Py_XDECREF(tb);
-        }
+        _PyErr_ReplaceException(exc, val, tb);
+        Py_XDECREF(close_result);
         Py_DECREF(result);
     }
     Py_XDECREF(modeobj);
index f146958b804b6d8eeaffe75d4e9e335d2dd17eb0..b4632edf44f7f31525770bbc6d9ba1e445df620d 100644 (file)
@@ -483,15 +483,8 @@ buffered_close(buffered *self, PyObject *args)
     res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_close, NULL);
 
     if (exc != NULL) {
-        if (res != NULL) {
-            Py_CLEAR(res);
-            PyErr_Restore(exc, val, tb);
-        }
-        else {
-            Py_DECREF(exc);
-            Py_XDECREF(val);
-            Py_XDECREF(tb);
-        }
+        _PyErr_ReplaceException(exc, val, tb);
+        Py_CLEAR(res);
     }
 
 end:
index 48e0c665641cf6a7c246a47819d0a1f882ec2f21..65c8d8d4e893511a7a394617fece06cbcf901b98 100644 (file)
@@ -2480,15 +2480,8 @@ textiowrapper_close(textio *self, PyObject *args)
 
         res = PyObject_CallMethod(self->buffer, "close", NULL);
         if (exc != NULL) {
-            if (res != NULL) {
-                Py_CLEAR(res);
-                PyErr_Restore(exc, val, tb);
-            }
-            else {
-                Py_DECREF(exc);
-                Py_XDECREF(val);
-                Py_XDECREF(tb);
-            }
+            _PyErr_ReplaceException(exc, val, tb);
+            Py_CLEAR(res);
         }
         return res;
     }
index 64ba05dd6cd72190ff4f14914f5287ec8d17ebe8..00dfd3ec540ebabe7c3ecde41a558bdc4fce6130 100644 (file)
@@ -263,6 +263,26 @@ PyErr_Clear(void)
     PyErr_Restore(NULL, NULL, NULL);
 }
 
+/* Restore previously fetched exception if an exception is not set,
+   otherwise drop previously fetched exception.
+   Like _PyErr_ChainExceptions() in Python 3, but doesn't set the context.
+ */
+void
+_PyErr_ReplaceException(PyObject *exc, PyObject *val, PyObject *tb)
+{
+    if (exc == NULL)
+        return;
+
+    if (PyErr_Occurred()) {
+        Py_DECREF(exc);
+        Py_XDECREF(val);
+        Py_XDECREF(tb);
+    }
+    else {
+        PyErr_Restore(exc, val, tb);
+    }
+}
+
 /* Convenience functions to set a type error exception and return 0 */
 
 int