summaryrefslogtreecommitdiff
path: root/Lib/gzip.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-10-06 21:21:18 +0000
committerAntoine Pitrou <solipsis@pitrou.net>2010-10-06 21:21:18 +0000
commitbb17589eb306ff4ac1ce6c7e76306b34576a61da (patch)
treecb65843c40e29630ac98da085d79bc3e54a9d780 /Lib/gzip.py
parent4022bc5f8d0a2a1fa462ccd6068853075d849e16 (diff)
downloadcpython-bb17589eb306ff4ac1ce6c7e76306b34576a61da.tar.gz
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.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py
index 7c3fd5157e..21b140498f 100644
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -210,6 +210,13 @@ class GzipFile(io.BufferedIOBase):
s = repr(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
@@ -288,6 +295,7 @@ class GzipFile(io.BufferedIOBase):
self._add_read_data(uncompress)
def write(self,data):
+ self._check_closed()
if self.mode != WRITE:
import errno
raise IOError(errno.EBADF, "write() on read-only GzipFile object")
@@ -308,6 +316,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")
@@ -457,6 +466,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))