8000 Merge pull request #26154 from tacaswell/fix/py312_itertools · matplotlib/matplotlib@1d0d255 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d0d255

Browse files
authored
Merge pull request #26154 from tacaswell/fix/py312_itertools
MNT: py312 deprecates pickling objects in itertools
2 parents 623da30 + ba60210 commit 1d0d255

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

lib/matplotlib/cbook.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ def __getstate__(self):
192192
for s, d in self.callbacks.items()},
193193
# It is simpler to reconstruct this from callbacks in __setstate__.
194194
"_func_cid_map": None,
195+
"_cid_gen": next(self._cid_gen)
195196
}
196197

197198
def __setstate__(self, state):
199+
cid_count = state.pop('_cid_gen')
198200
vars(self).update(state)
199201
self.callbacks = {
200202
s: {cid: _weak_or_strong_ref(func, self._remove_proxy)
@@ -203,6 +205,7 @@ def __setstate__(self, state):
203205
self._func_cid_map = {
204206
s: {proxy: cid for cid, proxy in d.items()}
205207
for s, d in self.callbacks.items()}
208+
self._cid_gen = itertools.count(cid_count)
206209

207210
def connect(self, signal, func):
208211
"""Register *func* to be called when signal *signal* is generated."""

lib/matplotlib/figure.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ def current(self):
105105
"""Return the active axes, or None if the stack is empty."""
106106
return max(self._axes, key=self._axes.__getitem__, default=None)
107107

108+
def __getstate__(self):
109+
return {
110+
**vars(self),
111+
"_counter": max(self._axes.values(), default=0)
112+
}
113+
114+
def __setstate__(self, state):
115+
next_counter = state.pop('_counter')
116+
vars(self).update(state)
117+
self._counter = itertools.count(next_counter)
118+
108119

109120
class SubplotParams:
110121
"""

lib/matplotlib/tests/test_backend_tk.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def _isolated_tk_test(success_count, func=None):
3939
reason="$DISPLAY and $WAYLAND_DISPLAY are unset"
4040
)
4141
@pytest.mark.xfail( # https://github.com/actions/setup-python/issues/649
42-
'TF_BUILD' in os.environ and sys.platform == 'darwin' and
43-
sys.version_info[:2] < (3, 11),
42+
('TF_BUILD' in os.environ or 'GITHUB_ACTION' in os.environ) and
43+
sys.platform == 'darwin' and sys.version_info[:2] < (3, 11),
4444
reason='Tk version mismatch on Azure macOS CI'
4545
)
4646
@functools.wraps(func)

lib/matplotlib/tests/test_backends_interactive.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ def _get_testable_interactive_backends():
6363
elif env["MPLBACKEND"].startswith('wx') and sys.platform == 'darwin':
6464
# ignore on OSX because that's currently broken (github #16849)
6565
marks.append(pytest.mark.xfail(reason='github #16849'))
66-
elif (env['MPLBACKEND'] == 'tkagg' and 'TF_BUILD' in os.environ and
67-
sys.platform == 'darwin' and sys.version_info[:2] < (3, 11)):
66+
elif (env['MPLBACKEND'] == 'tkagg' and
67+
('TF_BUILD' in os.environ or 'GITHUB_ACTION' in os.environ) and
68+
sys.platform == 'darwin' and
69+
sys.version_info[:2] < (3, 11)
70+
):
6871
marks.append( # https://github.com/actions/setup-python/issues/649
6972
pytest.mark.xfail(reason='Tk version mismatch on Azure macOS CI'))
7073
envs.append(
@@ -273,7 +276,8 @@ def _test_thread_impl():
273276
reason='PyPy does not support Tkinter threading: '
274277
'https://foss.heptapod.net/pypy/pypy/-/issues/1929',
275278
strict=True))
276-
elif (backend == 'tkagg' and 'TF_BUILD' in os.environ and
279+
elif (backend == 'tkagg' and
280+
('TF_BUILD' in os.environ or 'GITHUB_ACTION' in os.environ) and
277281
sys.platform == 'darwin' and sys.version_info[:2] < (3, 11)):
278282
param.marks.append( # https://github.com/actions/setup-python/issues/649
279283
pytest.mark.xfail('Tk version mismatch on Azure macOS CI'))
@@ -546,8 +550,11 @@ def _test_number_of_draws_script():
546550
elif backend == "wx":
547551
param.marks.append(
548552
pytest.mark.skip("wx does not support blitting"))
549-
elif (backend == 'tkagg' and 'TF_BUILD' in os.environ and
550-
sys.platform == 'darwin' and sys.version_info[:2] < (3, 11)):
553+
elif (backend == 'tkagg' and
554+
('TF_BUILD' in os.environ or 'GITHUB_ACTION' in os.environ) and
555+
sys.platform == 'darwin' and
556+
sys.version_info[:2] < (3, 11)
557+
):
551558
param.marks.append( # https://github.com/actions/setup-python/issues/649
552559
pytest.mark.xfail('Tk version mismatch on Azure macOS CI')
553560
)

lib/matplotlib/tests/test_cbook.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ def is_not_empty(self):
209209
assert self.callbacks._func_cid_map != {}
210210
assert self.callbacks.callbacks != {}
211211

212+
def test_cid_restore(self):
213+
cb = cbook.CallbackRegistry()
214+
cb.connect('a', lambda: None)
215+
cb2 = pickle.loads(pickle.dumps(cb))
216+
cid = cb2.connect('c', lambda: None)
217+
assert cid == 1
218+
212219
@pytest.mark.parametrize('pickle', [True, False])
213220
def test_callback_complete(self, pickle):
214221
# ensure we start with an empty registry

0 commit comments

Comments
 (0)
0