classification
Title: Overwriting dict.__getattr__ is inconsistent
Type: behavior Stage: resolved
Components: None Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Albert.Zeyer, Anthony.Kong, Mark.Shannon, benjamin.peterson, python-dev
Priority: normal Keywords:

Created on 2012-04-23 22:53 by Albert.Zeyer, last changed 2012-04-24 15:10 by python-dev. This issue is now closed.

Messages (4)
msg159099 - (view) Author: Albert Zeyer (Albert.Zeyer) Date: 2012-04-23 22:53
```
class Foo1(dict):
    def __getattr__(self, key): return self[key]
    def __setattr__(self, key, value): self[key] = value

class Foo2(dict):
    __getattr__ = dict.__getitem__
    __setattr__ = dict.__setitem__

o1 = Foo1()
o1.x = 42
print(o1, o1.x)

o2 = Foo2()
o2.x = 42
print(o2, o2.x)
```

With CPython 2.5, 2.6 (similarly in 3.2), I get:
({'x': 42}, 42)
({}, 42)

With PyPy 1.5.0, I get the expected output::
({'x': 42}, 42)
({'x': 42}, 42)

I asked this also on SO: http://stackoverflow.com/questions/6305267/python-inconsistence-in-the-way-you-define-the-function-setattr

From the answers, I am not exactly sure wether this is considered as a bug in CPython or not. Anyway, I just wanted to post this here.
msg159110 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-04-24 01:45
It's a CPython bug.
msg159153 - (view) Author: Roundup Robot (python-dev) Date: 2012-04-24 15:09
New changeset 971865f12377 by Benjamin Peterson in branch '3.2':
don't use a slot wrapper from a different special method (closes #14658)
http://hg.python.org/cpython/rev/971865f12377

New changeset 0c1c8f8955d8 by Benjamin Peterson in branch 'default':
merge 3.2 (#14658)
http://hg.python.org/cpython/rev/0c1c8f8955d8
msg159155 - (view) Author: Roundup Robot (python-dev) Date: 2012-04-24 15:10
New changeset e3eda2d91e93 by Benjamin Peterson in branch '2.7':
don't use a slot wrapper from a different special method (closes #14658)
http://hg.python.org/cpython/rev/e3eda2d91e93
History
Date User Action Args
2012-04-24 15:10:31python-devsetmessages: + msg159155
2012-04-24 15:09:28python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg159153

resolution: fixed
stage: resolved
2012-04-24 14:18:46Mark.Shannonsetnosy: + Mark.Shannon
2012-04-24 13:58:18Anthony.Kongsetnosy: + Anthony.Kong
2012-04-24 01:45:23benjamin.petersonsetmessages: + msg159110
2012-04-23 22:54:14pitrousetnosy: + benjamin.peterson
2012-04-23 22:53:32Albert.Zeyercreate