From 9cc84b4b3ad324c9fb81d124cb9d17c48f7d4cff Mon Sep 17 00:00:00 2001 From: Oren Laadan Date: Sun, 28 Sep 2025 10:57:57 +0800 Subject: [PATCH] Add missing open-modes for memory filesystem (fixes #1921) Memory filesystem's open() method supports some - but not all - file modes. Specifically it supports "r+b" but not "a+b", "w+b", and "x+b". Add them. (It also doesn't respect read-only/write-only properties; but the fix is more involved and not (yet?) in demand; left as an exercise to the reader...) --- fsspec/implementations/memory.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fsspec/implementations/memory.py b/fsspec/implementations/memory.py index 838876d1f..f6b67bbc8 100644 --- a/fsspec/implementations/memory.py +++ b/fsspec/implementations/memory.py @@ -187,10 +187,10 @@ def _open( parent = self._parent(parent) if self.isfile(parent): raise FileExistsError(parent) - if mode in ["rb", "ab", "r+b"]: + if mode in ["rb", "ab", "r+b", "a+b"]: if path in self.store: f = self.store[path] - if mode == "ab": + if "a" in mode: # position at the end of file f.seek(0, 2) else: @@ -199,8 +199,8 @@ def _open( return f else: raise FileNotFoundError(path) - elif mode in {"wb", "xb"}: - if mode == "xb" and self.exists(path): + elif mode in {"wb", "w+b", "xb", "x+b"}: + if "x" in mode and self.exists(path): raise FileExistsError m = MemoryFile(self, path, kwargs.get("data")) if not self._intrans: