Skip to content

Simplify the internal API to connect picklable callbacks. #22484

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 1 commit into from
Feb 23, 2022
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
5 changes: 2 additions & 3 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,8 @@ def __init__(self, fig, rect,
self._internal_update(kwargs)

for name, axis in self._axis_map.items():
axis.callbacks._pickled_cids.add(
axis.callbacks.connect(
'units', self._unit_change_handler(name)))
axis.callbacks._connect_picklable(
'units', self._unit_change_handler(name))

rcParams = mpl.rcParams
self.tick_params(
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ def connect(self, signal, func):
self.callbacks[signal][cid] = proxy
return cid

def _connect_picklable(self, signal, func):
"""
Like `.connect`, but the callback is kept when pickling/unpickling.

Currently internal-use only.
"""
cid = self.connect(signal, func)
self._pickled_cids.add(cid)
return cid

# Keep a reference to sys.is_finalizing, as sys may have been cleared out
# at that point.
def _remove_proxy(self, proxy, *, _is_finalizing=sys.is_finalizing):
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ def setup(self):
self.callbacks = cbook.CallbackRegistry()

def connect(self, s, func, pickle):
cid = self.callbacks.connect(s, func)
if pickle:
self.callbacks._pickled_cids.add(cid)
return cid
return self.callbacks.connect(s, func)
else:
return self.callbacks._connect_picklable(s, func)

def disconnect(self, cid):
return self.callbacks.disconnect(cid)
Expand Down
14 changes: 6 additions & 8 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,12 @@ def __init__(
self.fmt_zdata = None

self.mouse_init()
self.figure.canvas.callbacks._pickled_cids.update({
self.figure.canvas.mpl_connect(
'motion_notify_event', self._on_move),
self.figure.canvas.mpl_connect(
'button_press_event', self._button_press),
self.figure.canvas.mpl_connect(
'button_release_event', self._button_release),
})
self.figure.canvas.callbacks._connect_picklable(
'motion_notify_event', self._on_move)
self.figure.canvas.callbacks._connect_picklable(
'button_press_event', self._button_press)
self.figure.canvas.callbacks._connect_picklable(
'button_release_event', self._button_release)
self.set_top_view()

self.patch.set_linewidth(0)
Expand Down