]> granicus.if.org Git - python/commitdiff
Merged revisions 85291 via svnmerge from
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 6 Oct 2010 21:26:52 +0000 (21:26 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 6 Oct 2010 21:26:52 +0000 (21:26 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r85291 | antoine.pitrou | 2010-10-06 23:21:18 +0200 (mer., 06 oct. 2010) | 4 lines

  Issue #9759: GzipFile now raises ValueError when an operation is attempted
  after the file is closed.  Patch by Jeffrey Finkelstein.
........

Lib/gzip.py
Lib/test/test_gzip.py
Misc/NEWS

index 5fc7639f56fbdf82491625ae12d96548e965f738..2bcb4dbfb0b386e85c1f0705ffa8c0c4a2328d83 100644 (file)
@@ -138,6 +138,13 @@ class GzipFile(io.BufferedIOBase):
         s = repr(self.fileobj)
         return '<gzip ' + s[1:-1] + ' ' + hex(id(self)) + '>'
 
+    def _check_closed(self):
+        """Raises a ValueError if the underlying file object has been closed.
+
+        """
+        if self.closed:
+            raise ValueError('I/O operation on closed file.')
+
     def _init_write(self, filename):
         self.name = filename
         self.crc = zlib.crc32("") & 0xffffffffL
@@ -202,6 +209,7 @@ class GzipFile(io.BufferedIOBase):
             self.fileobj.read(2)     # Read & discard the 16-bit header CRC
 
     def write(self,data):
+        self._check_closed()
         if self.mode != WRITE:
             import errno
             raise IOError(errno.EBADF, "write() on read-only GzipFile object")
@@ -222,6 +230,7 @@ class GzipFile(io.BufferedIOBase):
         return len(data)
 
     def read(self, size=-1):
+        self._check_closed()
         if self.mode != READ:
             import errno
             raise IOError(errno.EBADF, "read() on write-only GzipFile object")
@@ -359,6 +368,7 @@ class GzipFile(io.BufferedIOBase):
             self.myfileobj = None
 
     def flush(self,zlib_mode=zlib.Z_SYNC_FLUSH):
+        self._check_closed()
         if self.mode == WRITE:
             # Ensure the compressor's buffer is flushed
             self.fileobj.write(self.compress.flush(zlib_mode))
index 87131d0c7448d82d922ed0e90d09a5b48bc25b96..2ddebaf95682659cd7f13dd02382a0f714ec6cde 100644 (file)
@@ -51,6 +51,28 @@ class TestGzip(unittest.TestCase):
         f = gzip.GzipFile(self.filename, 'r') ; d = f.read() ; f.close()
         self.assertEqual(d, data1*50)
 
+    def test_io_on_closed_object(self):
+        # Test that I/O operations on closed GzipFile objects raise a
+        # ValueError, just like the corresponding functions on file objects.
+
+        # Write to a file, open it for reading, then close it.
+        self.test_write()
+        f = gzip.GzipFile(self.filename, 'r')
+        f.close()
+        with self.assertRaises(ValueError):
+            f.read(1)
+        with self.assertRaises(ValueError):
+            f.seek(0)
+        with self.assertRaises(ValueError):
+            f.tell()
+        # Open the file for writing, then close it.
+        f = gzip.GzipFile(self.filename, 'w')
+        f.close()
+        with self.assertRaises(ValueError):
+            f.write(b'')
+        with self.assertRaises(ValueError):
+            f.flush()
+
     def test_append(self):
         self.test_write()
         # Append to the previous file
index f9914b7893274829dc88528c4a9976619188059a..f3bf12958660121f27b4652f3ad3dee1c0b26bd3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -48,6 +48,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #9759: GzipFile now raises ValueError when an operation is attempted
+  after the file is closed.  Patch by Jeffrey Finkelstein.
+
 - Issue #9042: Fix interaction of custom translation classes and caching in
   gettext.