indygreg / python-zstandard

Python bindings to the Zstandard (zstd) compression library
BSD 3-Clause "New" or "Revised" License
481 stars 84 forks source link

Return True from ZstdDecompressionReader.seekable #222

Open hauntsaninja opened 2 months ago

hauntsaninja commented 2 months ago

There are several issues on this tracker discussing python-zstandard's approach to .seek, summarised by this comment: https://github.com/indygreg/python-zstandard/issues/216#issuecomment-2005651108

But it sounds like python-zstandard is still happy with forward seeks. If so, we should return True from seekable, so that e.g. we can forward seek through io.BufferedReader: https://github.com/python/cpython/blob/dd0a1f9da283bd784e2c88efec0a45cef978516a/Modules/_io/bufferedio.c#L1227

Here's a small repro:

import zstandard
fn = "whatever.zst"
unbuffered = zstandard.open(fn)
unbuffered.seek(10)
unbuffered.read(10)  # works fine

buffered = io.BufferedReader(zstandard.open(fn))
buffered.seek(10)  # fails
# UnsupportedOperation: File or stream is not seekable.

It's useful to wrap with BufferedReader to get functionality like readline (I noticed you recommended doing this here as well :-) https://github.com/indygreg/python-zstandard/issues/13#issuecomment-466797822 )