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
a2d6d712
Commit
a2d6d712
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 (#16597)
parent
bacf1bf3
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
13 deletions
+68
-13
Lib/_pyio.py
Lib/_pyio.py
+8
-4
Lib/test/test_io.py
Lib/test/test_io.py
+27
-0
Modules/_io/bufferedio.c
Modules/_io/bufferedio.c
+17
-5
Modules/_io/textio.c
Modules/_io/textio.c
+16
-4
No files found.
Lib/_pyio.py
View file @
a2d6d712
...
...
@@ -340,8 +340,10 @@ class IOBase:
This method has no effect if the file is already closed.
"""
if
not
self
.
__closed
:
self
.
flush
()
self
.
__closed
=
True
try
:
self
.
flush
()
finally
:
self
.
__closed
=
True
def
__del__
(
self
):
"""Destructor. Calls close()."""
...
...
@@ -1568,8 +1570,10 @@ class TextIOWrapper(TextIOBase):
def
close
(
self
):
if
self
.
buffer
is
not
None
and
not
self
.
closed
:
self
.
flush
()
self
.
buffer
.
close
()
try
:
self
.
flush
()
finally
:
self
.
buffer
.
close
()
@
property
def
closed
(
self
):
...
...
Lib/test/test_io.py
View file @
a2d6d712
...
...
@@ -573,6 +573,7 @@ class IOTest(unittest.TestCase):
raise
IOError
()
f
.
flush
=
bad_flush
self
.
assertRaises
(
IOError
,
f
.
close
)
# exception not swallowed
self
.
assertTrue
(
f
.
closed
)
def
test_multi_close
(
self
):
f
=
self
.
open
(
support
.
TESTFN
,
"wb"
,
buffering
=
0
)
...
...
@@ -732,6 +733,21 @@ class CommonBufferedTests:
raw
.
flush
=
bad_flush
b
=
self
.
tp
(
raw
)
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
.
assertFalse
(
b
.
closed
)
def
test_multi_close
(
self
):
raw
=
self
.
MockRawIO
()
...
...
@@ -1230,6 +1246,16 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
DeprecationWarning
)):
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
):
tp
=
io
.
BufferedWriter
...
...
@@ -2378,6 +2404,7 @@ class TextIOWrapperTest(unittest.TestCase):
raise
IOError
()
txt
.
flush
=
bad_flush
self
.
assertRaises
(
IOError
,
txt
.
close
)
# exception not swallowed
self
.
assertTrue
(
txt
.
closed
)
def
test_multi_close
(
self
):
txt
=
self
.
TextIOWrapper
(
self
.
BytesIO
(
self
.
testdata
),
encoding
=
"ascii"
)
...
...
Modules/_io/bufferedio.c
View file @
a2d6d712
...
...
@@ -455,7 +455,7 @@ buffered_closed_get(buffered *self, void *context)
static
PyObject
*
buffered_close
(
buffered
*
self
,
PyObject
*
args
)
{
PyObject
*
res
=
NULL
;
PyObject
*
res
=
NULL
,
*
exc
=
NULL
,
*
val
,
*
tb
;
int
r
;
CHECK_INITIALIZED
(
self
)
...
...
@@ -475,13 +475,25 @@ buffered_close(buffered *self, PyObject *args)
res
=
PyObject_CallMethodObjArgs
((
PyObject
*
)
self
,
_PyIO_str_flush
,
NULL
);
if
(
!
ENTER_BUFFERED
(
self
))
return
NULL
;
if
(
res
==
NULL
)
{
goto
end
;
}
Py_X
DECREF
(
res
);
if
(
res
==
NULL
)
PyErr_Fetch
(
&
exc
,
&
val
,
&
tb
)
;
else
Py_
DECREF
(
res
);
res
=
PyObject_CallMethodObjArgs
(
self
->
raw
,
_PyIO_str_close
,
NULL
);
if
(
exc
!=
NULL
)
{
if
(
res
!=
NULL
)
{
Py_CLEAR
(
res
);
PyErr_Restore
(
exc
,
val
,
tb
);
}
else
{
Py_DECREF
(
exc
);
Py_XDECREF
(
val
);
Py_XDECREF
(
tb
);
}
}
end:
LEAVE_BUFFERED
(
self
)
return
res
;
...
...
Modules/_io/textio.c
View file @
a2d6d712
...
...
@@ -2442,14 +2442,26 @@ textiowrapper_close(textio *self, PyObject *args)
Py_RETURN_NONE
;
/* stream already closed */
}
else
{
PyObject
*
exc
=
NULL
,
*
val
,
*
tb
;
res
=
PyObject_CallMethod
((
PyObject
*
)
self
,
"flush"
,
NULL
);
if
(
res
==
NULL
)
{
return
NULL
;
}
if
(
res
==
NULL
)
PyErr_Fetch
(
&
exc
,
&
val
,
&
tb
);
else
Py_DECREF
(
res
);
return
PyObject_CallMethod
(
self
->
buffer
,
"close"
,
NULL
);
res
=
PyObject_CallMethod
(
self
->
buffer
,
"close"
,
NULL
);
if
(
exc
!=
NULL
)
{
if
(
res
!=
NULL
)
{
Py_CLEAR
(
res
);
PyErr_Restore
(
exc
,
val
,
tb
);
}
else
{
Py_DECREF
(
exc
);
Py_XDECREF
(
val
);
Py_XDECREF
(
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