Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
c610f3df
Commit
c610f3df
authored
Dec 20, 2012
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
call close on the underlying stream even if flush raises (closes #16597)
Patch by Serhiy Storchaka.
parent
45bf8a7f
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
80 additions
and
13 deletions
+80
-13
Lib/_pyio.py
Lib/_pyio.py
+8
-4
Lib/test/test_io.py
Lib/test/test_io.py
+28
-0
Misc/NEWS
Misc/NEWS
+3
-0
Modules/_io/bufferedio.c
Modules/_io/bufferedio.c
+21
-5
Modules/_io/textio.c
Modules/_io/textio.c
+20
-4
No files found.
Lib/_pyio.py
View file @
c610f3df
...
@@ -346,8 +346,10 @@ class IOBase(metaclass=abc.ABCMeta):
...
@@ -346,8 +346,10 @@ class IOBase(metaclass=abc.ABCMeta):
This method has no effect if the file is already closed.
This method has no effect if the file is already closed.
"""
"""
if
not
self
.
__closed
:
if
not
self
.
__closed
:
self
.
flush
()
try
:
self
.
__closed
=
True
self
.
flush
()
finally
:
self
.
__closed
=
True
def
__del__
(
self
):
def
__del__
(
self
):
"""Destructor. Calls close()."""
"""Destructor. Calls close()."""
...
@@ -1584,8 +1586,10 @@ class TextIOWrapper(TextIOBase):
...
@@ -1584,8 +1586,10 @@ class TextIOWrapper(TextIOBase):
def
close
(
self
):
def
close
(
self
):
if
self
.
buffer
is
not
None
and
not
self
.
closed
:
if
self
.
buffer
is
not
None
and
not
self
.
closed
:
self
.
flush
()
try
:
self
.
buffer
.
close
()
self
.
flush
()
finally
:
self
.
buffer
.
close
()
@
property
@
property
def
closed
(
self
):
def
closed
(
self
):
...
...
Lib/test/test_io.py
View file @
c610f3df
...
@@ -603,6 +603,7 @@ class IOTest(unittest.TestCase):
...
@@ -603,6 +603,7 @@ class IOTest(unittest.TestCase):
raise
IOError
()
raise
IOError
()
f
.
flush
=
bad_flush
f
.
flush
=
bad_flush
self
.
assertRaises
(
IOError
,
f
.
close
)
# exception not swallowed
self
.
assertRaises
(
IOError
,
f
.
close
)
# exception not swallowed
self
.
assertTrue
(
f
.
closed
)
def
test_multi_close
(
self
):
def
test_multi_close
(
self
):
f
=
self
.
open
(
support
.
TESTFN
,
"wb"
,
buffering
=
0
)
f
=
self
.
open
(
support
.
TESTFN
,
"wb"
,
buffering
=
0
)
...
@@ -780,6 +781,22 @@ class CommonBufferedTests:
...
@@ -780,6 +781,22 @@ class CommonBufferedTests:
raw
.
flush
=
bad_flush
raw
.
flush
=
bad_flush
b
=
self
.
tp
(
raw
)
b
=
self
.
tp
(
raw
)
self
.
assertRaises
(
IOError
,
b
.
close
)
# exception not swallowed
self
.
assertRaises
(
IOError
,
b
.
close
)
# exception not swallowed
self
.
assertTrue
(
b
.
closed
)
def
test_close_error_on_close
(
self
):
raw
=
self
.
MockRawIO
()
def
bad_flush
():
raise
IOError
(
'flush'
)
def
bad_close
():
raise
IOError
(
'close'
)
raw
.
close
=
bad_close
b
=
self
.
tp
(
raw
)
b
.
flush
=
bad_flush
with
self
.
assertRaises
(
IOError
)
as
err
:
# exception not swallowed
b
.
close
()
self
.
assertEqual
(
err
.
exception
.
args
,
(
'close'
,))
self
.
assertEqual
(
err
.
exception
.
__context__
.
args
,
(
'flush'
,))
self
.
assertFalse
(
b
.
closed
)
def
test_multi_close
(
self
):
def
test_multi_close
(
self
):
raw
=
self
.
MockRawIO
()
raw
=
self
.
MockRawIO
()
...
@@ -1296,6 +1313,16 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
...
@@ -1296,6 +1313,16 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
with
self
.
assertRaises
(
TypeError
):
with
self
.
assertRaises
(
TypeError
):
self
.
tp
(
self
.
MockRawIO
(),
8
,
12
)
self
.
tp
(
self
.
MockRawIO
(),
8
,
12
)
def
test_write_error_on_close
(
self
):
raw
=
self
.
MockRawIO
()
def
bad_write
(
b
):
raise
IOError
()
raw
.
write
=
bad_write
b
=
self
.
tp
(
raw
)
b
.
write
(
b'spam'
)
self
.
assertRaises
(
IOError
,
b
.
close
)
# exception not swallowed
self
.
assertTrue
(
b
.
closed
)
class
CBufferedWriterTest
(
BufferedWriterTest
,
SizeofTest
):
class
CBufferedWriterTest
(
BufferedWriterTest
,
SizeofTest
):
tp
=
io
.
BufferedWriter
tp
=
io
.
BufferedWriter
...
@@ -2465,6 +2492,7 @@ class TextIOWrapperTest(unittest.TestCase):
...
@@ -2465,6 +2492,7 @@ class TextIOWrapperTest(unittest.TestCase):
raise
IOError
()
raise
IOError
()
txt
.
flush
=
bad_flush
txt
.
flush
=
bad_flush
self
.
assertRaises
(
IOError
,
txt
.
close
)
# exception not swallowed
self
.
assertRaises
(
IOError
,
txt
.
close
)
# exception not swallowed
self
.
assertTrue
(
txt
.
closed
)
def
test_multi_close
(
self
):
def
test_multi_close
(
self
):
txt
=
self
.
TextIOWrapper
(
self
.
BytesIO
(
self
.
testdata
),
encoding
=
"ascii"
)
txt
=
self
.
TextIOWrapper
(
self
.
BytesIO
(
self
.
testdata
),
encoding
=
"ascii"
)
...
...
Misc/NEWS
View file @
c610f3df
...
@@ -12,6 +12,9 @@ What's New in Python 3.3.1?
...
@@ -12,6 +12,9 @@ What's New in Python 3.3.1?
Core and Builtins
Core and Builtins
-----------------
-----------------
- Issue #16597: Make BufferedIO.close call close() on the underlying stream if
invoking flush() fails.
- Issue #16722: In the bytes() constructor, try to call __bytes__ on the
- Issue #16722: In the bytes() constructor, try to call __bytes__ on the
argument before __index__.
argument before __index__.
...
...
Modules/_io/bufferedio.c
View file @
c610f3df
...
@@ -484,7 +484,7 @@ buffered_closed_get(buffered *self, void *context)
...
@@ -484,7 +484,7 @@ buffered_closed_get(buffered *self, void *context)
static
PyObject
*
static
PyObject
*
buffered_close
(
buffered
*
self
,
PyObject
*
args
)
buffered_close
(
buffered
*
self
,
PyObject
*
args
)
{
{
PyObject
*
res
=
NULL
;
PyObject
*
res
=
NULL
,
*
exc
=
NULL
,
*
val
,
*
tb
;
int
r
;
int
r
;
CHECK_INITIALIZED
(
self
)
CHECK_INITIALIZED
(
self
)
...
@@ -512,13 +512,29 @@ buffered_close(buffered *self, PyObject *args)
...
@@ -512,13 +512,29 @@ buffered_close(buffered *self, PyObject *args)
res
=
PyObject_CallMethodObjArgs
((
PyObject
*
)
self
,
_PyIO_str_flush
,
NULL
);
res
=
PyObject_CallMethodObjArgs
((
PyObject
*
)
self
,
_PyIO_str_flush
,
NULL
);
if
(
!
ENTER_BUFFERED
(
self
))
if
(
!
ENTER_BUFFERED
(
self
))
return
NULL
;
return
NULL
;
if
(
res
==
NULL
)
{
if
(
res
==
NULL
)
goto
end
;
PyErr_Fetch
(
&
exc
,
&
val
,
&
tb
)
;
}
else
Py_X
DECREF
(
res
);
Py_
DECREF
(
res
);
res
=
PyObject_CallMethodObjArgs
(
self
->
raw
,
_PyIO_str_close
,
NULL
);
res
=
PyObject_CallMethodObjArgs
(
self
->
raw
,
_PyIO_str_close
,
NULL
);
if
(
exc
!=
NULL
)
{
if
(
res
!=
NULL
)
{
Py_CLEAR
(
res
);
PyErr_Restore
(
exc
,
val
,
tb
);
}
else
{
PyObject
*
val2
;
Py_DECREF
(
exc
);
Py_XDECREF
(
tb
);
PyErr_Fetch
(
&
exc
,
&
val2
,
&
tb
);
PyErr_NormalizeException
(
&
exc
,
&
val2
,
&
tb
);
PyException_SetContext
(
val2
,
val
);
PyErr_Restore
(
exc
,
val2
,
tb
);
}
}
end:
end:
LEAVE_BUFFERED
(
self
)
LEAVE_BUFFERED
(
self
)
return
res
;
return
res
;
...
...
Modules/_io/textio.c
View file @
c610f3df
...
@@ -2554,6 +2554,7 @@ textiowrapper_close(textio *self, PyObject *args)
...
@@ -2554,6 +2554,7 @@ textiowrapper_close(textio *self, PyObject *args)
Py_RETURN_NONE
;
/* stream already closed */
Py_RETURN_NONE
;
/* stream already closed */
}
}
else
{
else
{
PyObject
*
exc
=
NULL
,
*
val
,
*
tb
;
if
(
self
->
deallocating
)
{
if
(
self
->
deallocating
)
{
res
=
_PyObject_CallMethodId
(
self
->
buffer
,
&
PyId__dealloc_warn
,
"O"
,
self
);
res
=
_PyObject_CallMethodId
(
self
->
buffer
,
&
PyId__dealloc_warn
,
"O"
,
self
);
if
(
res
)
if
(
res
)
...
@@ -2562,13 +2563,28 @@ textiowrapper_close(textio *self, PyObject *args)
...
@@ -2562,13 +2563,28 @@ textiowrapper_close(textio *self, PyObject *args)
PyErr_Clear
();
PyErr_Clear
();
}
}
res
=
_PyObject_CallMethodId
((
PyObject
*
)
self
,
&
PyId_flush
,
NULL
);
res
=
_PyObject_CallMethodId
((
PyObject
*
)
self
,
&
PyId_flush
,
NULL
);
if
(
res
==
NULL
)
{
if
(
res
==
NULL
)
return
NULL
;
PyErr_Fetch
(
&
exc
,
&
val
,
&
tb
);
}
else
else
Py_DECREF
(
res
);
Py_DECREF
(
res
);
return
_PyObject_CallMethodId
(
self
->
buffer
,
&
PyId_close
,
NULL
);
res
=
_PyObject_CallMethodId
(
self
->
buffer
,
&
PyId_close
,
NULL
);
if
(
exc
!=
NULL
)
{
if
(
res
!=
NULL
)
{
Py_CLEAR
(
res
);
PyErr_Restore
(
exc
,
val
,
tb
);
}
else
{
PyObject
*
val2
;
Py_DECREF
(
exc
);
Py_XDECREF
(
tb
);
PyErr_Fetch
(
&
exc
,
&
val2
,
&
tb
);
PyErr_NormalizeException
(
&
exc
,
&
val2
,
&
tb
);
PyException_SetContext
(
val2
,
val
);
PyErr_Restore
(
exc
,
val2
,
tb
);
}
}
return
res
;
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment