Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pickling state #6245

Merged
merged 7 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
philippjfr marked this conversation as resolved.
Show resolved Hide resolved


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