bpo-37523: Raise ValueError for I/O operations on a closed zipfile.ZipExtFile. (GH-14658)

Raises ValueError when calling the following on a closed zipfile.ZipExtFile: read, readable, seek, seekable, tell.
This commit is contained in:
Daniel Hillier 2019-11-30 19:30:47 +11:00 committed by Serhiy Storchaka
parent 1df65f7c6c
commit 8d62df60d8
3 changed files with 25 additions and 0 deletions

View file

@ -889,12 +889,16 @@ class ZipExtFile(io.BufferedIOBase):
return self._readbuffer[self._offset: self._offset + 512]
def readable(self):
if self.closed:
raise ValueError("I/O operation on closed file.")
return True
def read(self, n=-1):
"""Read and return up to n bytes.
If the argument is omitted, None, or negative, data is read and returned until EOF is reached.
"""
if self.closed:
raise ValueError("read from closed file.")
if n is None or n < 0:
buf = self._readbuffer[self._offset:]
self._readbuffer = b''
@ -1031,9 +1035,13 @@ class ZipExtFile(io.BufferedIOBase):
super().close()
def seekable(self):
if self.closed:
raise ValueError("I/O operation on closed file.")
return self._seekable
def seek(self, offset, whence=0):
if self.closed:
raise ValueError("seek on closed file.")
if not self._seekable:
raise io.UnsupportedOperation("underlying stream is not seekable")
curr_pos = self.tell()
@ -1082,6 +1090,8 @@ class ZipExtFile(io.BufferedIOBase):
return self.tell()
def tell(self):
if self.closed:
raise ValueError("tell on closed file.")
if not self._seekable:
raise io.UnsupportedOperation("underlying stream is not seekable")
filepos = self._orig_file_size - self._left - len(self._readbuffer) + self._offset