Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e415ed1

Browse filesBrowse files
committed
Fix unpickling of CallbackRegistry on Py2.
In general, callbacks may not be pickled; thus, we simply recreate an empty dictionary at unpickling. In order to ensure that `__setstate__` (which just defers to `__init__`) is called, `__getstate__` must return a truthy value (for pickle protocol>=3, i.e. Py3, the *actual* behavior is that `__setstate__` will be called as long as `__getstate__` does not return `None`, but this is undocumented -- see http://bugs.python.org/issue12290).
1 parent 194afed commit e415ed1
Copy full SHA for e415ed1

File tree

Expand file treeCollapse file tree

2 files changed

+14
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+14
-4
lines changed
Open diff view settings
Collapse file

‎lib/matplotlib/cbook.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/cbook.py
+9-4Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,18 @@ def __init__(self):
502502
self._cid = 0
503503
self._func_cid_map = {}
504504

505+
# In general, callbacks may not be pickled; thus, we simply recreate an
506+
# empty dictionary at unpickling. In order to ensure that `__setstate__`
507+
# (which just defers to `__init__`) is called, `__getstate__` must
508+
# return a truthy value (for pickle protocol>=3, i.e. Py3, the
509+
# *actual* behavior is that `__setstate__` will be called as long as
510+
# `__getstate__` does not return `None`, but this is undocumented -- see
511+
# http://bugs.python.org/issue12290).
512+
505513
def __getstate__(self):
506-
# We cannot currently pickle the callables in the registry, so
507-
# return an empty dictionary.
508-
return {}
514+
return True
509515

510516
def __setstate__(self, state):
511-
# re-initialise an empty callback registry
512517
self.__init__()
513518

514519
def connect(self, s, func):
Collapse file

‎lib/matplotlib/tests/test_cbook.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_cbook.py
+5Lines 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

@@ -283,6 +284,10 @@ def test_callback_complete(self):
283284
def dummy(self):
284285
pass
285286

287+
def test_pickling(self):
288+
assert hasattr(pickle.loads(pickle.dumps(cbook.CallbackRegistry())),
289+
"callbacks")
290+
286291

287292
def test_sanitize_sequence():
288293
d = {'a': 1, 'b': 2, 'c': 3}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.