From c01373c0dff256e6382274d3962e547b9f7745e1 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 19 Jun 2023 21:28:02 -0400 Subject: [PATCH 1/3] MNT: py312 deprecates pickling objects in itertools --- lib/matplotlib/cbook.py | 3 +++ lib/matplotlib/tests/test_cbook.py | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index 850028c297db..87656b5c3c00 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -192,9 +192,11 @@ def __getstate__(self): for s, d in self.callbacks.items()}, # It is simpler to reconstruct this from callbacks in __setstate__. "_func_cid_map": None, + "_cid_gen": next(self._cid_gen) } def __setstate__(self, state): + cid_count = state.pop('_cid_gen') vars(self).update(state) self.callbacks = { s: {cid: _weak_or_strong_ref(func, self._remove_proxy) @@ -203,6 +205,7 @@ def __setstate__(self, state): self._func_cid_map = { s: {proxy: cid for cid, proxy in d.items()} for s, d in self.callbacks.items()} + self._cid_gen = itertools.count(cid_count) def connect(self, signal, func): """Register *func* to be called when signal *signal* is generated.""" diff --git a/lib/matplotlib/tests/test_cbook.py b/lib/matplotlib/tests/test_cbook.py index effe2338931f..55dc934baf42 100644 --- a/lib/matplotlib/tests/test_cbook.py +++ b/lib/matplotlib/tests/test_cbook.py @@ -209,6 +209,13 @@ def is_not_empty(self): assert self.callbacks._func_cid_map != {} assert self.callbacks.callbacks != {} + def test_cid_restore(self): + cb = cbook.CallbackRegistry() + cb.connect('a', lambda: None) + cb2 = pickle.loads(pickle.dumps(cb)) + cid = cb2.connect('c', lambda: None) + assert cid == 1 + @pytest.mark.parametrize('pickle', [True, False]) def test_callback_complete(self, pickle): # ensure we start with an empty registry From 4e0b60c875b3afdabf59abdb8286305224ab8509 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 20 Jun 2023 10:48:36 -0400 Subject: [PATCH 2/3] FIX: also account for itertools.count used in _AxesStack --- lib/matplotlib/figure.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 947f638b5f95..dab2bc4a5ab7 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -105,6 +105,17 @@ def current(self): """Return the active axes, or None if the stack is empty.""" return max(self._axes, key=self._axes.__getitem__, default=None) + def __getstate__(self): + return { + **vars(self), + "_counter": max(self._axes.values(), default=0) + } + + def __setstate__(self, state): + next_counter = state.pop('_counter') + vars(self).update(state) + self._counter = itertools.count(next_counter) + class SubplotParams: """ From ba602101babe4b727b4f3b4f7bbbd21f66fa14a3 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 20 Jun 2023 18:45:15 -0400 Subject: [PATCH 3/3] CI: skip tk tests on GHA as well --- lib/matplotlib/tests/test_backend_tk.py | 4 ++-- .../tests/test_backends_interactive.py | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/tests/test_backend_tk.py b/lib/matplotlib/tests/test_backend_tk.py index fac315b2b6f4..e44e5589452b 100644 --- a/lib/matplotlib/tests/test_backend_tk.py +++ b/lib/matplotlib/tests/test_backend_tk.py @@ -39,8 +39,8 @@ def _isolated_tk_test(success_count, func=None): reason="$DISPLAY and $WAYLAND_DISPLAY are unset" ) @pytest.mark.xfail( # https://github.com/actions/setup-python/issues/649 - 'TF_BUILD' in os.environ and sys.platform == 'darwin' and - sys.version_info[:2] < (3, 11), + ('TF_BUILD' in os.environ or 'GITHUB_ACTION' in os.environ) and + sys.platform == 'darwin' and sys.version_info[:2] < (3, 11), reason='Tk version mismatch on Azure macOS CI' ) @functools.wraps(func) diff --git a/lib/matplotlib/tests/test_backends_interactive.py b/lib/matplotlib/tests/test_backends_interactive.py index 4d9d30da50d3..f198f2c95612 100644 --- a/lib/matplotlib/tests/test_backends_interactive.py +++ b/lib/matplotlib/tests/test_backends_interactive.py @@ -63,8 +63,11 @@ def _get_testable_interactive_backends(): elif env["MPLBACKEND"].startswith('wx') and sys.platform == 'darwin': # ignore on OSX because that's currently broken (github #16849) marks.append(pytest.mark.xfail(reason='github #16849')) - elif (env['MPLBACKEND'] == 'tkagg' and 'TF_BUILD' in os.environ and - sys.platform == 'darwin' and sys.version_info[:2] < (3, 11)): + elif (env['MPLBACKEND'] == 'tkagg' and + ('TF_BUILD' in os.environ or 'GITHUB_ACTION' in os.environ) and + sys.platform == 'darwin' and + sys.version_info[:2] < (3, 11) + ): marks.append( # https://github.com/actions/setup-python/issues/649 pytest.mark.xfail(reason='Tk version mismatch on Azure macOS CI')) envs.append( @@ -273,7 +276,8 @@ def _test_thread_impl(): reason='PyPy does not support Tkinter threading: ' 'https://foss.heptapod.net/pypy/pypy/-/issues/1929', strict=True)) - elif (backend == 'tkagg' and 'TF_BUILD' in os.environ and + elif (backend == 'tkagg' and + ('TF_BUILD' in os.environ or 'GITHUB_ACTION' in os.environ) and sys.platform == 'darwin' and sys.version_info[:2] < (3, 11)): param.marks.append( # https://github.com/actions/setup-python/issues/649 pytest.mark.xfail('Tk version mismatch on Azure macOS CI')) @@ -546,8 +550,11 @@ def _test_number_of_draws_script(): elif backend == "wx": param.marks.append( pytest.mark.skip("wx does not support blitting")) - elif (backend == 'tkagg' and 'TF_BUILD' in os.environ and - sys.platform == 'darwin' and sys.version_info[:2] < (3, 11)): + elif (backend == 'tkagg' and + ('TF_BUILD' in os.environ or 'GITHUB_ACTION' in os.environ) and + sys.platform == 'darwin' and + sys.version_info[:2] < (3, 11) + ): param.marks.append( # https://github.com/actions/setup-python/issues/649 pytest.mark.xfail('Tk version mismatch on Azure macOS CI') )