diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-10-06 21:26:52 +0000 |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-10-06 21:26:52 +0000 |
commit | 76a66aab19b2eea153c192d2ee9658fd243f303d (patch) | |
tree | d77f6f73c8d2f72f9e7c420dcdee1bd534a936b4 /Lib/gzip.py | |
parent | 2471063dc711789193df572672763513b31ff974 (diff) | |
download | cpython-git-76a66aab19b2eea153c192d2ee9658fd243f303d.tar.gz |
Merged revisions 85291 via svnmerge from
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.
........
Diffstat (limited to 'Lib/gzip.py')
-rw-r--r-- | Lib/gzip.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py index 5fc7639f56..2bcb4dbfb0 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -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)) |