Kaydet (Commit) 877effc2 authored tarafından Hynek Schlawack's avatar Hynek Schlawack

#4841: Fix FileIO constructor to honor closefd when called repeatedly

Patch by Victor Stinner.
üst 8e6287f5
...@@ -593,6 +593,19 @@ class IOTest(unittest.TestCase): ...@@ -593,6 +593,19 @@ class IOTest(unittest.TestCase):
self.assertEqual(rawio.read(2), None) self.assertEqual(rawio.read(2), None)
self.assertEqual(rawio.read(2), b"") self.assertEqual(rawio.read(2), b"")
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): class CIOTest(IOTest):
def test_IOBase_finalize(self): def test_IOBase_finalize(self):
......
...@@ -198,9 +198,13 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) ...@@ -198,9 +198,13 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
assert(PyFileIO_Check(oself)); assert(PyFileIO_Check(oself));
if (self->fd >= 0) { if (self->fd >= 0) {
/* Have to close the existing file first. */ if (self->closefd) {
if (internal_close(self) < 0) /* Have to close the existing file first. */
return -1; if (internal_close(self) < 0)
return -1;
}
else
self->fd = -1;
} }
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:fileio", if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:fileio",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment