nteract / vdom

🎄 Virtual DOM for Python
https://github.com/nteract/vdom/blob/master/docs/mimetype-spec.md
BSD 3-Clause "New" or "Revised" License
221 stars 34 forks source link

FrozenDict cannot be unpickled #104

Open mlucool opened 4 years ago

mlucool commented 4 years ago

Hi,

In python 3, FrozenDict can be pickled, but not unpickled (python2 does not have this issue):

$ python3 -c 'from vdom.frozendict import FrozenDict; from six.moves import cPickle; cPickle.loads(cPickle.dumps(FrozenDict(a=1), -1))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/python/python-3.7/std/lib64/python3.7/site-packages/vdom/frozendict.py", line 26, in __setitem__
    return self.__readonly__(super(FrozenDict, self).__setitem__, *args, **kwargs)
  File "/usr/local/python/python-3.7/std/lib64/python3.7/site-packages/vdom/frozendict.py", line 21, in __readonly__
    raise ValueError("Can not modify FrozenDict")
ValueError: Can not modify FrozenDict

I believe this is due to the reduce method. For example, this fixes it:

from vdom.frozendict import FrozenDict
def _reduce_vdom_frozendict(self):
    return (FrozenDict, (tuple(self.items()),))
FrozenDict.__reduce__ = _reduce_vdom_frozendict

I am happy to make a PR if you believe this is the right fix.

mlucool commented 3 years ago

Bumping this as we still run into it sometimes. Would you be willing to accept a PR for this?

CC @captainsafia @rgbkrk @willingc