Skip to content

Commit

Permalink
Fix inconsistent file mode formatting and comparison (rb+ changed t…
Browse files Browse the repository at this point in the history
…o `r+b`) (#1426)
  • Loading branch information
lobis authored Nov 14, 2023
1 parent 05686da commit 70be2bf
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion fsspec/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions fsspec/implementations/cached.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion fsspec/implementations/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion fsspec/implementations/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
9 changes: 9 additions & 0 deletions fsspec/implementations/tests/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 70be2bf

Please sign in to comment.