mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-03 21:24:31 +03:00
If an error occurs after creating a file, remove the file
This commit is contained in:
parent
6faebd3ff3
commit
71c30ed266
|
@ -652,6 +652,15 @@ class TestImage:
|
|||
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:
|
||||
|
||||
|
|
|
@ -2286,7 +2286,9 @@ class Image:
|
|||
else:
|
||||
save_handler = SAVE[format.upper()]
|
||||
|
||||
created = False
|
||||
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,8 +2298,15 @@ class Image:
|
|||
|
||||
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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user