Load cache from JSON file, return empty dict if file doesn't exist. Uses try-except instead of exists() check to avoid TOCTOU race condition. This is the same pattern used in meson_runner.py for test_list_cache. Returns: Dictionary mapping file paths to
(self)
| 76 | self.cache = self._load_cache() |
| 77 | |
| 78 | def _load_cache(self) -> dict[str, CacheEntry]: |
| 79 | """ |
| 80 | Load cache from JSON file, return empty dict if file doesn't exist. |
| 81 | |
| 82 | Uses try-except instead of exists() check to avoid TOCTOU race condition. |
| 83 | This is the same pattern used in meson_runner.py for test_list_cache. |
| 84 | |
| 85 | Returns: |
| 86 | Dictionary mapping file paths to cache entries |
| 87 | """ |
| 88 | try: |
| 89 | with open(self.cache_file, "r") as f: |
| 90 | data = json.load(f) |
| 91 | |
| 92 | # Convert JSON dict to CacheEntry objects |
| 93 | cache: dict[str, CacheEntry] = {} |
| 94 | for file_path, entry_data in data.items(): |
| 95 | cache[file_path] = CacheEntry( |
| 96 | modification_time=entry_data["modification_time"], |
| 97 | md5_hash=entry_data["md5_hash"], |
| 98 | ) |
| 99 | return cache |
| 100 | except FileNotFoundError: |
| 101 | # Cache file doesn't exist yet - start fresh |
| 102 | return {} |
| 103 | except (json.JSONDecodeError, KeyError, TypeError, IOError, OSError): |
| 104 | # Cache corrupted or inaccessible - start fresh |
| 105 | return {} |
| 106 | |
| 107 | def _save_cache(self) -> None: |
| 108 | """Save current cache state to JSON file.""" |
no test coverage detected