Skip to content

Commit 9cc84b4

Browse files
committed
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...)
1 parent 65dae8d commit 9cc84b4

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

fsspec/implementations/memory.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ def _open(
187187
parent = self._parent(parent)
188188
if self.isfile(parent):
189189
raise FileExistsError(parent)
190-
if mode in ["rb", "ab", "r+b"]:
190+
if mode in ["rb", "ab", "r+b", "a+b"]:
191191
if path in self.store:
192192
f = self.store[path]
193-
if mode == "ab":
193+
if "a" in mode:
194194
# position at the end of file
195195
f.seek(0, 2)
196196
else:
@@ -199,8 +199,8 @@ def _open(
199199
return f
200200
else:
201201
raise FileNotFoundError(path)
202-
elif mode in {"wb", "xb"}:
203-
if mode == "xb" and self.exists(path):
202+
elif mode in {"wb", "w+b", "xb", "x+b"}:
203+
if "x" in mode and self.exists(path):
204204
raise FileExistsError
205205
m = MemoryFile(self, path, kwargs.get("data"))
206206
if not self._intrans:

0 commit comments

Comments
 (0)