diff --git a/Tests/test_image.py b/Tests/test_image.py index 2cd858df16f..5127feb9358 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -652,6 +652,15 @@ def test_no_resource_warning_on_save(self, tmp_path): with warnings.catch_warnings(): im.save(temp_file) + def test_no_new_file_on_error(self, tmp_path): + temp_file = str(tmp_path / "temp.jpg") + + im = Image.new("RGB", (0, 0)) + with pytest.raises(SystemError): + im.save(temp_file) + + assert not os.path.exists(temp_file) + def test_load_on_nonexclusive_multiframe(self): with open("Tests/images/frozenpond.mpo", "rb") as fp: diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 8213f79b1f0..dc44f7e1790 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2287,6 +2287,7 @@ def save(self, fp, format=None, **params): save_handler = SAVE[format.upper()] if open_fp: + created = not os.path.exists(filename) if params.get("append", False): # Open also for reading ("+"), because TIFF save_all # writer needs to go back and edit the written data. @@ -2296,10 +2297,17 @@ def save(self, fp, format=None, **params): try: save_handler(self, fp, filename) - finally: - # do what we can to clean up + except Exception: if open_fp: fp.close() + if created: + try: + os.remove(filename) + except PermissionError: + pass + raise + if open_fp: + fp.close() def seek(self, frame): """