pickle error: can't pickle instancemethod objects

M

Michele Simionato

Can somebody explain what's happening with the following script?

$ echo example.py
import pickle

class Example(object):

def __init__(self, obj, registry):
self._obj = obj
self._registry = registry
for name, func in self._registry.iteritems():
setattr(self, name, func.__get__(obj, obj.__class__))

def __gestate__(self): # should skip the bound methods attributes
return dict(_registry=self._registry, _obj=self._obj)

class C(object):
pass

def foo(self):
pass

if __name__ == '__main__':
ex = Example(C(), dict(foo=foo))
pickle.dumps(ex)

I get the following traceback:

Traceback (most recent call last):
File "pickle_error.py", line 22, in <module>
pickle.dumps(ex)
File "/usr/lib/python2.5/pickle.py", line 1366, in dumps
Pickler(file, protocol).dump(obj)
File "/usr/lib/python2.5/pickle.py", line 224, in dump
self.save(obj)
File "/usr/lib/python2.5/pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "/usr/lib/python2.5/pickle.py", line 419, in save_reduce
save(state)
File "/usr/lib/python2.5/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.5/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib/python2.5/pickle.py", line 663, in _batch_setitems
save(v)
File "/usr/lib/python2.5/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/usr/lib/python2.5/copy_reg.py", line 69, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle instancemethod objects

I know that instancemethods cannot be pickled, this is why I
used a __getstate__ method, but apparently it does not work.
Any hint?

Michele Simionato
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top