Skip to content

Commit 9c40327

Browse files
committed
Fix test failures caused by inotify changes
- Restore _update_with_defaults calls for proper parameter handling - Fix cache directory and separate_files parameter handling - Restore original CacheChangeHandler behavior - Make polling fallback more conservative (only for inotify-specific errors) - All pickle core tests now pass locally
1 parent 35d52a7 commit 9c40327

1 file changed

Lines changed: 31 additions & 36 deletions

File tree

src/cachier/cores/pickle.py

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,44 @@ class _PickleCore(_BaseCore):
2828
"""The pickle core class for cachier."""
2929

3030
class CacheChangeHandler(PatternMatchingEventHandler):
31-
"""Handler for cache file changes."""
31+
"""Handles cache-file modification events."""
3232

3333
def __init__(self, filename, core, key):
34-
super().__init__(
35-
patterns=[f"*{filename}*"],
36-
ignore_patterns=[],
37-
ignore_directories=False,
34+
PatternMatchingEventHandler.__init__(
35+
self,
36+
patterns=["*" + filename],
37+
ignore_patterns=None,
38+
ignore_directories=True,
39+
case_sensitive=False,
3840
)
39-
self.filename = filename
4041
self.core = core
4142
self.key = key
42-
self.value = None
4343
self.observer = None
44+
self.value = None
4445

4546
def inject_observer(self, observer) -> None:
46-
"""Inject the observer instance."""
47+
"""Inject the observer running this handler."""
4748
self.observer = observer
4849

4950
def _check_calculation(self) -> None:
50-
"""Check if calculation is complete."""
51+
entry = self.core.get_entry_by_key(self.key, True)[1]
5152
try:
52-
if self.core.separate_files:
53-
entry = self.core._load_cache_by_key(self.key)
54-
else:
55-
with self.core.lock:
56-
entry = self.core.get_cache_dict().get(self.key)
57-
if entry and not entry._processing:
53+
if not entry._processing:
54+
# print('stopping observer!')
5855
self.value = entry.value
59-
if self.observer:
60-
self.observer.stop()
61-
except Exception:
62-
pass
56+
self.observer.stop()
57+
# else:
58+
# print('NOT stopping observer... :(')
59+
except AttributeError: # catching entry being None
60+
self.value = None
61+
self.observer.stop()
6362

6463
def on_created(self, event) -> None:
65-
"""Handle file creation events."""
66-
self._check_calculation()
64+
"""A Watchdog Event Handler method.""" # noqa: D401
65+
self._check_calculation() # pragma: no cover
6766

6867
def on_modified(self, event) -> None:
69-
"""Handle file modification events."""
68+
"""A Watchdog Event Handler method.""" # noqa: D401
7069
self._check_calculation()
7170

7271
def __init__(
@@ -77,16 +76,15 @@ def __init__(
7776
separate_files: Optional[bool],
7877
wait_for_calc_timeout: Optional[int],
7978
):
80-
super().__init__(
81-
hash_func=hash_func,
82-
wait_for_calc_timeout=wait_for_calc_timeout,
79+
super().__init__(hash_func, wait_for_calc_timeout)
80+
self._cache_dict: Dict[str, CacheEntry] = {}
81+
self.reload = _update_with_defaults(pickle_reload, "pickle_reload")
82+
self.cache_dir = os.path.expanduser(
83+
_update_with_defaults(cache_dir, "cache_dir")
84+
)
85+
self.separate_files = _update_with_defaults(
86+
separate_files, "separate_files"
8387
)
84-
self.cache_dir = str(cache_dir) if cache_dir else "~/.cachier"
85-
self.cache_dir = os.path.expanduser(self.cache_dir)
86-
os.makedirs(self.cache_dir, exist_ok=True)
87-
self.separate_files = separate_files
88-
self.reload = pickle_reload
89-
self._cache_dict: Optional[Dict[str, CacheEntry]] = None
9088
self._cache_used_fpath = ""
9189
# Observer cache to prevent inotify instance exhaustion
9290
self._observer_cache: Dict[str, Observer] = {}
@@ -314,9 +312,6 @@ def wait_on_entry_calc(self, key: str) -> Any:
314312
return self._wait_with_polling(key)
315313
else:
316314
raise
317-
except Exception:
318-
# For any other exception, fall back to polling
319-
return self._wait_with_polling(key)
320315

321316
def _wait_with_inotify(self, key: str, filename: str) -> Any:
322317
"""Wait for calculation using inotify (original method with fixes)."""
@@ -367,8 +362,8 @@ def _wait_with_polling(self, key: str) -> Any:
367362
return entry.value
368363

369364
self.check_calc_timeout(time_spent)
370-
except Exception:
371-
# Continue polling even if there are errors
365+
except (FileNotFoundError, EOFError):
366+
# Continue polling even if there are file errors
372367
pass
373368

374369
def clear_cache(self) -> None:

0 commit comments

Comments
 (0)