__getattribute__ and methods proxying

David Zaslavsky diazona at ellipsix.net
Sat Jun 12 15:36:55 EDT 2010


Hi,

The problem is that when you make this call:
> proc.cmdline()
there are really two steps involved. First you are accessing proc.cmdline, 
then you are calling it. You could think of it as this:
 func = proc.cmdline
 func()
__getattribute__ is able to modify how the first step works, but not the 
second. And it is the second step where the OSError gets raised.

You could get around this by returning a wrapper function from 
__getattribute__, something like this I think:

     def __getattribute__(self, name):
         f = object.__getattribute__(self, name)
         # here you should really check whether it's a function
         def wrapper(self, *args, **kwargs)
             print "here 1!"
             try:
                 f(*args, **kwargs)
             except OSError, err:
                 print "here 2!"
                 if err.errno == errno.ESRCH:
                     raise NoSuchProcess
                 if err.errno == errno.EPERM:
                     raise AccessDenied
        return wrapper

That way "func" gets set to the wrapper function, which will handle your 
exception as you want.

:) David



More information about the Python-list mailing list