Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions c-ext/compressionreader.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ static PyObject *compressionreader_seekable(ZstdCompressionReader *self) {
Py_RETURN_FALSE;
}

static PyObject *compressionreader_seek(PyObject *self, PyObject *args) {
PyErr_SetString(PyExc_OSError, "stream is not seekable");
return NULL;
}

static PyObject *compressionreader_readline(PyObject *self, PyObject *args) {
set_io_unsupported_operation();
return NULL;
Expand Down Expand Up @@ -760,6 +765,8 @@ static PyMethodDef compressionreader_methods[] = {
PyDoc_STR("Not implemented")},
{"readlines", (PyCFunction)compressionreader_readlines, METH_VARARGS,
PyDoc_STR("Not implemented")},
{"seek", (PyCFunction)compressionreader_seek, METH_VARARGS,
PyDoc_STR("Raises OSError")},
{"seekable", (PyCFunction)compressionreader_seekable, METH_NOARGS,
PyDoc_STR("Returns False")},
{"tell", (PyCFunction)compressionreader_tell, METH_NOARGS,
Expand Down
4 changes: 4 additions & 0 deletions rust-ext/src/compression_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ impl ZstdCompressionReader {
false
}

fn seek(&self, _data: &Bound<'_, PyAny>) -> PyResult<()> {
Err(PyOSError::new_err("stream is not seekable"))
}

fn readline(&self, py: Python) -> PyResult<()> {
let io = py.import("io")?;
let exc = io.getattr("UnsupportedOperation")?;
Expand Down
14 changes: 14 additions & 0 deletions tests/test_compressor_stream_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ def test_not_implemented(self):
with self.assertRaises(OSError):
reader.write(b"foo")

def test_seek(self):
cctx = zstd.ZstdCompressor()

with cctx.stream_reader(b"foo" * 60) as reader:
with self.assertRaises(OSError):
reader.seek(0)

reader.read(1)
with self.assertRaises(OSError):
reader.seek(0)

with self.assertRaises(OSError):
reader.seek(0)

def test_constant_methods(self):
cctx = zstd.ZstdCompressor()

Expand Down
1 change: 1 addition & 0 deletions zstandard/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class ZstdCompressionReader(BinaryIO):
def readable(self) -> bool: ...
def writable(self) -> bool: ...
def seekable(self) -> bool: ...
def seek(self, pos: int, whence: int = ...) -> int: ...
def readline(self, limit: int = ...) -> bytes: ...
def readlines(self, hint: int = ...) -> List[bytes]: ...
def write(self, data: ByteString): ...
Expand Down
3 changes: 3 additions & 0 deletions zstandard/backend_cffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,9 @@ def writable(self):
def seekable(self):
return False

def seek(self, offset, whence=None):
raise io.UnsupportedOperation()

def readline(self):
raise io.UnsupportedOperation()

Expand Down