Skip to content

MemoryFilesSstem missing valid modes for open() #1921

@orenl

Description

@orenl
import fsspec

fs = fsspec.filesystem('memory')

with fs.open('hello', 'w+b') as f:
    f.write(b'world')

results in:

$ python3 /tmp/filemode.py
Traceback (most recent call last):
  File "/tmp/try2.py", line 5, in <module>
    with fs.open('hello', 'w+b') as f:
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "virtualenvs/app-tNTrAeIV-py3.12/lib/python3.12/site-packages/fsspec/spec.py", line 1338, in open
    f = self._open(
        ^^^^^^^^^^^
  File "virtualenvs/app-tNTrAeIV-py3.12/lib/python3.12/site-packages/fsspec/implementations/memory.py", line 211, in _open
    raise ValueError(f"unsupported file mode for {name}: {mode!r}")
ValueError: unsupported file mode for MemoryFileSystem: 'w+b'

The open() method should respect all valid modes, including "w+b", "a+b", "x+b".
Will follow up with a PR to fix this behavior.

Note also that the memory filesystem does not repsect "read-only"/"write-only" properties; However fixing this is more involved change, and may impact backward compatibilty for users that rely on such (broken) behavior, and nobody complained so far.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions