Skip to content

Commit

Permalink
Fix pickling state (#6245)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Høxbro Hansen <[email protected]>
  • Loading branch information
ahuang11 and hoxbro authored Jun 7, 2024
1 parent 4bd31bc commit d6b2d95
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
50 changes: 20 additions & 30 deletions holoviews/core/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,37 +716,27 @@ def __setstate__(self, d):
"Restores options applied to this object."
d = param_aliases(d)

# Backwards compatibility for objects before id was made a property
opts_id = d['_id'] if '_id' in d else d.pop('id', None)
try:
load_options = Store.load_counter_offset is not None
if load_options:
matches = [k for k in d if k.startswith('_custom_option')]
for match in matches:
custom_id = int(match.split('_')[-1])+Store.load_counter_offset
if not isinstance(d[match], dict):
# Backward compatibility before multiple backends
backend_info = {'matplotlib':d[match]}
else:
backend_info = d[match]
for backend, info in backend_info.items():
if backend not in Store._custom_options:
Store._custom_options[backend] = {}
Store._custom_options[backend][custom_id] = info
if backend_info:
if custom_id not in Store._weakrefs:
Store._weakrefs[custom_id] = []
ref = weakref.ref(self, partial(cleanup_custom_options, custom_id))
Store._weakrefs[opts_id].append(ref)
d.pop(match)

if opts_id is not None:
opts_id += Store.load_counter_offset
except Exception:
self.param.warning("Could not unpickle custom style information.")
d['_id'] = opts_id
load_options = Store.load_counter_offset is not None
if load_options:
matches = [k for k in d if k.startswith('_custom_option')]
for match in matches:
custom_id = int(match.split('_')[-1])+Store.load_counter_offset
for backend, info in d[match].items():
if backend not in Store._custom_options:
Store._custom_options[backend] = {}
Store._custom_options[backend][custom_id] = info
if d[match]:
if custom_id not in Store._weakrefs:
Store._weakrefs[custom_id] = []
ref = weakref.ref(self, partial(cleanup_custom_options, custom_id))
Store._weakrefs[d["_id"]].append(ref)
d.pop(match)

if d["_id"] is not None:
d["_id"] += Store.load_counter_offset

self.__dict__.update(d)
super().__setstate__({})
super().__setstate__(d)


class Dimensioned(LabelledData):
Expand Down
10 changes: 10 additions & 0 deletions holoviews/tests/core/test_dimensioned.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import gc
import pickle

from holoviews.core.element import Element
from holoviews.core.options import Keywords, Options, OptionTree, Store
from holoviews.core.spaces import HoloMap
from holoviews.element.chart import Curve

from ..utils import LoggingComparisonTestCase

Expand Down Expand Up @@ -247,3 +249,11 @@ def test_opts_clear_cleans_unused_tree(self):
ExampleElement([]).opts(style_opt1='A').opts.clear()
custom_options = Store._custom_options['backend_1']
self.assertEqual(len(custom_options), 0)

class TestGetSetState:

def test_pickle_roundtrip(self):
curve = Curve([0, 1, 2], kdims=["XAXIS"])
roundtrip_curve = pickle.loads(pickle.dumps(curve))
assert curve.kdims == roundtrip_curve.kdims
assert curve.vdims == roundtrip_curve.vdims

0 comments on commit d6b2d95

Please sign in to comment.