Merge pull request #8852 from radarhere/save

Only change readonly if saved filename matches opened filename
This commit is contained in:
mergify[bot] 2025-04-01 09:22:14 +00:00 committed by GitHub
commit 64178415e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View File

@ -258,6 +258,15 @@ class TestImage:
assert im.readonly
im.save(temp_file)
def test_save_without_changing_readonly(self, tmp_path: Path) -> None:
temp_file = tmp_path / "temp.bmp"
with Image.open("Tests/images/rgb32bf-rgba.bmp") as im:
assert im.readonly
im.save(temp_file)
assert im.readonly
def test_dump(self, tmp_path: Path) -> None:
im = Image.new("L", (10, 10))
im._dump(str(tmp_path / "temp_L.ppm"))

View File

@ -2540,8 +2540,13 @@ class Image:
msg = f"unknown file extension: {ext}"
raise ValueError(msg) from e
from . import ImageFile
# may mutate self!
self._ensure_mutable()
if isinstance(self, ImageFile.ImageFile) and filename == self.filename:
self._ensure_mutable()
else:
self.load()
save_all = params.pop("save_all", None)
self.encoderinfo = {**getattr(self, "encoderinfo", {}), **params}