From 47bebfc801c3905979715d0d22a9560c395581e6 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 29 Apr 2025 14:57:10 +1000 Subject: [PATCH] Allow loading state from Pillow < 11.2.1 --- Tests/test_pickle.py | 10 ++++++++++ src/PIL/ImageFile.py | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Tests/test_pickle.py b/Tests/test_pickle.py index 1c48cb743..54cef00ad 100644 --- a/Tests/test_pickle.py +++ b/Tests/test_pickle.py @@ -162,3 +162,13 @@ def test_pickle_font_file(tmp_path: Path, protocol: int) -> None: # Assert helper_assert_pickled_font_images(font, unpickled_font) + + +def test_load_earlier_data() -> None: + im = pickle.loads( + b"\x80\x04\x95@\x00\x00\x00\x00\x00\x00\x00\x8c\x12PIL.PngImagePlugin" + b"\x94\x8c\x0cPngImageFile\x94\x93\x94)\x81\x94]\x94(}\x94\x8c\x01L\x94K\x01" + b"K\x01\x86\x94NC\x01\x00\x94eb." + ) + assert im.mode == "L" + assert im.size == (1, 1) diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index bcb7d462e..bf556a2c6 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -257,7 +257,8 @@ class ImageFile(Image.Image): def __setstate__(self, state: list[Any]) -> None: self.tile = [] - self.filename = state[5] + if len(state) > 5: + self.filename = state[5] super().__setstate__(state) def verify(self) -> None: