[Python-3000] Method to populate tp_* slots via getattr()?

Adam Olsen rhamph at gmail.com
Wed Apr 2 18:07:34 CEST 2008


On Wed, Apr 2, 2008 at 7:30 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> One of the issues with porting to Py3k is the problem that __getattr__
>  and __getattribute__ can't reliably provide special methods like __add__
>  the way __getattr__ could with classic classes. (As first noted by Terry
>  Reedy years ago, and recently seeing some new activity on the bug
>  tracker [1])
>
>  The culprit here is the fact that __getattribute__ and its associated
>  machinery is typically never invoked for the methods with dedicated tp_*
>  slots in the C-level type structure.
>
>  What do people think of the idea of providing an extra method on type
>  objects that goes through all of the C-level special method slots, and
>  for each one that isn't currently set, does a getattr() on the
>  associated special name and stores the result (if any) on the current
>  type object?
>
>  When converting a proxy class that relies on __getattr__ from classic to
>  new-style, all that would then be needed is to invoke the new method on
>  the class object after defining the class (a class decorator or
>  metaclass could be provided somewhere to make this a bit tidier).
>
>  This seems a lot cleaner than expecting everyone that implements a proxy
>  object to maintain there own list of all of the relevant special
>  methods, and locates the implementation support in an area of the code
>  that already has plenty of infrastructure dedicated to keeping Python
>  visible attributes in sync with the C visible tp_* slots.
>
>  Thoughts? Altenative ideas? Howls of protest?
>
>  [1] http://bugs.python.org/issue643841

I've been wondering if we should provide a ProxyMixin that returned
all the special methods to their old lookup behaviour.  I think that'd
be cleaner than providing a method to do it.  Not sure how easy it'd
be to implement though.

-- 
Adam Olsen, aka Rhamphoryncus


More information about the Python-3000 mailing list