Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59e32b1

Browse files
authored
Merge pull request #26177 from meeseeksmachine/auto-backport-of-pr-26154-on-v3.7.x
Backport PR #26154 on branch v3.7.x (MNT: py312 deprecates pickling objects in itertools)
2 parents 658397b + b7fd4c1 commit 59e32b1

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,11 @@ def __getstate__(self):
208208
for s, d in self.callbacks.items()},
209209
# It is simpler to reconstruct this from callbacks in __setstate__.
210210
"_func_cid_map": None,
211+
"_cid_gen": next(self._cid_gen)
211212
}
212213

213214
def __setstate__(self, state):
215+
cid_count = state.pop('_cid_gen')
214216
vars(self).update(state)
215217
self.callbacks = {
216218
s: {cid: _weak_or_strong_ref(func, self._remove_proxy)
@@ -219,6 +221,7 @@ def __setstate__(self, state):
219221
self._func_cid_map = {
220222
s: {proxy: cid for cid, proxy in d.items()}
221223
for s, d in self.callbacks.items()}
224+
self._cid_gen = itertools.count(cid_count)
222225

223226
def connect(self, signal, func):
224227
"""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
@@ -106,6 +106,17 @@ def current(self):
106106
"""Return the active axes, or None if the stack is empty."""
107107
return max(self._axes, key=self._axes.__getitem__, default=None)
108108

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

110121
class SubplotParams:
111122
"""

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(
@@ -271,7 +274,8 @@ def _test_thread_impl():
271274
reason='PyPy does not support Tkinter threading: '
272275
'https://foss.heptapod.net/pypy/pypy/-/issues/1929',
273276
strict=True))
274-
elif (backend == 'tkagg' and 'TF_BUILD' in os.environ and
277+
elif (backend == 'tkagg' and
278+
('TF_BUILD' in os.environ or 'GITHUB_ACTION' in os.environ) and
275279
sys.platform == 'darwin' and sys.version_info[:2] < (3, 11)):
276280
param.marks.append( # https://github.com/actions/setup-python/issues/649
277281
pytest.mark.xfail('Tk version mismatch on Azure macOS CI'))
@@ -552,8 +556,11 @@ def _test_number_of_draws_script():
552556
elif backend == "wx":
553557
param.marks.append(
554558
pytest.mark.skip("wx does not support blitting"))
555-
elif (backend == 'tkagg' and 'TF_BUILD' in os.environ and
556-
sys.platform == 'darwin' and sys.version_info[:2] < (3, 11)):
559+
elif (backend == 'tkagg' and
560+
('TF_BUILD' in os.environ or 'GITHUB_ACTION' in os.environ) and
561+
sys.platform == 'darwin' and
562+
sys.version_info[:2] < (3, 11)
563+
):
557564
param.marks.append( # https://github.com/actions/setup-python/issues/649
558565
pytest.mark.xfail('Tk version mismatch on Azure macOS CI')
559566
)

lib/matplotlib/tests/test_cbook.py

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

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

0 commit comments

Comments
 (0)