Skip to content

Commit 5813452

Browse files
committed
Merge pull request #7914 from anntzer/callbackregistry-unpickling
Fix unpickling of CallbackRegistry on Py2.
1 parent fb3a2eb commit 5813452

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/matplotlib/cbook.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,13 +481,18 @@ def __init__(self):
481481
self._cid = 0
482482
self._func_cid_map = {}
483483

484+
# In general, callbacks may not be pickled; thus, we simply recreate an
485+
# empty dictionary at unpickling. In order to ensure that `__setstate__`
486+
# (which just defers to `__init__`) is called, `__getstate__` must
487+
# return a truthy value (for pickle protocol>=3, i.e. Py3, the
488+
# *actual* behavior is that `__setstate__` will be called as long as
489+
# `__getstate__` does not return `None`, but this is undocumented -- see
490+
# http://bugs.python.org/issue12290).
491+
484492
def __getstate__(self):
485-
# We cannot currently pickle the callables in the registry, so
486-
# return an empty dictionary.
487-
return {}
493+
return True
488494

489495
def __setstate__(self, state):
490-
# re-initialise an empty callback registry
491496
self.__init__()
492497

493498
def connect(self, s, func):

lib/matplotlib/tests/test_cbook.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
33
import itertools
4+
import pickle
45
from weakref import ref
56
import warnings
67

@@ -331,6 +332,10 @@ def test_callback_complete(self):
331332
def dummy(self):
332333
pass
333334

335+
def test_pickling(self):
336+
assert hasattr(pickle.loads(pickle.dumps(cbook.CallbackRegistry())),
337+
"callbacks")
338+
334339

335340
def _kwarg_norm_helper(inp, expected, kwargs_to_norm, warn_count=0):
336341

0 commit comments

Comments
 (0)