]> granicus.if.org Git - python/commitdiff
Merged revisions 85291 via svnmerge from
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 6 Oct 2010 21:29:56 +0000 (21:29 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 6 Oct 2010 21:29:56 +0000 (21:29 +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 6a9fbc2c1addb26c4cf7ad2c3d8c92d83835ff9a..8a2a7184df0ac18bb36520410f45174e8a2e6573 100644 (file)
@@ -129,6 +129,7 @@ class GzipFile:
         self.fileobj = fileobj
         self.offset = 0
         self.mtime = mtime
+        self.closed = False
 
         if self.mode == WRITE:
             self._write_gzip_header()
@@ -145,6 +146,13 @@ class GzipFile:
         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(b"") & 0xffffffff
@@ -215,6 +223,7 @@ class GzipFile:
 
 
     def write(self,data):
+        self._check_closed()
         if self.mode != WRITE:
             import errno
             raise IOError(errno.EBADF, "write() on read-only GzipFile object")
@@ -228,6 +237,7 @@ class GzipFile:
             self.offset += 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")
@@ -349,6 +359,7 @@ class GzipFile:
         if self.myfileobj:
             self.myfileobj.close()
             self.myfileobj = None
+        self.closed = True
 
     def __del__(self):
         try:
@@ -360,6 +371,7 @@ class GzipFile:
         self.close()
 
     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))
@@ -377,6 +389,7 @@ class GzipFile:
         return False
 
     def tell(self):
+        self._check_closed()
         return self.offset
 
     def rewind(self):
index d92863532fad3ada13c5a000129a8582708665f7..a4a7e24480218878a6207245020f249c2eaecb20 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 134232c62fd28f01ac5eb2addb86b322f22d4650..ec6d9f9bdff4c1fe72cb6f910400ba4905c5f6cd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -122,6 +122,9 @@ C-API
 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.