[Python-checkins] cpython (3.2): #4841: Fix FileIO constructor to honor closefd when called repeatedly

hynek.schlawack python-checkins at python.org
Fri May 25 10:30:09 CEST 2012


http://hg.python.org/cpython/rev/8a58670048c9
changeset: 77135:8a58670048c9
branch: 3.2
parent: 77122:9ef2cb56926d
user: Hynek Schlawack <hs at ox.cx>
date: Fri May 25 10:05:53 2012 +0200
summary:
 #4841: Fix FileIO constructor to honor closefd when called repeatedly
Patch by Victor Stinner.
files:
 Lib/test/test_io.py | 13 +++++++++++++
 Modules/_io/fileio.c | 10 +++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -634,6 +634,19 @@
 for obj in test:
 self.assertTrue(hasattr(obj, "__dict__"))
 
+ def test_fileio_closefd(self):
+ # Issue #4841
+ with self.open(__file__, 'rb') as f1, \
+ self.open(__file__, 'rb') as f2:
+ fileio = self.FileIO(f1.fileno(), closefd=False)
+ # .__init__() must not close f1
+ fileio.__init__(f2.fileno(), closefd=False)
+ f1.readline()
+ # .close() must not close f2
+ fileio.close()
+ f2.readline()
+
+
 class CIOTest(IOTest):
 
 def test_IOBase_finalize(self):
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -227,9 +227,13 @@
 
 assert(PyFileIO_Check(oself));
 if (self->fd >= 0) {
- /* Have to close the existing file first. */
- if (internal_close(self) < 0)
- return -1;
+ if (self->closefd) {
+ /* Have to close the existing file first. */
+ if (internal_close(self) < 0)
+ return -1;
+ }
+ else
+ self->fd = -1;
 }
 
 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:fileio",
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /