Skip to content

Commit 820eca9

Browse files
committed
localmemory: merge with memory
1 parent deb502c commit 820eca9

File tree

6 files changed

+127
-56
lines changed

6 files changed

+127
-56
lines changed

fsspec/implementations/localmemory.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

fsspec/implementations/memory.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,66 @@ class MemoryFileSystem(AbstractFileSystem):
2121
in memory filesystem.
2222
"""
2323

24-
store: ClassVar[dict[str, Any]] = {} # global, do not overwrite!
25-
pseudo_dirs = [""] # global, do not overwrite!
24+
global_store: ClassVar[dict[str, Any]] = {} # global, do not overwrite!
25+
global_pseudo_dirs = [""] # global, do not overwrite!
2626
protocol = "memory"
2727
root_marker = "/"
2828
_intrans = False
2929

30+
# never called
31+
# def __call__(cls, *args, **kwargs):
32+
# print("MemoryFileSystem call kwargs", kwargs)
33+
# skip = kwargs.pop("skip_instance_cache", False)
34+
# instance = super().__call__(*args, **kwargs)
35+
# if skip:
36+
# instance.store = {}
37+
# instance.pseudo_dirs = [""]
38+
# else:
39+
# instance.store = instance.global_store
40+
# instance.pseudo_dirs = instance.global_pseudo_dirs
41+
# return instance
42+
43+
# FIXME AttributeError: 'MemoryFileSystem' object has no attribute '_skip_instance_cache'
44+
# def __new__(cls, *args, **kwargs):
45+
# # print("new kwargs", kwargs)
46+
# # skip = kwargs.pop("skip_instance_cache", False)
47+
# instance = super().__new__(cls, *args, **kwargs)
48+
# # FIXME AttributeError: 'MemoryFileSystem' object has no attribute '_skip_instance_cache'
49+
# skip = instance._skip_instance_cache
50+
# print("new skip", skip)
51+
# if skip:
52+
# instance.store = {}
53+
# instance.pseudo_dirs = [""]
54+
# else:
55+
# instance.store = instance.global_store
56+
# instance.pseudo_dirs = instance.global_pseudo_dirs
57+
# return instance
58+
3059
def __init__(self, *args, **kwargs):
31-
self.logger = logger
60+
# print("MemoryFileSystem init kwargs", kwargs)
3261
super().__init__(*args, **kwargs)
62+
self.logger = logger
63+
# print("MemoryFileSystem init kwargs", kwargs)
64+
# skip = kwargs.pop("skip_instance_cache", False)
65+
# if skip:
66+
# self.store = {}
67+
# self.pseudo_dirs = [""]
68+
# local_memory = kwargs.pop("local_memory", False)
69+
# if local_memory:
70+
# skip = self._skip_instance_cache
71+
skip = kwargs.get("skip_instance_cache", False)
72+
# print("MemoryFileSystem init skip", skip)
73+
# FIXME skip is None
74+
# assert skip in (True, False)
75+
if skip:
76+
# local
77+
self.store = {}
78+
self.pseudo_dirs = [""]
79+
else:
80+
# global
81+
self.store = self.global_store
82+
self.pseudo_dirs = self.global_pseudo_dirs
83+
# super().__init__(*args, **kwargs)
3384

3485
@classmethod
3586
def _strip_protocol(cls, path):

fsspec/implementations/tests/test_localmemory.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

fsspec/implementations/tests/test_memory.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,66 @@
44
import pytest
55

66
from fsspec.implementations.local import LocalFileSystem, make_path_posix
7+
from fsspec.implementations.memory import MemoryFileSystem
8+
9+
10+
def test_identical_instances():
11+
fs1 = MemoryFileSystem()
12+
fs2 = MemoryFileSystem()
13+
14+
assert id(fs1) == id(fs2)
15+
assert id(fs1.store) == id(fs2.store)
16+
17+
fs1.touch("/fs1.txt")
18+
fs2.touch("/fs2.txt")
19+
assert fs1.ls("/", detail=False) == ["/fs1.txt", "/fs2.txt"]
20+
assert fs2.ls("/", detail=False) == ["/fs1.txt", "/fs2.txt"]
21+
22+
23+
def _clear(m):
24+
m.store.clear()
25+
m.pseudo_dirs.clear()
26+
m.pseudo_dirs.append("")
27+
28+
29+
def test_separate_instances_1_1():
30+
# fs1 = MemoryFileSystem(local_memory=True)
31+
# fs2 = MemoryFileSystem(local_memory=True)
32+
# FIXME only one param
33+
# fs1 = MemoryFileSystem(skip_instance_cache=True, local_memory=True)
34+
# fs2 = MemoryFileSystem(skip_instance_cache=True, local_memory=True)
35+
fs1 = MemoryFileSystem(skip_instance_cache=True)
36+
fs2 = MemoryFileSystem(skip_instance_cache=True)
37+
assert id(fs1) != id(fs2)
38+
assert id(fs1.store) != id(fs2.store)
39+
fs1.touch("/fs1.txt")
40+
fs2.touch("/fs2.txt")
41+
assert fs1.ls("/", detail=False) == ["/fs1.txt"]
42+
assert fs2.ls("/", detail=False) == ["/fs2.txt"]
43+
44+
45+
def test_separate_instances_1_0():
46+
fs1 = MemoryFileSystem(skip_instance_cache=True) # local
47+
fs2 = MemoryFileSystem() # global
48+
_clear(fs2)
49+
assert id(fs1) != id(fs2)
50+
assert id(fs1.store) != id(fs2.store)
51+
fs1.touch("/fs1.txt")
52+
fs2.touch("/fs2.txt")
53+
assert fs1.ls("/", detail=False) == ["/fs1.txt"] # local
54+
assert fs2.ls("/", detail=False) == ["/fs2.txt"] # global
55+
56+
57+
def test_separate_instances_0_1():
58+
fs1 = MemoryFileSystem() # global
59+
fs2 = MemoryFileSystem(skip_instance_cache=True) # local
60+
_clear(fs1)
61+
assert id(fs1) != id(fs2)
62+
assert id(fs1.store) != id(fs2.store)
63+
fs1.touch("/fs1.txt")
64+
fs2.touch("/fs2.txt")
65+
assert fs2.ls("/", detail=False) == ["/fs2.txt"] # local
66+
assert fs1.ls("/", detail=False) == ["/fs1.txt"] # global
767

868

969
def test_1(m):

fsspec/registry.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,6 @@ def register_implementation(name, cls, clobber=False, errtxt=None):
171171
"err": "LibArchive requires to be installed",
172172
},
173173
"local": {"class": "fsspec.implementations.local.LocalFileSystem"},
174-
"localmemory": {
175-
"class": "fsspec.implementations.localmemory.LocalMemoryFileSystem"
176-
},
177174
"memory": {"class": "fsspec.implementations.memory.MemoryFileSystem"},
178175
"oci": {
179176
"class": "ocifs.OCIFileSystem",

fsspec/spec.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,17 @@ def __init__(cls, *args, **kwargs):
6363
cls._pid = os.getpid()
6464

6565
def __call__(cls, *args, **kwargs):
66+
# print("_Cached call kwargs", kwargs)
6667
kwargs = apply_config(cls, kwargs)
6768
extra_tokens = tuple(
6869
getattr(cls, attr, None) for attr in cls._extra_tokenize_attributes
6970
)
7071
token = tokenize(
7172
cls, cls._pid, threading.get_ident(), *args, *extra_tokens, **kwargs
7273
)
73-
skip = kwargs.pop("skip_instance_cache", False)
74+
# skip = kwargs.pop("skip_instance_cache", False)
75+
skip = kwargs.get("skip_instance_cache", False)
76+
assert skip in (True, False)
7477
if os.getpid() != cls._pid:
7578
cls._cache.clear()
7679
cls._pid = os.getpid()
@@ -81,8 +84,12 @@ def __call__(cls, *args, **kwargs):
8184
obj = super().__call__(*args, **kwargs)
8285
# Setting _fs_token here causes some static linters to complain.
8386
obj._fs_token_ = token
87+
# no. too late. must be passed to init
88+
# obj._skip_instance_cache_ = skip
89+
# print("_Cached call skip", skip)
8490
obj.storage_args = args
8591
obj.storage_options = kwargs
92+
# setattr(obj, "_skip_instance_cache", skip)
8693
if obj.async_impl and obj.mirror_sync_methods:
8794
from .asyn import mirror_sync_methods
8895

@@ -160,6 +167,7 @@ def __init__(self, *args, **storage_options):
160167
warnings.warn("add_aliases has been removed.", FutureWarning)
161168
# This is set in _Cached
162169
self._fs_token_ = None
170+
# self._skip_instance_cache_ = None
163171

164172
@property
165173
def fsid(self):
@@ -172,6 +180,10 @@ def fsid(self):
172180
def _fs_token(self):
173181
return self._fs_token_
174182

183+
# @property
184+
# def _skip_instance_cache(self):
185+
# return self._skip_instance_cache_
186+
175187
def __dask_tokenize__(self):
176188
return self._fs_token
177189

0 commit comments

Comments
 (0)