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

Skip to content

Commit 08387d8

Browse files
committed
Nested classes and instancemethods are directly picklable on Py3.5+.
https://docs.python.org/3/whatsnew/3.5.html#pickle The previous workarounds were private and can be directly removed.
1 parent 9b48fd8 commit 08387d8

6 files changed

Lines changed: 1 addition & 115 deletions

File tree

lib/matplotlib/axes/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def __init__(self, axes, command='plot'):
158158
self.set_prop_cycle()
159159

160160
def __getstate__(self):
161-
# note: it is not possible to pickle a itertools.cycle instance
161+
# note: it is not possible to pickle a generator (and thus a cycler).
162162
return {'axes': self.axes, 'command': self.command}
163163

164164
def __setstate__(self, state):

lib/matplotlib/cbook/__init__.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,44 +2160,6 @@ def violin_stats(X, method, points=100):
21602160
return vpstats
21612161

21622162

2163-
class _NestedClassGetter(object):
2164-
# recipe from http://stackoverflow.com/a/11493777/741316
2165-
"""
2166-
When called with the containing class as the first argument,
2167-
and the name of the nested class as the second argument,
2168-
returns an instance of the nested class.
2169-
"""
2170-
def __call__(self, containing_class, class_name):
2171-
nested_class = getattr(containing_class, class_name)
2172-
2173-
# make an instance of a simple object (this one will do), for which we
2174-
# can change the __class__ later on.
2175-
nested_instance = _NestedClassGetter()
2176-
2177-
# set the class of the instance, the __init__ will never be called on
2178-
# the class but the original state will be set later on by pickle.
2179-
nested_instance.__class__ = nested_class
2180-
return nested_instance
2181-
2182-
2183-
class _InstanceMethodPickler(object):
2184-
"""
2185-
Pickle cannot handle instancemethod saving. _InstanceMethodPickler
2186-
provides a solution to this.
2187-
"""
2188-
def __init__(self, instancemethod):
2189-
"""Takes an instancemethod as its only argument."""
2190-
if six.PY3:
2191-
self.parent_obj = instancemethod.__self__
2192-
self.instancemethod_name = instancemethod.__func__.__name__
2193-
else:
2194-
self.parent_obj = instancemethod.im_self
2195-
self.instancemethod_name = instancemethod.im_func.__name__
2196-
2197-
def get_instancemethod(self):
2198-
return getattr(self.parent_obj, self.instancemethod_name)
2199-
2200-
22012163
def pts_to_prestep(x, *args):
22022164
"""
22032165
Convert continuous line to pre-steps.

lib/matplotlib/markers.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,6 @@ def __init__(self, marker=None, fillstyle=None):
187187
self.set_fillstyle(fillstyle)
188188
self.set_marker(marker)
189189

190-
def __getstate__(self):
191-
d = self.__dict__.copy()
192-
d.pop('_marker_function')
193-
return d
194-
195-
def __setstate__(self, statedict):
196-
self.__dict__ = statedict
197-
self.set_marker(self._marker)
198-
199190
def _recache(self):
200191
if self._marker_function is None:
201192
return

lib/matplotlib/offsetbox.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,6 @@ def __init__(self, *args, **kwargs):
156156
self._children = []
157157
self._offset = (0, 0)
158158

159-
def __getstate__(self):
160-
state = martist.Artist.__getstate__(self)
161-
162-
# pickle cannot save instancemethods, so handle them here
163-
from .cbook import _InstanceMethodPickler
164-
import inspect
165-
166-
offset = state['_offset']
167-
if inspect.ismethod(offset):
168-
state['_offset'] = _InstanceMethodPickler(offset)
169-
return state
170-
171-
def __setstate__(self, state):
172-
self.__dict__ = state
173-
from .cbook import _InstanceMethodPickler
174-
if isinstance(self._offset, _InstanceMethodPickler):
175-
self._offset = self._offset.get_instancemethod()
176-
self.stale = True
177-
178159
def set_figure(self, fig):
179160
"""
180161
Set the figure

lib/matplotlib/patches.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,14 +2033,6 @@ def __call__(self, x0, y0, width, height, mutation_size,
20332033
else:
20342034
return self.transmute(x0, y0, width, height, mutation_size)
20352035

2036-
def __reduce__(self):
2037-
# because we have decided to nest these classes, we need to
2038-
# add some more information to allow instance pickling.
2039-
return (cbook._NestedClassGetter(),
2040-
(BoxStyle, self.__class__.__name__),
2041-
self.__dict__
2042-
)
2043-
20442036
class Square(_Base):
20452037
"""
20462038
A simple square box.
@@ -2819,14 +2811,6 @@ def __call__(self, posA, posB,
28192811

28202812
return shrunk_path
28212813

2822-
def __reduce__(self):
2823-
# because we have decided to nest these classes, we need to
2824-
# add some more information to allow instance pickling.
2825-
return (cbook._NestedClassGetter(),
2826-
(ConnectionStyle, self.__class__.__name__),
2827-
self.__dict__
2828-
)
2829-
28302814
class Arc3(_Base):
28312815
"""
28322816
Creates a simple quadratic bezier curve between two
@@ -3276,14 +3260,6 @@ def __call__(self, path, mutation_size, linewidth,
32763260
else:
32773261
return self.transmute(path, mutation_size, linewidth)
32783262

3279-
def __reduce__(self):
3280-
# because we have decided to nest these classes, we need to
3281-
# add some more information to allow instance pickling.
3282-
return (cbook._NestedClassGetter(),
3283-
(ArrowStyle, self.__class__.__name__),
3284-
self.__dict__
3285-
)
3286-
32873263
class _Curve(_Base):
32883264
"""
32893265
A simple arrow which will work with any path instance. The

lib/matplotlib/transforms.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,30 +1680,6 @@ def _init(self, child):
16801680
def __eq__(self, other):
16811681
return self._child.__eq__(other)
16821682

1683-
# NOTE: Transform.__[gs]etstate__ should be sufficient when using only
1684-
# Python 3.4+.
1685-
def __getstate__(self):
1686-
# only store the child information and parents
1687-
return {
1688-
'child': self._child,
1689-
'input_dims': self.input_dims,
1690-
'output_dims': self.output_dims,
1691-
# turn the weak-values dictionary into a normal dictionary
1692-
'parents': dict((k, v()) for (k, v) in
1693-
six.iteritems(self._parents))
1694-
}
1695-
1696-
def __setstate__(self, state):
1697-
# re-initialise the TransformWrapper with the state's child
1698-
self._init(state['child'])
1699-
# The child may not be unpickled yet, so restore its information.
1700-
self.input_dims = state['input_dims']
1701-
self.output_dims = state['output_dims']
1702-
# turn the normal dictionary back into a dictionary with weak
1703-
# values
1704-
self._parents = dict((k, weakref.ref(v)) for (k, v) in
1705-
six.iteritems(state['parents']) if v is not None)
1706-
17071683
def __str__(self):
17081684
return ("{}(\n"
17091685
"{})"

0 commit comments

Comments
 (0)