From 8ab8cdc815872b35d30ce43e945a71daa8ac9b78 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 9 May 2023 22:36:33 +1000 Subject: [PATCH] Remove temporary file when error is raised --- src/PIL/EpsImagePlugin.py | 7 +++++++ src/PIL/ImageGrab.py | 2 ++ src/PIL/JpegImagePlugin.py | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/src/PIL/EpsImagePlugin.py b/src/PIL/EpsImagePlugin.py index 1c88d22c749..bdac874c44f 100644 --- a/src/PIL/EpsImagePlugin.py +++ b/src/PIL/EpsImagePlugin.py @@ -134,6 +134,13 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False): if gs_windows_binary is not None: if not gs_windows_binary: + try: + os.unlink(outfile) + if infile_temp: + os.unlink(infile_temp) + except OSError: + pass + msg = "Unable to locate Ghostscript on paths" raise OSError(msg) command[0] = gs_windows_binary diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 2592ba2df3d..05385e36cba 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -144,6 +144,8 @@ def grabclipboard(): err = subprocess.run(args, stdout=fh, stderr=subprocess.PIPE).stderr os.close(fh) if err: + os.unlink(filepath) + msg = f"{args[0]} error: {err.strip().decode()}" raise ChildProcessError(msg) im = Image.open(filepath) diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 5dd1a61afe1..dfc7e6e9f56 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -457,6 +457,11 @@ def load_djpeg(self): if os.path.exists(self.filename): subprocess.check_call(["djpeg", "-outfile", path, self.filename]) else: + try: + os.unlink(path) + except OSError: + pass + msg = "Invalid Filename" raise ValueError(msg)