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

Skip to content

MNT: py312 deprecates pickling objects in itertools #26154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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."""
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 12 additions & 5 deletions lib/matplotlib/tests/test_backends_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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')
)
Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down