summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_io.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-06-09 06:15:42 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-06-09 06:15:42 (GMT)
commit85e4235c0e702b180f3f50b0124fd40c2f460be0 (patch)
treeaeab6bfae28ee6212e222b60f4ed7319aff12037 /Lib/test/test_io.py
parent9f488512a8f95684efe60a0fa6422389fbcd2ef6 (diff)
parent8a8f7f983099f172d1f7c25d4fd99f5c0eb14072 (diff)
downloadcpython-85e4235c0e702b180f3f50b0124fd40c2f460be0.zip
cpython-85e4235c0e702b180f3f50b0124fd40c2f460be0.tar.gz
cpython-85e4235c0e702b180f3f50b0124fd40c2f460be0.tar.bz2
Issue #21677: Fixed chaining nonnormalized exceptions in io close() methods.
Diffstat (limited to 'Lib/test/test_io.py')
-rw-r--r--Lib/test/test_io.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 347832d..3c2c102 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -792,9 +792,27 @@ class CommonBufferedTests:
with self.assertRaises(OSError) as err: # exception not swallowed
b.close()
self.assertEqual(err.exception.args, ('close',))
+ self.assertIsInstance(err.exception.__context__, OSError)
self.assertEqual(err.exception.__context__.args, ('flush',))
self.assertFalse(b.closed)
+ def test_nonnormalized_close_error_on_close(self):
+ # Issue #21677
+ raw = self.MockRawIO()
+ def bad_flush():
+ raise non_existing_flush
+ def bad_close():
+ raise non_existing_close
+ raw.close = bad_close
+ b = self.tp(raw)
+ b.flush = bad_flush
+ with self.assertRaises(NameError) as err: # exception not swallowed
+ b.close()
+ self.assertIn('non_existing_close', str(err.exception))
+ self.assertIsInstance(err.exception.__context__, NameError)
+ self.assertIn('non_existing_flush', str(err.exception.__context__))
+ self.assertFalse(b.closed)
+
def test_multi_close(self):
raw = self.MockRawIO()
b = self.tp(raw)
@@ -2576,6 +2594,39 @@ class TextIOWrapperTest(unittest.TestCase):
self.assertRaises(OSError, txt.close) # exception not swallowed
self.assertTrue(txt.closed)
+ def test_close_error_on_close(self):
+ buffer = self.BytesIO(self.testdata)
+ def bad_flush():
+ raise OSError('flush')
+ def bad_close():
+ raise OSError('close')
+ buffer.close = bad_close
+ txt = self.TextIOWrapper(buffer, encoding="ascii")
+ txt.flush = bad_flush
+ with self.assertRaises(OSError) as err: # exception not swallowed
+ txt.close()
+ self.assertEqual(err.exception.args, ('close',))
+ self.assertIsInstance(err.exception.__context__, OSError)
+ self.assertEqual(err.exception.__context__.args, ('flush',))
+ self.assertFalse(txt.closed)
+
+ def test_nonnormalized_close_error_on_close(self):
+ # Issue #21677
+ buffer = self.BytesIO(self.testdata)
+ def bad_flush():
+ raise non_existing_flush
+ def bad_close():
+ raise non_existing_close
+ buffer.close = bad_close
+ txt = self.TextIOWrapper(buffer, encoding="ascii")
+ txt.flush = bad_flush
+ with self.assertRaises(NameError) as err: # exception not swallowed
+ txt.close()
+ self.assertIn('non_existing_close', str(err.exception))
+ self.assertIsInstance(err.exception.__context__, NameError)
+ self.assertIn('non_existing_flush', str(err.exception.__context__))
+ self.assertFalse(txt.closed)
+
def test_multi_close(self):
txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
txt.close()