Re: [Python-Dev] [Python-checkins] cpython (3.2): don't use a slot wrapper from a different special method (closes #14658)

I'm not happy with this fix. Admittedly code like: class S(str): __getattr__ = str.__add__ s = S('a') print(S.b) is a little weird. But I think it should work (ie print 'ab') properly. This works without the patch. class S(str): __getattribute__ = str.__add__ s = S('a') print(S.b) (Prints 'ab') Also "slot wrapper" is a low-level implementation detail and shouldn't impact the language semantics. dict.__getitem__ is a slot wrapper; dict.__getitem__ is not. str.__getitem__ is a slot wrapper; list.__getitem__ is not. If any of these change then the semantics of the language changes. Cheers, Mark benjamin.peterson wrote:

2012/4/24 Mark Shannon <mark@hotpy.org>:
I'm not happy with this fix.
It's not perfect, but it's an improvement.
Does it? $ cat > x.py class S(str): __getattribute__ = str.__add__ s = S('a') print(S.b) $ python3 x.py Traceback (most recent call last): File "x.py", line 4, in <module> print(S.b) AttributeError: type object 'S' has no attribute 'b'
-- Regards, Benjamin

Mark Shannon wrote:
I can easily believe I'm missing something, but here are the results with the patch in place: {'x': 42} 42 {'x': 42} 42 ab and here's the code: 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) class S(str): __getattr__ = str.__add__ s = S('a') print(s.b) ~Ethan~

Benjamin Peterson wrote:
Actually, I think it is probably correct. I've been trying to break it by assigning various unusual objects to special attributes and it seems OK so far. I don't really trust all that slot-wrapper stuff, but rewriting is a lot of work and would introduce new errors, so I'll just leave it at that. [snip] Cheers, Mark.

2012/4/24 Mark Shannon <mark@hotpy.org>:
I'm not happy with this fix.
It's not perfect, but it's an improvement.
Does it? $ cat > x.py class S(str): __getattribute__ = str.__add__ s = S('a') print(S.b) $ python3 x.py Traceback (most recent call last): File "x.py", line 4, in <module> print(S.b) AttributeError: type object 'S' has no attribute 'b'
-- Regards, Benjamin

Mark Shannon wrote:
I can easily believe I'm missing something, but here are the results with the patch in place: {'x': 42} 42 {'x': 42} 42 ab and here's the code: 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) class S(str): __getattr__ = str.__add__ s = S('a') print(s.b) ~Ethan~

Benjamin Peterson wrote:
Actually, I think it is probably correct. I've been trying to break it by assigning various unusual objects to special attributes and it seems OK so far. I don't really trust all that slot-wrapper stuff, but rewriting is a lot of work and would introduce new errors, so I'll just leave it at that. [snip] Cheers, Mark.
participants (3)
-
Benjamin Peterson
-
Ethan Furman
-
Mark Shannon