[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


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



More information about the Python-Dev mailing list