diff --git a/fsspec/caching.py b/fsspec/caching.py index 2812724fc..8d441fe46 100644 --- a/fsspec/caching.py +++ b/fsspec/caching.py @@ -111,7 +111,7 @@ def _makefile(self) -> mmap.mmap | bytearray: fd.write(b"1") fd.flush() else: - fd = open(self.location, "rb+") + fd = open(self.location, "r+b") return mmap.mmap(fd.fileno(), self.size) diff --git a/fsspec/implementations/cached.py b/fsspec/implementations/cached.py index 89fab0791..eef916bb2 100644 --- a/fsspec/implementations/cached.py +++ b/fsspec/implementations/cached.py @@ -771,10 +771,10 @@ def __init__(self, fs, path, fn=None, mode="wb", autocommit=True, seek=0): self.autocommit = autocommit def __reduce__(self): - # always open in rb+ to allow continuing writing at a location + # always open in r+b to allow continuing writing at a location return ( LocalTempFile, - (self.fs, self.path, self.fn, "rb+", self.autocommit, self.tell()), + (self.fs, self.path, self.fn, "r+b", self.autocommit, self.tell()), ) def __enter__(self): diff --git a/fsspec/implementations/http.py b/fsspec/implementations/http.py index cdd84c5ce..7b249bd94 100644 --- a/fsspec/implementations/http.py +++ b/fsspec/implementations/http.py @@ -802,7 +802,7 @@ async def get_range(session, url, start, end, file=None, **kwargs): async with r: out = await r.read() if file: - with open(file, "rb+") as f: + with open(file, "r+b") as f: f.seek(start) f.write(out) else: diff --git a/fsspec/implementations/memory.py b/fsspec/implementations/memory.py index 32daaf0f1..4022ff13a 100644 --- a/fsspec/implementations/memory.py +++ b/fsspec/implementations/memory.py @@ -175,7 +175,7 @@ def _open( parent = self._parent(parent) if self.isfile(parent): raise FileExistsError(parent) - if mode in ["rb", "ab", "rb+"]: + if mode in ["rb", "ab", "r+b"]: if path in self.store: f = self.store[path] if mode == "ab": diff --git a/fsspec/implementations/tests/test_memory.py b/fsspec/implementations/tests/test_memory.py index 05a40b287..038d72eaa 100644 --- a/fsspec/implementations/tests/test_memory.py +++ b/fsspec/implementations/tests/test_memory.py @@ -191,6 +191,15 @@ def test_seekable(m): assert f.tell() == 2 +# https://github.com/fsspec/filesystem_spec/issues/1425 +@pytest.mark.parametrize("mode", ["r", "rb", "w", "wb", "ab", "r+b"]) +def test_open_mode(m, mode): + filename = "mode.txt" + m.touch(filename) + with m.open(filename, mode=mode) as _: + pass + + def test_remove_all(m): m.touch("afile") m.rm("/", recursive=True)