Context manager for acquiring a file.
(self, needs_lock: bool = True)
| 203 | |
| 204 | @contextmanager |
| 205 | def acquire_context(self, needs_lock: bool = True) -> Iterator[T_File]: |
| 206 | """Context manager for acquiring a file.""" |
| 207 | file, cached = self._acquire_with_cache_info(needs_lock) |
| 208 | try: |
| 209 | yield file |
| 210 | except Exception: |
| 211 | if not cached: |
| 212 | self.close(needs_lock) |
| 213 | raise |
| 214 | |
| 215 | def _acquire_with_cache_info(self, needs_lock: bool = True) -> tuple[T_File, bool]: |
| 216 | """Acquire a file, returning the file and whether it was cached.""" |