__getattribute__ and methods proxying

G

Giampaolo Rodolà

Hi,
I have a class which looks like the one below.
What I'm trying to accomplish is to "wrap" all my method calls and
attribute lookups into a "proxy" method which translates certain
exceptions into others.
The code below *apparently* works: the original method is called but
for some reason the "except" clause is totally ignored.

I thought __getattribute__ was designed for such kind of things
("proxying") but apparently it seems I was wrong.



class NoSuchProcess(Exception): pass
class AccessDenied(Exception): pass


class Process(object):

def __getattribute__(self, name):
# wrap all method calls and attributes lookups so that
# underlying OSError exceptions get translated into
# NSP and AD exceptions
try:
print "here 1!"
return object.__getattribute__(self, name)
except OSError, err:
print "here 2!"
if err.errno == errno.ESRCH:
raise NoSuchProcess
if err.errno == errno.EPERM:
raise AccessDenied

def cmdline(self):
raise OSError("bla bla")


proc = Process()
proc.cmdline()



--- Giampaolo
http://code.google.com/p/pyftpdlib
http://code.google.com/p/psutil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top