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

Can not pickle instances of classes having methods decorated with lru_cache #13

Open
simonepozzoli opened this issue Oct 19, 2021 · 1 comment

Comments

@simonepozzoli
Copy link

I detected that is impossible to pickle an instance of a class having a method decorated with lru_cache after the latter method is called. If the method is never called, the pickle succeeds.

Example:

import pickle
from methodtools import lru_cache

class MyClass(object):
    @lru_cache(maxsize=None)
    def decorated_method(self):
        return True

c = MyClass()
c.decorated_method()  # without this call the pickle succeeds

pickle.dumps(c)
Traceback (most recent call last):
  File "test_lru_cache_pickle.py", line 12, in <module>
    pickle.dumps(c)
  File "/usr/lib/python2.7/pickle.py", line 1380, in dumps
    Pickler(file, protocol).dump(obj)
  File "/usr/lib/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/usr/lib/python2.7/pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "/usr/lib/python2.7/pickle.py", line 425, in save_reduce
    save(state)
  File "/usr/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/usr/lib/python2.7/pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/usr/lib/python2.7/pickle.py", line 669, in _batch_setitems
    save(v)
  File "/usr/lib/python2.7/pickle.py", line 306, in save
    rv = reduce(self.proto)
  File "/usr/lib/python2.7/copy_reg.py", line 77, in _reduce_ex
    raise TypeError("a class that defines __slots__ without "
TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled
@youknowone
Copy link
Owner

Oh, the cached value must be ignored for pickle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants