Skip to content

Commit 529e113

Browse files
authored
Merge pull request #4561 from hugovk/fix-webp-pickle
Fix pickling WebP
2 parents e4fa234 + 9963521 commit 529e113

2 files changed

Lines changed: 43 additions & 35 deletions

File tree

Tests/test_pickle.py

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import pickle
22

3+
import pytest
34
from PIL import Image
45

6+
from .helper import skip_unless_feature
57

6-
def helper_pickle_file(tmp_path, pickle, protocol=0, mode=None):
8+
9+
def helper_pickle_file(
10+
tmp_path, pickle, protocol=0, test_file="Tests/images/hopper.jpg", mode=None
11+
):
712
# Arrange
8-
with Image.open("Tests/images/hopper.jpg") as im:
13+
with Image.open(test_file) as im:
914
filename = str(tmp_path / "temp.pkl")
1015
if mode:
1116
im = im.convert(mode)
@@ -35,41 +40,30 @@ def helper_pickle_string(
3540
assert im == loaded_im
3641

3742

38-
def test_pickle_image(tmp_path):
39-
# Act / Assert
40-
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
41-
helper_pickle_string(pickle, protocol)
42-
helper_pickle_file(tmp_path, pickle, protocol)
43-
44-
45-
def test_pickle_p_mode():
46-
# Act / Assert
47-
for test_file in [
48-
"Tests/images/test-card.png",
49-
"Tests/images/zero_bb.png",
50-
"Tests/images/zero_bb_scale2.png",
51-
"Tests/images/non_zero_bb.png",
52-
"Tests/images/non_zero_bb_scale2.png",
53-
"Tests/images/p_trns_single.png",
54-
"Tests/images/pil123p.png",
55-
"Tests/images/itxt_chunks.png",
56-
]:
57-
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
58-
helper_pickle_string(pickle, protocol=protocol, test_file=test_file)
59-
60-
61-
def test_pickle_pa_mode(tmp_path):
62-
# Act / Assert
63-
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
64-
helper_pickle_string(pickle, protocol, mode="PA")
65-
helper_pickle_file(tmp_path, pickle, protocol, mode="PA")
66-
67-
68-
def test_pickle_l_mode(tmp_path):
43+
@pytest.mark.parametrize(
44+
("test_file", "test_mode"),
45+
[
46+
("Tests/images/hopper.jpg", None),
47+
("Tests/images/hopper.jpg", "L"),
48+
("Tests/images/hopper.jpg", "PA"),
49+
pytest.param(
50+
"Tests/images/hopper.webp", None, marks=skip_unless_feature("webp")
51+
),
52+
("Tests/images/test-card.png", None),
53+
("Tests/images/zero_bb.png", None),
54+
("Tests/images/zero_bb_scale2.png", None),
55+
("Tests/images/non_zero_bb.png", None),
56+
("Tests/images/non_zero_bb_scale2.png", None),
57+
("Tests/images/p_trns_single.png", None),
58+
("Tests/images/pil123p.png", None),
59+
("Tests/images/itxt_chunks.png", None),
60+
],
61+
)
62+
def test_pickle_image(tmp_path, test_file, test_mode):
6963
# Act / Assert
7064
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
71-
helper_pickle_string(pickle, protocol, mode="L")
72-
helper_pickle_file(tmp_path, pickle, protocol, mode="L")
65+
helper_pickle_string(pickle, protocol, test_file, test_mode)
66+
helper_pickle_file(tmp_path, pickle, protocol, test_file, test_mode)
7367

7468

7569
def test_pickle_la_mode_with_palette(tmp_path):
@@ -88,3 +82,15 @@ def test_pickle_la_mode_with_palette(tmp_path):
8882

8983
im.mode = "PA"
9084
assert im == loaded_im
85+
86+
87+
@skip_unless_feature("webp")
88+
def test_pickle_tell():
89+
# Arrange
90+
image = Image.open("Tests/images/hopper.webp")
91+
92+
# Act: roundtrip
93+
unpickled_image = pickle.loads(pickle.dumps(image))
94+
95+
# Assert
96+
assert unpickled_image.tell() == 0

src/PIL/WebPImagePlugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class WebPImageFile(ImageFile.ImageFile):
3838

3939
format = "WEBP"
4040
format_description = "WebP image"
41+
__loaded = 0
42+
__logical_frame = 0
4143

4244
def _open(self):
4345
if not _webp.HAVE_WEBPANIM:

0 commit comments

Comments
 (0)