diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-29 10:49:46 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-29 10:49:46 (GMT) |
commit | c3ed2e7f8326d703ed971220a7eb76317341efbf (patch) | |
tree | d7bfa945b55a990f608aa5ccb9b8a3caaac2a02d /Lib | |
parent | 4c2e4fa242d2567e6f0bb5d112cc4e3c5d085b68 (diff) | |
download | cpython-c3ed2e7f8326d703ed971220a7eb76317341efbf.zip cpython-c3ed2e7f8326d703ed971220a7eb76317341efbf.tar.gz cpython-c3ed2e7f8326d703ed971220a7eb76317341efbf.tar.bz2 |
Issue #9962: GzipFile now has the peek() method.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/gzip.py | 25 | ||||
-rw-r--r-- | Lib/test/test_gzip.py | 22 |
2 files changed, 46 insertions, 1 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py index 3edc839..58e866b 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -204,7 +204,10 @@ class GzipFile(io.BufferedIOBase): return self.name def __repr__(self): - s = repr(self.fileobj) + fileobj = self.fileobj + if isinstance(fileobj, _PaddedFile): + fileobj = fileobj.file + s = repr(fileobj) return '<gzip ' + s[1:-1] + ' ' + hex(id(self)) + '>' def _init_write(self, filename): @@ -336,6 +339,26 @@ class GzipFile(io.BufferedIOBase): self.offset += size return chunk + def peek(self, n): + if self.mode != READ: + import errno + raise IOError(errno.EBADF, "read() on write-only GzipFile object") + + # Do not return ridiculously small buffers + if n < 100: + n = 100 + if self.extrasize == 0: + if self.fileobj is None: + return b'' + try: + self._read(max(self.max_read_chunk, n)) + except EOFError: + pass + offset = self.offset - self.extrastart + remaining = self.extrasize + assert remaining == len(self.extrabuf) - offset + return self.extrabuf[offset:offset + n] + def _unread(self, buf): self.extrasize = len(buf) + self.extrasize self.offset -= len(buf) diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index e49fe00..8e493b5 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -286,6 +286,28 @@ class TestGzip(unittest.TestCase): with gzip.GzipFile(fileobj=buf, mode="rb") as f: self.assertEqual(f.read(), uncompressed) + def test_peek(self): + uncompressed = data1 * 200 + with gzip.GzipFile(self.filename, "wb") as f: + f.write(uncompressed) + + def sizes(): + while True: + for n in range(5, 50, 10): + yield n + + with gzip.GzipFile(self.filename, "rb") as f: + f.max_read_chunk = 33 + nread = 0 + for n in sizes(): + s = f.peek(n) + if s == b'': + break + self.assertEqual(f.read(len(s)), s) + nread += len(s) + self.assertEqual(f.read(100), b'') + self.assertEqual(nread, len(uncompressed)) + # Testing compress/decompress shortcut functions def test_compress(self): |