Acquire a file, returning the file and whether it was cached.
(self, needs_lock: bool = True)
| 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.""" |
| 217 | with self._optional_lock(needs_lock): |
| 218 | try: |
| 219 | file = self._cache[self._key] |
| 220 | except KeyError: |
| 221 | kwargs = self._kwargs |
| 222 | if self._mode is not _OMIT_MODE: |
| 223 | kwargs = kwargs.copy() |
| 224 | kwargs["mode"] = self._mode |
| 225 | file = self._opener(*self._args, **kwargs) |
| 226 | if self._mode == "w": |
| 227 | # ensure file doesn't get overridden when opened again |
| 228 | self._mode = "a" |
| 229 | self._cache[self._key] = file |
| 230 | return file, False |
| 231 | else: |
| 232 | return file, True |
| 233 | |
| 234 | def close(self, needs_lock: bool = True) -> None: |
| 235 | """Explicitly close any associated file object (if necessary).""" |
no test coverage detected