diff Lib/test/test_io.py @ 49783:eade47dfc1e4 legacy-trunk

Fixed 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. Approved by Barry Backport of r67106 from the py3k branch
author Amaury Forgeot d'Arc <amauryfa@gmail.com>
date Thu, 20 Nov 2008 23:34:31 +0000
parents f41cb206d061
children dc4acf753296
line wrap: on
line diff
--- a/Lib/test/test_io.py	Thu Nov 20 22:06:22 2008 +0000
+++ b/Lib/test/test_io.py	Thu Nov 20 23:34:31 2008 +0000
@@ -272,6 +272,30 @@
         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):