Skip to content

Commit be6e5d1

Browse files
authored
Merge pull request matplotlib#24659 from meeseeksmachine/auto-backport-of-pr-24657-on-v3.6.x
Backport PR matplotlib#24657 on branch v3.6.x (BUG: Fix bug with mutable input modification)
2 parents b4641ac + d84fb9e commit be6e5d1

File tree

3 files changed

+6
-0
lines changed

3 files changed

+6
-0
lines changed

lib/matplotlib/image.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,9 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
16301630
rgba = sm.to_rgba(arr, bytes=True)
16311631
if pil_kwargs is None:
16321632
pil_kwargs = {}
1633+
else:
1634+
# we modify this below, so make a copy (don't modify caller's dict)
1635+
pil_kwargs = pil_kwargs.copy()
16331636
pil_shape = (rgba.shape[1], rgba.shape[0])
16341637
image = PIL.Image.frombuffer(
16351638
"RGBA", pil_shape, rgba, "raw", "RGBA", 0, 1)

lib/matplotlib/tests/test_agg.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,11 @@ def test_pil_kwargs_webp():
254254
buf_small = io.BytesIO()
255255
pil_kwargs_low = {"quality": 1}
256256
plt.savefig(buf_small, format="webp", pil_kwargs=pil_kwargs_low)
257+
assert len(pil_kwargs_low) == 1
257258
buf_large = io.BytesIO()
258259
pil_kwargs_high = {"quality": 100}
259260
plt.savefig(buf_large, format="webp", pil_kwargs=pil_kwargs_high)
261+
assert len(pil_kwargs_high) == 1
260262
assert buf_large.getbuffer().nbytes > buf_small.getbuffer().nbytes
261263

262264

lib/matplotlib/tests/test_image.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ def test_imsave_pil_kwargs_tiff():
249249
buf = io.BytesIO()
250250
pil_kwargs = {"description": "test image"}
251251
plt.imsave(buf, [[0, 1], [2, 3]], format="tiff", pil_kwargs=pil_kwargs)
252+
assert len(pil_kwargs) == 1
252253
im = Image.open(buf)
253254
tags = {TAGS[k].name: v for k, v in im.tag_v2.items()}
254255
assert tags["ImageDescription"] == "test image"

0 commit comments

Comments
 (0)