diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-24 21:21:57 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-24 21:21:57 (GMT) |
commit | 7665be6087d879a96c4238684b2dfc642c67e60c (patch) | |
tree | e0b51ca3e484cd4717ed04857935a018234a3d9b | |
parent | 8ffe917cee26b83fed4f227c4ed16d4eec15dcf9 (diff) | |
download | cpython-7665be6087d879a96c4238684b2dfc642c67e60c.zip cpython-7665be6087d879a96c4238684b2dfc642c67e60c.tar.gz cpython-7665be6087d879a96c4238684b2dfc642c67e60c.tar.bz2 |
Issue #21802: The reader in BufferedRWPair now is closed even when closing
writer failed in BufferedRWPair.close().
-rw-r--r-- | Lib/_pyio.py | 6 | ||||
-rw-r--r-- | Lib/test/test_io.py | 47 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Modules/_io/bufferedio.c | 14 |
4 files changed, 64 insertions, 6 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 09aa78d..3ed02e4 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -1230,8 +1230,10 @@ class BufferedRWPair(BufferedIOBase): return self.writer.flush() def close(self): - self.writer.close() - self.reader.close() + try: + self.writer.close() + finally: + self.reader.close() def isatty(self): return self.reader.isatty() or self.writer.isatty() diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 668b023..5b2bf46 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -1588,6 +1588,53 @@ class BufferedRWPairTest(unittest.TestCase): pair.close() self.assertTrue(pair.closed) + def test_reader_close_error_on_close(self): + def reader_close(): + reader_non_existing + reader = self.MockRawIO() + reader.close = reader_close + writer = self.MockRawIO() + pair = self.tp(reader, writer) + with self.assertRaises(NameError) as err: + pair.close() + self.assertIn('reader_non_existing', str(err.exception)) + self.assertTrue(pair.closed) + self.assertFalse(reader.closed) + self.assertTrue(writer.closed) + + def test_writer_close_error_on_close(self): + def writer_close(): + writer_non_existing + reader = self.MockRawIO() + writer = self.MockRawIO() + writer.close = writer_close + pair = self.tp(reader, writer) + with self.assertRaises(NameError) as err: + pair.close() + self.assertIn('writer_non_existing', str(err.exception)) + self.assertFalse(pair.closed) + self.assertTrue(reader.closed) + self.assertFalse(writer.closed) + + def test_reader_writer_close_error_on_close(self): + def reader_close(): + reader_non_existing + def writer_close(): + writer_non_existing + reader = self.MockRawIO() + reader.close = reader_close + writer = self.MockRawIO() + writer.close = writer_close + pair = self.tp(reader, writer) + with self.assertRaises(NameError) as err: + pair.close() + self.assertIn('reader_non_existing', str(err.exception)) + self.assertIsInstance(err.exception.__context__, NameError) + self.assertIn('writer_non_existing', str(err.exception.__context__)) + self.assertFalse(pair.closed) + self.assertFalse(reader.closed) + self.assertFalse(writer.closed) + def test_isatty(self): class SelectableIsAtty(MockRawIO): def __init__(self, isatty): @@ -18,6 +18,9 @@ Core and Builtins Library ------- +- Issue #21802: The reader in BufferedRWPair now is closed even when closing + writer failed in BufferedRWPair.close(). + - Issue #23671: string.Template now allows to specify the "self" parameter as keyword argument. string.Formatter now allows to specify the "self" and the "format_string" parameters as keyword arguments. diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 02cf038..445c870 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -2365,12 +2365,18 @@ bufferedrwpair_writable(rwpair *self, PyObject *args) static PyObject * bufferedrwpair_close(rwpair *self, PyObject *args) { + PyObject *exc = NULL, *val, *tb; PyObject *ret = _forward_call(self->writer, &PyId_close, args); if (ret == NULL) - return NULL; - Py_DECREF(ret); - - return _forward_call(self->reader, &PyId_close, args); + PyErr_Fetch(&exc, &val, &tb); + else + Py_DECREF(ret); + ret = _forward_call(self->reader, &PyId_close, args); + if (exc != NULL) { + _PyErr_ChainExceptions(exc, val, tb); + Py_CLEAR(ret); + } + return ret; } static PyObject * |