[New-bugs-announce] [issue16251] object.__reduce__()

Eric Snow report at bugs.python.org
Tue Oct 16 20:24:54 CEST 2012


New submission from Eric Snow:

In Objects/typeobject.c, reduce_2() makes _PyObject_GetAttrId() calls to pull some methods for the object in question.  This is a problem when the object's type defines __getattr__() or __getattribute__() and returns something like None when the attribute is not found:


from copy import deepcopy

class Spam(dict):
    def __getattr__(self, name):
        # defaults to None
        return self.get(name)

deepcopy(Spam(ham=5))


While we could do a check on the result of _PyObject_GetAttrId() to make sure it is callable, the better solution would be to look up the methods on the object's type rather than on the object.  Doing so falls in line with the specified pattern for special method lookup [1].  I'm guessing there are a few other related places in typeobject.c that have the same problem which could be fixed in the same way.

I'm marking this as a bug rather than a feature since it is a deviation from the specification for special methods.

[1] http://docs.python.org/dev/reference/datamodel.html#special-method-lookup

----------
components: Interpreter Core
messages: 173066
nosy: eric.snow
priority: normal
severity: normal
stage: needs patch
status: open
title: object.__reduce__()
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16251>
_______________________________________


More information about the New-bugs-announce mailing list