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

Skip to content

Commit 4c05969

Browse files
committed
merge 3.3 (#16597)
2 parents 7643c92 + 6862361 commit 4c05969

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()
@@ -1304,6 +1321,16 @@ def test_max_buffer_size_removal(self):
13041321
with self.assertRaises(TypeError):
13051322
self.tp(self.MockRawIO(), 8, 12)
13061323

1324+
def test_write_error_on_close(self):
1325+
raw = self.MockRawIO()
1326+
def bad_write(b):
1327+
raise IOError()
1328+
raw.write = bad_write
1329+
b = self.tp(raw)
1330+
b.write(b'spam')
1331+
self.assertRaises(IOError, b.close) # exception not swallowed
1332+
self.assertTrue(b.closed)
1333+
13071334

13081335
class CBufferedWriterTest(BufferedWriterTest, SizeofTest):
13091336
tp = io.BufferedWriter
@@ -2473,6 +2500,7 @@ def bad_flush():
24732500
raise IOError()
24742501
txt.flush = bad_flush
24752502
self.assertRaises(IOError, txt.close) # exception not swallowed
2503+
self.assertTrue(txt.closed)
24762504

24772505
def test_multi_close(self):
24782506
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
@@ -10,6 +10,9 @@ What's New in Python 3.4.0 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

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

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,10 +512,10 @@ 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

@@ -524,6 +524,22 @@ buffered_close(buffered *self, PyObject *args)
524524
self->buffer = NULL;
525525
}
526526

527+
if (exc != NULL) {
528+
if (res != NULL) {
529+
Py_CLEAR(res);
530+
PyErr_Restore(exc, val, tb);
531+
}
532+
else {
533+
PyObject *val2;
534+
Py_DECREF(exc);
535+
Py_XDECREF(tb);
536+
PyErr_Fetch(&exc, &val2, &tb);
537+
PyErr_NormalizeException(&exc, &val2, &tb);
538+
PyException_SetContext(val2, val);
539+
PyErr_Restore(exc, val2, tb);
540+
}
541+
}
542+
527543
end:
528544
LEAVE_BUFFERED(self)
529545
return res;

Modules/_io/textio.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,7 @@ textiowrapper_close(textio *self, PyObject *args)
25482548
Py_RETURN_NONE; /* stream already closed */
25492549
}
25502550
else {
2551+
PyObject *exc = NULL, *val, *tb;
25512552
if (self->deallocating) {
25522553
res = _PyObject_CallMethodId(self->buffer, &PyId__dealloc_warn, "O", self);
25532554
if (res)
@@ -2556,13 +2557,28 @@ textiowrapper_close(textio *self, PyObject *args)
25562557
PyErr_Clear();
25572558
}
25582559
res = _PyObject_CallMethodId((PyObject *)self, &PyId_flush, NULL);
2559-
if (res == NULL) {
2560-
return NULL;
2561-
}
2560+
if (res == NULL)
2561+
PyErr_Fetch(&exc, &val, &tb);
25622562
else
25632563
Py_DECREF(res);
25642564

2565-
return _PyObject_CallMethodId(self->buffer, &PyId_close, NULL);
2565+
res = _PyObject_CallMethodId(self->buffer, &PyId_close, NULL);
2566+
if (exc != NULL) {
2567+
if (res != NULL) {
2568+
Py_CLEAR(res);
2569+
PyErr_Restore(exc, val, tb);
2570+
}
2571+
else {
2572+
PyObject *val2;
2573+
Py_DECREF(exc);
2574+
Py_XDECREF(tb);
2575+
PyErr_Fetch(&exc, &val2, &tb);
2576+
PyErr_NormalizeException(&exc, &val2, &tb);
2577+
PyException_SetContext(val2, val);
2578+
PyErr_Restore(exc, val2, tb);
2579+
}
2580+
}
2581+
return res;
25662582
}
25672583
}
25682584

0 commit comments

Comments
 (0)