From c64270a19ff041c1ddaaf80e8d7f041d15296de8 Mon Sep 17 00:00:00 2001 From: Jyotishman Das <107503099+rishi02102017@users.noreply.github.com> Date: Fri, 27 Nov 2026 23:28:05 +0530 Subject: [PATCH 1/2] Do not keep save parameters after a failed save If save() raised before calling the format's save handler, for an unknown format, save_all with a format that cannot save multiple frames, or a path that could not be opened, encoderinfo was not restored. The leftover parameters then took priority over the ones passed to the next save(). --- Tests/test_image.py | 15 +++++++++++++++ src/PIL/Image.py | 8 ++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 1406fd6a35a..5bfd5e02b07 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -220,6 +220,21 @@ def test_unknown_extension(self, tmp_path: Path) -> None: with pytest.raises(ValueError): im.save(temp_file) + def test_failed_save_does_not_affect_next_save(self, tmp_path: Path) -> None: + im = Image.new("RGB", (1, 1)) + with pytest.raises(KeyError): + im.save(io.BytesIO(), "UNKNOWN", dpi=(72, 72)) + with pytest.raises(KeyError): + im.save(io.BytesIO(), "BMP", save_all=True, dpi=(72, 72)) + with pytest.raises(OSError): + im.save(tmp_path / "missing" / "temp.bmp", dpi=(72, 72)) + assert getattr(im, "encoderinfo", {}) == {} + + b = io.BytesIO() + im.save(b, "BMP", dpi=(300, 300)) + with Image.open(b) as reloaded: + assert reloaded.info["dpi"] == pytest.approx((300, 300), abs=0.01) + def test_internals(self) -> None: im = Image.new("L", (100, 100)) im.readonly = 1 diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 26284b8dde5..c5c1b27f3fb 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2749,10 +2749,6 @@ def save( self.load() save_all = params.pop("save_all", None) - self._default_encoderinfo = params - encoderinfo = getattr(self, "encoderinfo", {}) - self._attach_default_encoderinfo(self) - self.encoderconfig: tuple[Any, ...] = () if format.upper() not in SAVE: init() @@ -2777,6 +2773,10 @@ def save( else: fp = cast("IO[bytes]", fp) + self._default_encoderinfo = params + encoderinfo = getattr(self, "encoderinfo", {}) + self._attach_default_encoderinfo(self) + self.encoderconfig: tuple[Any, ...] = () try: save_handler(self, fp, filename) except Exception: From 6bd06a53850a4feb6c5931d49b2df6529577dfe0 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 28 Nov 2026 10:58:09 +1100 Subject: [PATCH 2/2] Simplify test --- Tests/test_image.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 5bfd5e02b07..44bedef8bb6 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -226,14 +226,14 @@ def test_failed_save_does_not_affect_next_save(self, tmp_path: Path) -> None: im.save(io.BytesIO(), "UNKNOWN", dpi=(72, 72)) with pytest.raises(KeyError): im.save(io.BytesIO(), "BMP", save_all=True, dpi=(72, 72)) - with pytest.raises(OSError): + with pytest.raises(FileNotFoundError): im.save(tmp_path / "missing" / "temp.bmp", dpi=(72, 72)) - assert getattr(im, "encoderinfo", {}) == {} + assert not hasattr(im, "encoderinfo") - b = io.BytesIO() - im.save(b, "BMP", dpi=(300, 300)) - with Image.open(b) as reloaded: - assert reloaded.info["dpi"] == pytest.approx((300, 300), abs=0.01) + f = tmp_path / "temp.bmp" + im.save(f, dpi=(300, 300)) + with Image.open(f) as reloaded: + assert reloaded.info["dpi"] == pytest.approx((300, 300), abs=0.001) def test_internals(self) -> None: im = Image.new("L", (100, 100))