[pypy-commit] pypy py3.3: Record __context__ exception when close() fails after a failure in flush()

amauryfa noreply at buildbot.pypy.org
Tue Mar 10 21:56:54 CET 2015


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
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:


More information about the pypy-commit mailing list