Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6862361

Browse files
committed
call close on the underlying stream even if flush raises (closes #16597)
Patch by Serhiy Storchaka.
1 parent 5ff3f73 commit 6862361

5 files changed

Lines changed: 80 additions & 13 deletions

File tree

Lib/_pyio.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,10 @@ def close(self):
346346
This method has no effect if the file is already closed.
347347
"""
348348
if not self.__closed:
349-
self.flush()
350-
self.__closed = True
349+
try:
350+
self.flush()
351+
finally:
352+
self.__closed = True
351353

352354
def __del__(self):
353355
"""Destructor. Calls close()."""
@@ -1584,8 +1586,10 @@ def flush(self):
15841586

15851587
def close(self):
15861588
if self.buffer is not None and not self.closed:
1587-
self.flush()
1588-
self.buffer.close()
1589+
try:
1590+
self.flush()
1591+
finally:
1592+
self.buffer.close()
15891593

15901594
@property
15911595
def closed(self):

Lib/test/test_io.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ def bad_flush():
603603
raise IOError()
604604
f.flush = bad_flush
605605
self.assertRaises(IOError, f.close) # exception not swallowed
606+
self.assertTrue(f.closed)
606607

607608
def test_multi_close(self):
608609
f = self.open(support.TESTFN, "wb", buffering=0)
@@ -780,6 +781,22 @@ def bad_flush():
780781
raw.flush = bad_flush
781782
b = self.tp(raw)
782783
self.assertRaises(IOError, b.close) # exception not swallowed
784+
self.assertTrue(b.closed)
785+
786+
def test_close_error_on_close(self):
787+
raw = self.MockRawIO()
788+
def bad_flush():
789+
raise IOError('flush')
790+
def bad_close():
791+
raise IOError('close')
792+
raw.close = bad_close
793+
b = self.tp(raw)
794+
b.flush = bad_flush
795+
with self.assertRaises(IOError) as err: # exception not swallowed
796+
b.close()
797+
self.assertEqual(err.exception.args, ('close',))
798+
self.assertEqual(err.exception.__context__.args, ('flush',))
799+
self.assertFalse(b.closed)
783800

784801
def test_multi_close(self):
785802
raw = self.MockRawIO()
@@ -1296,6 +1313,16 @@ def test_max_buffer_size_removal(self):
12961313
with self.assertRaises(TypeError):
12971314
self.tp(self.MockRawIO(), 8, 12)
12981315

1316+
def test_write_error_on_close(self):
1317+
raw = self.MockRawIO()
1318+
def bad_write(b):
1319+
raise IOError()
1320+
raw.write = bad_write
1321+
b = self.tp(raw)
1322+
b.write(b'spam')
1323+
self.assertRaises(IOError, b.close) # exception not swallowed
1324+
self.assertTrue(b.closed)
1325+
12991326

13001327
class CBufferedWriterTest(BufferedWriterTest, SizeofTest):
13011328
tp = io.BufferedWriter
@@ -2465,6 +2492,7 @@ def bad_flush():
24652492
raise IOError()
24662493
txt.flush = bad_flush
24672494
self.assertRaises(IOError, txt.close) # exception not swallowed
2495+
self.assertTrue(txt.closed)
24682496

24692497
def test_multi_close(self):
24702498
txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.3.1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #16597: Make BufferedIO.close call close() on the underlying stream if
16+
invoking flush() fails.
17+
1518
- Issue #16722: In the bytes() constructor, try to call __bytes__ on the
1619
argument before __index__.
1720

Modules/_io/bufferedio.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ buffered_closed_get(buffered *self, void *context)
484484
static PyObject *
485485
buffered_close(buffered *self, PyObject *args)
486486
{
487-
PyObject *res = NULL;
487+
PyObject *res = NULL, *exc = NULL, *val, *tb;
488488
int r;
489489

490490
CHECK_INITIALIZED(self)
@@ -512,13 +512,29 @@ buffered_close(buffered *self, PyObject *args)
512512
res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
513513
if (!ENTER_BUFFERED(self))
514514
return NULL;
515-
if (res == NULL) {
516-
goto end;
517-
}
518-
Py_XDECREF(res);
515+
if (res == NULL)
516+
PyErr_Fetch(&exc, &val, &tb);
517+
else
518+
Py_DECREF(res);
519519

520520
res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_close, NULL);
521521

522+
if (exc != NULL) {
523+
if (res != NULL) {
524+
Py_CLEAR(res);
525+
PyErr_Restore(exc, val, tb);
526+
}
527+
else {
528+
PyObject *val2;
529+
Py_DECREF(exc);
530+
Py_XDECREF(tb);
531+
PyErr_Fetch(&exc, &val2, &tb);
532+
PyErr_NormalizeException(&exc, &val2, &tb);
533+
PyException_SetContext(val2, val);
534+
PyErr_Restore(exc, val2, tb);
535+
}
536+
}
537+
522538
end:
523539
LEAVE_BUFFERED(self)
524540
return res;

Modules/_io/textio.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,6 +2554,7 @@ textiowrapper_close(textio *self, PyObject *args)
25542554
Py_RETURN_NONE; /* stream already closed */
25552555
}
25562556
else {
2557+
PyObject *exc = NULL, *val, *tb;
25572558
if (self->deallocating) {
25582559
res = _PyObject_CallMethodId(self->buffer, &PyId__dealloc_warn, "O", self);
25592560
if (res)
@@ -2562,13 +2563,28 @@ textiowrapper_close(textio *self, PyObject *args)
25622563
PyErr_Clear();
25632564
}
25642565
res = _PyObject_CallMethodId((PyObject *)self, &PyId_flush, NULL);
2565-
if (res == NULL) {
2566-
return NULL;
2567-
}
2566+
if (res == NULL)
2567+
PyErr_Fetch(&exc, &val, &tb);
25682568
else
25692569
Py_DECREF(res);
25702570

2571-
return _PyObject_CallMethodId(self->buffer, &PyId_close, NULL);
2571+
res = _PyObject_CallMethodId(self->buffer, &PyId_close, NULL);
2572+
if (exc != NULL) {
2573+
if (res != NULL) {
2574+
Py_CLEAR(res);
2575+
PyErr_Restore(exc, val, tb);
2576+
}
2577+
else {
2578+
PyObject *val2;
2579+
Py_DECREF(exc);
2580+
Py_XDECREF(tb);
2581+
PyErr_Fetch(&exc, &val2, &tb);
2582+
PyErr_NormalizeException(&exc, &val2, &tb);
2583+
PyException_SetContext(val2, val);
2584+
PyErr_Restore(exc, val2, tb);
2585+
}
2586+
}
2587+
return res;
25722588
}
25732589
}
25742590

0 commit comments

Comments
 (0)