From c8324eea84c762842c29fe8193e87ac89129e96b Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Fri, 30 Oct 2020 03:18:28 +0100 Subject: [PATCH] Backport PR #18839: MNT: make sure we do not mutate input in Text.update --- lib/matplotlib/tests/test_text.py | 12 ++++++++++++ lib/matplotlib/text.py | 2 ++ 2 files changed, 14 insertions(+) diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index 70855e6d879f..8d91b1318d88 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -8,9 +8,11 @@ import matplotlib as mpl from matplotlib.backend_bases import MouseEvent +from matplotlib.font_manager import FontProperties import matplotlib.patches as mpatches import matplotlib.pyplot as plt from matplotlib.testing.decorators import check_figures_equal, image_comparison +from matplotlib.text import Text needs_usetex = pytest.mark.skipif( @@ -689,3 +691,13 @@ def test_fontproperties_kwarg_precedence(): text2 = plt.ylabel("counts", size=40.0, fontproperties='Times New Roman') assert text1.get_size() == 40.0 assert text2.get_size() == 40.0 + + +def test_update_mutate_input(): + inp = dict(fontproperties=FontProperties(weight="bold"), + bbox=None) + cache = dict(inp) + t = Text() + t.update(inp) + assert inp['fontproperties'] == cache['fontproperties'] + assert inp['bbox'] == cache['bbox'] diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 3b30a4cfb502..efe4450f4fba 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -167,6 +167,8 @@ def __init__(self, def update(self, kwargs): # docstring inherited + # make a copy so we do not mutate user input! + kwargs = dict(kwargs) sentinel = object() # bbox can be None, so use another sentinel. # Update fontproperties first, as it has lowest priority. fontproperties = kwargs.pop("fontproperties", sentinel)