]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fixed issue #4233.
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Thu, 20 Nov 2008 23:34:31 +0000 (23:34 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Thu, 20 Nov 2008 23:34:31 +0000 (23:34 +0000)
Changed semantic of _fileio.FileIO's close()  method on file objects with closefd=False.
The file descriptor is still kept open but the file object behaves like a closed file.
The FileIO  object also got a new readonly attribute closefd.

Approved by Barry

Backport of r67106 from the py3k branch

Doc/library/io.rst
Lib/test/test_io.py
Misc/NEWS
Modules/_fileio.c

index 18df0e0514f5a61928f81ad57060d3692ca4d4f6..25d5f16dbae8ce0b20bfac4501b371bf650a350d 100644 (file)
@@ -214,8 +214,10 @@ I/O Base Classes
 
    .. method:: close()
 
-      Flush and close this stream.  This method has no effect if the file is
-      already closed.
+      Flush and close this stream. This method has no effect if the file is
+      already closed. Once the file is closed, any operation on the file 
+      (e.g. reading or writing) will raise an :exc:`IOError`. The internal
+      file descriptor isn't closed if *closefd* was False.
 
    .. attribute:: closed
 
index 378d9946408c325635e8fbc4828ca437a542f59e..c9bd38ddffbf44fcf85e5a5bbefaabbcfa7681d6 100644 (file)
@@ -272,6 +272,30 @@ class IOTest(unittest.TestCase):
         self.assertRaises(ValueError, io.open, test_support.TESTFN, 'w',
                           closefd=False)
 
+    def testReadClosed(self):
+        with io.open(test_support.TESTFN, "w") as f:
+            f.write("egg\n")
+        with io.open(test_support.TESTFN, "r") as f:
+            file = io.open(f.fileno(), "r", closefd=False)
+            self.assertEqual(file.read(), "egg\n")
+            file.seek(0)
+            file.close()
+            self.assertRaises(ValueError, file.read)
+
+    def test_no_closefd_with_filename(self):
+        # can't use closefd in combination with a file name
+        self.assertRaises(ValueError,
+                          io.open, test_support.TESTFN, "r", closefd=False)
+
+    def test_closefd_attr(self):
+        with io.open(test_support.TESTFN, "wb") as f:
+            f.write(b"egg\n")
+        with io.open(test_support.TESTFN, "r") as f:
+            self.assertEqual(f.buffer.raw.closefd, True)
+            file = io.open(f.fileno(), "r", closefd=False)
+            self.assertEqual(file.buffer.raw.closefd, False)
+
+
 class MemorySeekTestMixin:
 
     def testInit(self):
index c97f6b3450c318b803d665fda1554d8b6368e871..7e6787a841be51e2e95bd5952bc9bb3374302839 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,11 @@ What's New in Python 2.7 alpha 1
 Core and Builtins
 -----------------
 
+- Issue #4233: Changed semantic of ``_fileio.FileIO``'s ``close()`` 
+  method on file objects with closefd=False. The file descriptor is still
+  kept open but the file object behaves like a closed file. The ``FileIO``
+  object also got a new readonly attribute ``closefd``.
+
 - Issue #4348: Some bytearray methods returned that didn't cause any change to
   the bytearray, returned the same bytearray instead of a copy.
 
index 49bc29c73176f7c2a61d11db12378719c8b46834..b9310f380caf4635726d44d24a3bf1d819bdd10e 100644 (file)
@@ -61,10 +61,7 @@ static PyObject *
 fileio_close(PyFileIOObject *self)
 {
        if (!self->closefd) {
-               if (PyErr_WarnEx(PyExc_RuntimeWarning,
-                                "Trying to close unclosable fd!", 3) < 0) {
-                       return NULL;
-               }
+               self->fd = -1;
                Py_RETURN_NONE;
        }
        errno = internal_close(self);
@@ -820,6 +817,12 @@ get_closed(PyFileIOObject *self, void *closure)
        return PyBool_FromLong((long)(self->fd < 0));
 }
 
+static PyObject *
+get_closefd(PyFileIOObject *self, void *closure)
+{
+       return PyBool_FromLong((long)(self->closefd));
+}
+
 static PyObject *
 get_mode(PyFileIOObject *self, void *closure)
 {
@@ -828,6 +831,8 @@ get_mode(PyFileIOObject *self, void *closure)
 
 static PyGetSetDef fileio_getsetlist[] = {
        {"closed", (getter)get_closed, NULL, "True if the file is closed"},
+       {"closefd", (getter)get_closefd, NULL, 
+               "True if the file descriptor will be closed"},
        {"mode", (getter)get_mode, NULL, "String giving the file mode"},
        {0},
 };