diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-10-06 21:29:56 +0000 |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-10-06 21:29:56 +0000 |
commit | e3900542e4c90c66a5a89a38a3c19dd2cf117251 (patch) | |
tree | 88f58158eae1d5002cd4dc9c37a06aa3b7604460 /Lib/gzip.py | |
parent | e5e6bc68f9c9aa127ea7ae390b1a0a7005b5d805 (diff) | |
download | cpython-e3900542e4c90c66a5a89a38a3c19dd2cf117251.tar.gz cpython-e3900542e4c90c66a5a89a38a3c19dd2cf117251.zip |
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 | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py index 6a9fbc2c1ad..8a2a7184df0 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -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): |