[Python-Dev] python2.7 infinite recursion when loading pickled object
Schmitt Uwe (ID SIS)
uwe.schmitt at id.ethz.ch
Mon Aug 11 11:10:53 CEST 2014
More information about the Python-Dev mailing list
Mon Aug 11 11:10:53 CEST 2014
- Previous message: [Python-Dev] class Foo(object) vs class Foo: should be clearly explained in python 2 and 3 doc
- Next message: [Python-Dev] python2.7 infinite recursion when loading pickled object
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Dear all,
I discovered a problem using cPickle.loads from CPython 2.7.6.
The last line in the following code raises an infinite recursion
class T(object):
def __init__(self):
self.item = list()
def __getattr__(self, name):
return getattr(self.item, name)
import cPickle
t = T()
l = cPickle.dumps(t)
cPickle.loads(l)
loads triggers T.__getattr__ using "getattr(inst, "__setstate__", None)" for looking up a "__setstate__" method,
which is not implemented for T. As the item attribute is missing at this time, the ininfite recursion starts.
The infinite recursion disappears if I attach a default implementation for __setstate__ to T:
def __setstate__(self, dd):
self.__dict__ = dd
This could be fixed by using „hasattr“ in pickle before trying to call „getattr“.
Is this a bug or did I miss something ?
Kind Regards,
Uwe
- Previous message: [Python-Dev] class Foo(object) vs class Foo: should be clearly explained in python 2 and 3 doc
- Next message: [Python-Dev] python2.7 infinite recursion when loading pickled object
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list