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 99fea83

Browse filesBrowse files
authored
Merge pull request #10564 from anntzer/simplerpickle
MNT: Nested classes and instancemethods are directly picklable on Py3.5+.
2 parents 5e22976 + 08387d8 commit 99fea83
Copy full SHA for 99fea83

File tree

Expand file treeCollapse file tree

6 files changed

+1
-115
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+1
-115
lines changed

‎lib/matplotlib/axes/_base.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def __init__(self, axes, command='plot'):
155155
self.set_prop_cycle()
156156

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

161161
def __setstate__(self, state):

‎lib/matplotlib/cbook/__init__.py

Copy file name to clipboardExpand all lines: lib/matplotlib/cbook/__init__.py
-38Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,44 +1613,6 @@ def violin_stats(X, method, points=100):
16131613
return vpstats
16141614

16151615

1616-
class _NestedClassGetter(object):
1617-
# recipe from http://stackoverflow.com/a/11493777/741316
1618-
"""
1619-
When called with the containing class as the first argument,
1620-
and the name of the nested class as the second argument,
1621-
returns an instance of the nested class.
1622-
"""
1623-
def __call__(self, containing_class, class_name):
1624-
nested_class = getattr(containing_class, class_name)
1625-
1626-
# make an instance of a simple object (this one will do), for which we
1627-
# can change the __class__ later on.
1628-
nested_instance = _NestedClassGetter()
1629-
1630-
# set the class of the instance, the __init__ will never be called on
1631-
# the class but the original state will be set later on by pickle.
1632-
nested_instance.__class__ = nested_class
1633-
return nested_instance
1634-
1635-
1636-
class _InstanceMethodPickler(object):
1637-
"""
1638-
Pickle cannot handle instancemethod saving. _InstanceMethodPickler
1639-
provides a solution to this.
1640-
"""
1641-
def __init__(self, instancemethod):
1642-
"""Takes an instancemethod as its only argument."""
1643-
if six.PY3:
1644-
self.parent_obj = instancemethod.__self__
1645-
self.instancemethod_name = instancemethod.__func__.__name__
1646-
else:
1647-
self.parent_obj = instancemethod.im_self
1648-
self.instancemethod_name = instancemethod.im_func.__name__
1649-
1650-
def get_instancemethod(self):
1651-
return getattr(self.parent_obj, self.instancemethod_name)
1652-
1653-
16541616
def pts_to_prestep(x, *args):
16551617
"""
16561618
Convert continuous line to pre-steps.

‎lib/matplotlib/markers.py

Copy file name to clipboardExpand all lines: lib/matplotlib/markers.py
-9Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/offsetbox.py
-19Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/patches.py
-24Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/transforms.py
-24Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,30 +1677,6 @@ def _init(self, child):
16771677
def __eq__(self, other):
16781678
return self._child.__eq__(other)
16791679

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

0 commit comments

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