From ce46cc18dc1bdee925398b5cba022509d5ec917a Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Wed, 1 Apr 2026 20:12:24 -0700 Subject: [PATCH] [tmpfile] delete the temporary file if an error occurs --- masque/file/utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/masque/file/utils.py b/masque/file/utils.py index 25bc61d..ebf5fb3 100644 --- a/masque/file/utils.py +++ b/masque/file/utils.py @@ -145,7 +145,11 @@ def tmpfile(path: str | pathlib.Path) -> Iterator[IO[bytes]]: path = pathlib.Path(path) suffixes = ''.join(path.suffixes) with tempfile.NamedTemporaryFile(suffix=suffixes, delete=False) as tmp_stream: - yield tmp_stream + try: + yield tmp_stream + except Exception: + pathlib.Path(tmp_stream.name).unlink(missing_ok=True) + raise try: shutil.move(tmp_stream.name, path)