Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3.3
Changeset: r76309:76586cc5f6f7
Date: 2015-03-10 17:33 +0100
http://bitbucket.org/pypy/pypy/changeset/76586cc5f6f7/

Log:    Record __context__ exception when close() fails after a failure in
        flush()

diff --git a/pypy/module/_io/interp_bufferedio.py 
b/pypy/module/_io/interp_bufferedio.py
--- a/pypy/module/_io/interp_bufferedio.py
+++ b/pypy/module/_io/interp_bufferedio.py
@@ -306,11 +306,23 @@
         with self.lock:
             if self._closed(space):
                 return
+        flush_exception = None
         try:
             space.call_method(self, "flush")
+        except OperationError as flush_exception:
+            pass
         finally:
             with self.lock:
-                space.call_method(self.w_raw, "close")
+                try:
+                    space.call_method(self.w_raw, "close")
+                except OperationError as e:
+                    if flush_exception:
+                        space.setattr(e.get_w_value(space),
+                                      space.wrap('__context__'),
+                                      flush_exception.get_w_value(space))
+                    raise
+            if flush_exception:
+                raise
 
     def _dealloc_warn_w(self, space, w_source):
         space.call_method(self.w_raw, "_dealloc_warn", w_source)
diff --git a/pypy/module/_io/test/test_bufferedio.py 
b/pypy/module/_io/test/test_bufferedio.py
--- a/pypy/module/_io/test/test_bufferedio.py
+++ b/pypy/module/_io/test/test_bufferedio.py
@@ -560,6 +560,7 @@
         b.flush = bad_flush
         err = raises(IOError, b.close)  # exception not swallowed
         assert err.value.args == ('close',)
+        assert err.value.__context__.args == ('flush',)
         assert not b.closed
 
 class AppTestBufferedRWPair:
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to