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
b349bbfa
Commit
b349bbfa
authored
Feb 21, 2015
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #5700: io.FileIO() called flush() after closing the file.
flush() was not called in close() if closefd=False.
parent
b64f85e5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
9 deletions
+58
-9
Lib/test/test_io.py
Lib/test/test_io.py
+48
-2
Misc/NEWS
Misc/NEWS
+3
-0
Modules/_io/fileio.c
Modules/_io/fileio.c
+7
-7
No files found.
Lib/test/test_io.py
View file @
b349bbfa
...
...
@@ -564,13 +564,43 @@ class IOTest(unittest.TestCase):
with
self
.
open
(
zero
,
"r"
)
as
f
:
self
.
assertRaises
(
OverflowError
,
f
.
read
)
def
test_flush_error_on_close
(
self
):
f
=
self
.
open
(
support
.
TESTFN
,
"wb"
,
buffering
=
0
)
def
check_flush_error_on_close
(
self
,
*
args
,
**
kwargs
):
# Test that the file is closed despite failed flush
# and that flush() is called before file closed.
f
=
self
.
open
(
*
args
,
**
kwargs
)
closed
=
[]
def
bad_flush
():
closed
[:]
=
[
f
.
closed
]
raise
IOError
()
f
.
flush
=
bad_flush
self
.
assertRaises
(
IOError
,
f
.
close
)
# exception not swallowed
self
.
assertTrue
(
f
.
closed
)
self
.
assertTrue
(
closed
)
# flush() called
self
.
assertFalse
(
closed
[
0
])
# flush() called before file closed
def
test_flush_error_on_close
(
self
):
# raw file
# Issue #5700: io.FileIO calls flush() after file closed
self
.
check_flush_error_on_close
(
support
.
TESTFN
,
'wb'
,
buffering
=
0
)
fd
=
os
.
open
(
support
.
TESTFN
,
os
.
O_WRONLY
|
os
.
O_CREAT
)
self
.
check_flush_error_on_close
(
fd
,
'wb'
,
buffering
=
0
)
fd
=
os
.
open
(
support
.
TESTFN
,
os
.
O_WRONLY
|
os
.
O_CREAT
)
self
.
check_flush_error_on_close
(
fd
,
'wb'
,
buffering
=
0
,
closefd
=
False
)
os
.
close
(
fd
)
# buffered io
self
.
check_flush_error_on_close
(
support
.
TESTFN
,
'wb'
)
fd
=
os
.
open
(
support
.
TESTFN
,
os
.
O_WRONLY
|
os
.
O_CREAT
)
self
.
check_flush_error_on_close
(
fd
,
'wb'
)
fd
=
os
.
open
(
support
.
TESTFN
,
os
.
O_WRONLY
|
os
.
O_CREAT
)
self
.
check_flush_error_on_close
(
fd
,
'wb'
,
closefd
=
False
)
os
.
close
(
fd
)
# text io
self
.
check_flush_error_on_close
(
support
.
TESTFN
,
'w'
)
fd
=
os
.
open
(
support
.
TESTFN
,
os
.
O_WRONLY
|
os
.
O_CREAT
)
self
.
check_flush_error_on_close
(
fd
,
'w'
)
fd
=
os
.
open
(
support
.
TESTFN
,
os
.
O_WRONLY
|
os
.
O_CREAT
)
self
.
check_flush_error_on_close
(
fd
,
'w'
,
closefd
=
False
)
os
.
close
(
fd
)
def
test_multi_close
(
self
):
f
=
self
.
open
(
support
.
TESTFN
,
"wb"
,
buffering
=
0
)
...
...
@@ -741,13 +771,21 @@ class CommonBufferedTests:
self
.
assertEqual
(
repr
(
b
),
"<%s name='dummy'>"
%
clsname
)
def
test_flush_error_on_close
(
self
):
# Test that buffered file is closed despite failed flush
# and that flush() is called before file closed.
raw
=
self
.
MockRawIO
()
closed
=
[]
def
bad_flush
():
closed
[:]
=
[
b
.
closed
,
raw
.
closed
]
raise
IOError
()
raw
.
flush
=
bad_flush
b
=
self
.
tp
(
raw
)
self
.
assertRaises
(
IOError
,
b
.
close
)
# exception not swallowed
self
.
assertTrue
(
b
.
closed
)
self
.
assertTrue
(
raw
.
closed
)
self
.
assertTrue
(
closed
)
# flush() called
self
.
assertFalse
(
closed
[
0
])
# flush() called before file closed
self
.
assertFalse
(
closed
[
1
])
def
test_close_error_on_close
(
self
):
raw
=
self
.
MockRawIO
()
...
...
@@ -2484,12 +2522,20 @@ class TextIOWrapperTest(unittest.TestCase):
self
.
assertEqual
(
content
.
count
(
"Thread%03d
\
n
"
%
n
),
1
)
def
test_flush_error_on_close
(
self
):
# Test that text file is closed despite failed flush
# and that flush() is called before file closed.
txt
=
self
.
TextIOWrapper
(
self
.
BytesIO
(
self
.
testdata
),
encoding
=
"ascii"
)
closed
=
[]
def
bad_flush
():
closed
[:]
=
[
txt
.
closed
,
txt
.
buffer
.
closed
]
raise
IOError
()
txt
.
flush
=
bad_flush
self
.
assertRaises
(
IOError
,
txt
.
close
)
# exception not swallowed
self
.
assertTrue
(
txt
.
closed
)
self
.
assertTrue
(
txt
.
buffer
.
closed
)
self
.
assertTrue
(
closed
)
# flush() called
self
.
assertFalse
(
closed
[
0
])
# flush() called before file closed
self
.
assertFalse
(
closed
[
1
])
def
test_multi_close
(
self
):
txt
=
self
.
TextIOWrapper
(
self
.
BytesIO
(
self
.
testdata
),
encoding
=
"ascii"
)
...
...
Misc/NEWS
View file @
b349bbfa
...
...
@@ -18,6 +18,9 @@ Core and Builtins
Library
-------
- Issue #5700: io.FileIO() called flush() after closing the file.
flush() was not called in close() if closefd=False.
- Issue #21548: Fix pydoc.synopsis() and pydoc.apropos() on modules with empty
docstrings. Initial patch by Yuyang Guo.
...
...
Modules/_io/fileio.c
View file @
b349bbfa
...
...
@@ -101,16 +101,16 @@ internal_close(fileio *self)
static
PyObject
*
fileio_close
(
fileio
*
self
)
{
PyObject
*
res
;
res
=
PyObject_CallMethod
((
PyObject
*
)
&
PyRawIOBase_Type
,
"close"
,
"O"
,
self
);
if
(
!
self
->
closefd
)
{
self
->
fd
=
-
1
;
Py_RETURN_NONE
;
return
res
;
}
errno
=
internal_close
(
self
);
if
(
errno
<
0
)
return
NULL
;
return
PyObject_CallMethod
((
PyObject
*
)
&
PyRawIOBase_Type
,
"close"
,
"O"
,
self
);
if
(
internal_close
(
self
)
<
0
)
Py_CLEAR
(
res
);
return
res
;
}
static
PyObject
*
...
...
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