[Python-ideas] Hooks into super()'s __getattribute__

Ronald Oussoren ronaldoussoren at mac.com
Tue Jun 11 16:54:02 CEST 2013


Hi,

Super() currently does not have a way to hook into the behavior of attribute lookup, the __getattribute__ method of super peeks in the __dict__ of types along the MRO until it finds what it is looking for.

That can be a problem when a class implements __getattribute__ and doesn't necessarily store (all) attributes in the type __dict__.  PyObjC is an example where the current behavior causes problems: PyObjC defines proxy classes for classes in the Objective-C runtime. The __dict__ of those classes is filled on demand, whenever a method is called that isn't in the __dict__ yet PyObjC looks in the Objective-C runtime datastructures for the method and adds it to __dict__. This works fine for normal method resolution, but can fail with super: super will only return the correct value when the superclass method happens to be in __dict__ already.  My current solution for this is a custom subclass of super that must be used with Cocoa subclasses, and that's something I'd like to get rid off.

I'd therefore like to propose adding a slot to PyTypeObject that is called by super's __getattribute__ when present, instead of peeking in the type's tp_dict slot. Does this look like a sane solution? 

I've filed an issue about this that includes a proof of concept patch: http://bugs.python.org/issue18181. That patch is incomplete, but does make it possible to use the builtin super for Cocoa classes (with a suitably patched version of PyObjC). 

An earlier issue mentions that the current behavior of super can be inconsistent with the behavior of __getattribute__, see http://bugs.python.org/issue783528 (that issue is closed, but IMHO for the wrong reason). I'm appearently not the only one running into this problem ;-)

Ronald


More information about the Python-ideas mailing list