From b07d4059d3649e97a2159819372718176d66868f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 11 Oct 2024 19:01:02 +0530 Subject: [PATCH] Ensure output rgba data file is unlinked on error --- kitty/render_cache.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/kitty/render_cache.py b/kitty/render_cache.py index 1ca969a04..2d1bccda6 100644 --- a/kitty/render_cache.py +++ b/kitty/render_cache.py @@ -67,12 +67,17 @@ class ImageRenderCache: def render_image(self, src_path: str, output_path: str) -> None: import stat import subprocess - with open(src_path, 'rb') as src, open(output_path, 'wb', opener=partial(os.open, mode=stat.S_IREAD | stat.S_IWRITE)) as output: - cp = subprocess.run([kitten_exe(), '__convert_image__', 'RGBA'], stdin=src, stdout=output, stderr=subprocess.PIPE) - if cp.returncode != 0: - raise ValueError(f'Failed to convert {src_path} to RGBA data with error: {cp.stderr.decode("utf-8", "replace")}') - if output.seek(0, os.SEEK_END) < 8: - raise ValueError(f'Failed to convert {src_path} to RGBA data, no output written. stderr: {cp.stderr.decode("utf-8", "replace")}') + try: + with open(src_path, 'rb') as src, open(output_path, 'wb', opener=partial(os.open, mode=stat.S_IREAD | stat.S_IWRITE)) as output: + cp = subprocess.run([kitten_exe(), '__convert_image__', 'RGBA'], stdin=src, stdout=output, stderr=subprocess.PIPE) + if cp.returncode != 0: + raise ValueError(f'Failed to convert {src_path} to RGBA data with error: {cp.stderr.decode("utf-8", "replace")}') + if output.seek(0, os.SEEK_END) < 8: + raise ValueError(f'Failed to convert {src_path} to RGBA data, no output written. stderr: {cp.stderr.decode("utf-8", "replace")}') + except Exception: + with suppress(Exception): + os.unlink(output_path) + raise def read_metadata(self, output_path: str) -> tuple[int, int, int]: with open(output_path, 'rb') as f: