8000 Simplify CallbackRegistry pickling. · matplotlib/matplotlib@3d50903 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3d50903

Browse files
committed
Simplify CallbackRegistry pickling.
The deleted comment was really only relevant to Py2. Preserving _cid_gen (which is picklable) is necessary to later enable careful pickling of select callbacks, such as 3d mouse handlers (otherwise, the same cid may be assigned twice, one before pickling and once after unpickling).
1 parent 5914990 commit 3d50903

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,29 +151,18 @@ def h(exc):
151151
"""
152152

153153
# We maintain two mappings:
154-
# callbacks: signal -> {cid -> callback}
155-
# _func_cid_map: signal -> {callback -> cid}
156-
# (actually, callbacks are weakrefs to the actual callbacks).
154+
# callbacks: signal -> {cid -> weakref-to-callback}
155+
# _func_cid_map: signal -> {weakref-to-callback -> cid}
157156

158157
def __init__(self, exception_handler=_exception_printer):
159158
self.exception_handler = exception_handler
160159
self.callbacks = {}
161160
self._cid_gen = itertools.count()
162161
self._func_cid_map = {}
163162

164-
# In general, callbacks may not be pickled; thus, we simply recreate an
165-
# empty dictionary at unpickling. In order to ensure that `__setstate__`
166-
# (which just defers to `__init__`) is called, `__getstate__` must
167-
# return a truthy value (for pickle protocol>=3, i.e. Py3, the
168-
# *actual* behavior is that `__setstate__` will be called as long as
169-
# `__getstate__` does not return `None`, but this is undocumented -- see
170-
# http://bugs.python.org/issue12290).
171-
172163
def __getstate__(self):
173-
return {'exception_handler': self.exception_handler}
174-
175-
def __setstate__(self, state):
176-
self.__init__(**state)
164+
# In general, callbacks may not be pickled, so we just drop them.
165+
return {**vars(self), "callbacks": {}, "_func_cid_map": {}}
177166

178167
def connect(self, s, func):
179168
"""Register *func* to be called when signal *s* is generated.

0 commit comments

Comments
 (0)
0