Read the cache if it exists and is well-formed. If it is not well-formed, the call to write later should resolve the issue.
(cls, mode: Mode)
| 60 | |
| 61 | @classmethod |
| 62 | def read(cls, mode: Mode) -> Self: |
| 63 | class="st">"""Read the cache if it exists and is well-formed. |
| 64 | |
| 65 | If it is not well-formed, the call to write later should |
| 66 | resolve the issue. |
| 67 | class="st">""" |
| 68 | cache_file = get_cache_file(mode) |
| 69 | try: |
| 70 | exists = cache_file.exists() |
| 71 | except OSError as e: |
| 72 | class="cm"># Likely file too long; see #4172 and #4174 |
| 73 | err(fclass="st">"Unable to read cache file {cache_file} due to {e}") |
| 74 | return cls(mode, cache_file) |
| 75 | if not exists: |
| 76 | return cls(mode, cache_file) |
| 77 | |
| 78 | with cache_file.open(class="st">"rb") as fobj: |
| 79 | try: |
| 80 | data: dict[str, tuple[float, int, str]] = pickle.load(fobj) |
| 81 | file_data = {k: FileData(*v) for k, v in data.items()} |
| 82 | except (pickle.UnpicklingError, ValueError, IndexError): |
| 83 | return cls(mode, cache_file) |
| 84 | |
| 85 | return cls(mode, cache_file, file_data) |
| 86 | |
| 87 | @staticmethod |
| 88 | def hash_digest(path: Path) -> str: |