diff options
-rw-r--r-- | qutebrowser/utils/utils.py | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index 5e4c418ff..a52c5fada 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -192,6 +192,21 @@ def _resource_path(filename: str) -> pathlib.Path: return importlib_resources.files(qutebrowser) / filename +@contextlib.contextmanager +def _resource_keyerror_workaround() -> Iterator[None]: + """Re-raise KeyErrors as FileNotFoundErrors. + + WORKAROUND for zipfile.Path resources raising KeyError when a file was notfound: + https://bugs.python.org/issue43063 + + Only needed for Python 3.8 and 3.9. + """ + try: + yield + except KeyError as e: + raise FileNotFoundError(str(e)) + + def _glob_resources( resource_path: pathlib.Path, subdir: str, @@ -240,7 +255,8 @@ def read_file(filename: str) -> str: return _resource_cache[filename] path = _resource_path(filename) - return path.read_text(encoding='utf-8') + with _resource_keyerror_workaround(): + return path.read_text(encoding='utf-8') def read_file_binary(filename: str) -> bytes: @@ -253,7 +269,8 @@ def read_file_binary(filename: str) -> bytes: The file contents as a bytes object. """ path = _resource_path(filename) - return path.read_bytes() + with _resource_keyerror_workaround(): + return path.read_bytes() def parse_version(version: str) -> VersionNumber: |