[issue33173] GzipFile's .seekable() returns True even if underlying buffer is not seekable

Martin Panter report at bugs.python.org
Sat Oct 6 02:03:22 EDT 2018


Martin Panter <vadmium+py at gmail.com> added the comment:

If a change is made, it would be nice to bring the “gzip”, “bzip” and LZMA modules closer together. The current “bzip” and LZMA modules rely on the underlying “seekable” method without a fallback implementation, but also have a check for read mode.

I think the seeking functionality in these modules is a misfeature. But since it is already here, it is probably best to leave it alone, and just document it.

My comment about making “seekable” stricter is at <https://bugs.python.org/review/23529/diff/14296/Lib/gzip.py#oldcode550>. Even if the underlying stream is not seekable, GzipFile can still fast-forward. Here is a demonstration:

>>> z = BytesIO(bytes.fromhex(
...     "1F8B08000000000002FFF348CD29D051F05448CC55282E294DCE56C8CC53485448AFCA"
...     "2C5048CBCC490500F44BF0A01F000000"
... ))
>>> def seek(*args): raise UnsupportedOperation()
... 
>>> z.seek = seek  # Make the underlying stream not seekable
>>> f = GzipFile(fileobj=z)
>>> f.read(10)
b'Help, I am'
>>> f.seek(20)  # Fast forward
20
>>> f.read()
b'a gzip file'
>>> f.seek(0)  # Rewind
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/proj/python/cpython/Lib/gzip.py", line 368, in seek
    return self._buffer.seek(offset, whence)
  File "/home/proj/python/cpython/Lib/_compression.py", line 137, in seek
    self._rewind()
  File "/home/proj/python/cpython/Lib/gzip.py", line 515, in _rewind
    super()._rewind()
  File "/home/proj/python/cpython/Lib/_compression.py", line 115, in _rewind
    self._fp.seek(0)
  File "/home/proj/python/cpython/Lib/gzip.py", line 105, in seek
    return self.file.seek(off)
  File "<stdin>", line 1, in seek
io.UnsupportedOperation

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33173>
_______________________________________


More information about the Python-bugs-list mailing list