Do not load image during save if file extension is unknown (#8835)

Co-authored-by: Andrew Murray <radarhere@users.noreply.github.com>
This commit is contained in:
Andrew Murray 2025-03-27 23:44:27 +11:00 committed by GitHub
parent e1f0def839
commit 3c185d1f69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 8 deletions

View File

@ -43,7 +43,7 @@ def test_save() -> None:
# Arrange
with Image.open(TEST_FILE) as im:
dummy_fp = BytesIO()
dummy_filename = "dummy.filename"
dummy_filename = "dummy.h5"
# Act / Assert: stub cannot save without an implemented handler
with pytest.raises(OSError):

View File

@ -2510,13 +2510,6 @@ class Image:
# only set the name for metadata purposes
filename = os.fspath(fp.name)
# may mutate self!
self._ensure_mutable()
save_all = params.pop("save_all", False)
self.encoderinfo = {**getattr(self, "encoderinfo", {}), **params}
self.encoderconfig: tuple[Any, ...] = ()
preinit()
filename_ext = os.path.splitext(filename)[1].lower()
@ -2531,6 +2524,13 @@ class Image:
msg = f"unknown file extension: {ext}"
raise ValueError(msg) from e
# may mutate self!
self._ensure_mutable()
save_all = params.pop("save_all", False)
self.encoderinfo = {**getattr(self, "encoderinfo", {}), **params}
self.encoderconfig: tuple[Any, ...] = ()
if format.upper() not in SAVE:
init()
if save_all: