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
11 changes: 6 additions & 5 deletions obstore/python/obstore/fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import warnings
from collections import defaultdict
from functools import cached_property, lru_cache
from io import UnsupportedOperation
from pathlib import Path
from typing import TYPE_CHECKING, Literal, overload
from urllib.parse import urlparse
Expand Down Expand Up @@ -721,7 +722,7 @@ def read(self, length: int = -1) -> bytes:

"""
if self.mode != "rb":
raise ValueError("File not in read mode")
raise UnsupportedOperation("File not in read mode")
if length < 0:
length = self.size - self.tell()
if self.closed:
Expand All @@ -736,15 +737,15 @@ def read(self, length: int = -1) -> bytes:
def readline(self) -> bytes:
"""Read until first occurrence of newline character."""
if self.mode != "rb":
raise ValueError("File not in read mode")
raise UnsupportedOperation("File not in read mode")

out = self._reader.readline()
return out.to_bytes()

def readlines(self) -> list[bytes]:
"""Return all data, split by the newline character."""
if self.mode != "rb":
raise ValueError("File not in read mode")
raise UnsupportedOperation("File not in read mode")

out = self._reader.readlines()
return [b.to_bytes() for b in out]
Expand Down Expand Up @@ -775,7 +776,7 @@ def seek(self, loc: int, whence: int = 0) -> int:

"""
if self.mode != "rb":
raise ValueError("Seek only available in read mode.")
raise UnsupportedOperation("Seek only available in read mode.")

return self._reader.seek(loc, whence)

Expand All @@ -787,7 +788,7 @@ def write(self, data: bytes) -> int:

"""
if not self.writable():
raise ValueError("File not in write mode")
raise UnsupportedOperation("File not in write mode")
if self.closed:
raise ValueError("I/O operation on closed file.")
if self.forced:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import gc
import os
import zipfile
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -172,6 +173,24 @@ def test_buffered_file_forwards_size_to_open_reader():
assert len(data) == 500


def test_zipfile_write_mode():
"""Ensure a write-mode file can fall back to ZipFile non-seekable code path."""
register("memory")
fs: FsspecStore = fsspec.filesystem("memory")

with (
fs._open("archive.zip", mode="wb") as filelike,
zipfile.ZipFile(filelike, "w") as zf,
):
zf.writestr("hello.txt", "hello world")

with (
fs._open("archive.zip") as filelike,
zipfile.ZipFile(filelike) as zf,
):
assert zf.read("hello.txt") == b"hello world"


def test_construct_store_cache_diff_bucket_name(
minio_bucket: tuple[S3Config, ClientConfig],
):
Expand Down
Loading