diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2011-04-04 19:00:37 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-04-04 19:00:37 (GMT) |
commit | 4ec4b0c041f1045202024d0cc26c0c0767091fd0 (patch) | |
tree | 1411bbf18c80bb3e56e06ffbdd0b80fcd162355a /Lib/gzip.py | |
parent | 457cdf5e968700b85a998e848c59629affddf9b2 (diff) | |
download | cpython-4ec4b0c041f1045202024d0cc26c0c0767091fd0.zip cpython-4ec4b0c041f1045202024d0cc26c0c0767091fd0.tar.gz cpython-4ec4b0c041f1045202024d0cc26c0c0767091fd0.tar.bz2 |
Issue #10791: Implement missing method GzipFile.read1(), allowing GzipFile
to be wrapped in a TextIOWrapper. Patch by Nadeem Vawda.
Diffstat (limited to 'Lib/gzip.py')
-rw-r--r-- | Lib/gzip.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py index ba2149e..f8cd2a1 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -348,6 +348,28 @@ class GzipFile(io.BufferedIOBase): self.offset += size return chunk + def read1(self, size=-1): + self._check_closed() + if self.mode != READ: + import errno + raise IOError(errno.EBADF, "read1() on write-only GzipFile object") + + if self.extrasize <= 0 and self.fileobj is None: + return b'' + + try: + self._read() + except EOFError: + pass + if size < 0 or size > self.extrasize: + size = self.extrasize + + offset = self.offset - self.extrastart + chunk = self.extrabuf[offset: offset + size] + self.extrasize -= size + self.offset += size + return chunk + def peek(self, n): if self.mode != READ: import errno |