mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-20 20:24:45 +03:00
Do not write new image to disk until it is successfully completed
This commit is contained in:
parent
eff6c34bd1
commit
2f568643f0
|
@ -641,6 +641,15 @@ class TestImage:
|
||||||
im.save(temp_file)
|
im.save(temp_file)
|
||||||
assert not record
|
assert not record
|
||||||
|
|
||||||
|
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):
|
def test_load_on_nonexclusive_multiframe(self):
|
||||||
with open("Tests/images/frozenpond.mpo", "rb") as fp:
|
with open("Tests/images/frozenpond.mpo", "rb") as fp:
|
||||||
|
|
||||||
|
|
|
@ -2202,16 +2202,23 @@ class Image:
|
||||||
else:
|
else:
|
||||||
save_handler = SAVE[format.upper()]
|
save_handler = SAVE[format.upper()]
|
||||||
|
|
||||||
|
new_file = False
|
||||||
if open_fp:
|
if open_fp:
|
||||||
if params.get("append", False):
|
if params.get("append", False):
|
||||||
# Open also for reading ("+"), because TIFF save_all
|
# Open also for reading ("+"), because TIFF save_all
|
||||||
# writer needs to go back and edit the written data.
|
# writer needs to go back and edit the written data.
|
||||||
fp = builtins.open(filename, "r+b")
|
fp = builtins.open(filename, "r+b")
|
||||||
|
elif not os.path.exists(filename):
|
||||||
|
new_file = True
|
||||||
|
fp = io.BytesIO()
|
||||||
else:
|
else:
|
||||||
fp = builtins.open(filename, "w+b")
|
fp = builtins.open(filename, "w+b")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
save_handler(self, fp, filename)
|
save_handler(self, fp, filename)
|
||||||
|
if new_file:
|
||||||
|
with builtins.open(filename, "w+b") as new_file_fp:
|
||||||
|
new_file_fp.write(fp.getvalue())
|
||||||
finally:
|
finally:
|
||||||
# do what we can to clean up
|
# do what we can to clean up
|
||||||
if open_fp:
|
if open_fp:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user