Python interceptor package

F

Fritz Bosch

Hi group

I'm looking for a Python interceptor package, which will allow me to
intercept the invocation of any function or method call.

For those familiar with CORBA interceptors, this should be roughly
analogous (though simpler as we don't have a client and server side)
but should work for any native Python invocation.

A trivial but practical use of such interceptors could be to log such
invocations (also see the 'Interceptor pattern' from POSA2 by Douglas
Schmidt, et. al.)

In particular
- It should be possible to intercept
- Method calls (instance, class and static methods), either for
a specific instance or for all instances of a class (possibly
distinguishing between inherited and new or overridden methods)
- Function calls
- Calls to other callables
- It should not require any modification to existing modules
in a system
- It should be possible to install and remove such interceptors
at run-time (deriving a class and overriding a method is not
an option).
- It should be possible to install multiple interceptors (serving
different purposes) for any function/method

Has any work been done in this direction?

Thanks
Fritz
 
D

Dominic

Fritz said:
Hi group

I'm looking for a Python interceptor package, which will allow me to
intercept the invocation of any function or method call.

For those familiar with CORBA interceptors, this should be roughly
analogous (though simpler as we don't have a client and server side)
but should work for any native Python invocation.

A trivial but practical use of such interceptors could be to log such
invocations (also see the 'Interceptor pattern' from POSA2 by Douglas
Schmidt, et. al.)

In particular
- It should be possible to intercept
- Method calls (instance, class and static methods), either for
a specific instance or for all instances of a class (possibly
distinguishing between inherited and new or overridden methods)
- Function calls
- Calls to other callables
- It should not require any modification to existing modules
in a system
- It should be possible to install and remove such interceptors
at run-time (deriving a class and overriding a method is not
an option).
- It should be possible to install multiple interceptors (serving
different purposes) for any function/method

Has any work been done in this direction?
You seem to look for:
import sys
sys.settrace(...)
(Python interpreter hook), it should do the job.

Ciao,
Dominic
 
F

Fritz Bosch

Dominic said:
sys.settrace(...)
(Python interpreter hook), it should do the job.
....

What I'm looking for is an Interceptor class whose
constructor would interpose its (callable) instance
between a caller and a specified function or method,
and whose 'destroy' method would remove the interposition.

One would typically subclass the Interceptor class to
achieve the required interceptor behaviour.

sys.settrace looks like a possible starting point to create
such a package, allthough it is somewhat indiscriminate for my
purposes (I want to be selective about what I intercept).
Of course the selection can occur inside the trace function,
but the performance penalty may be to large (I need it in
a run-time environment, not only in a development
environment).

I have actually started to experiment with an Interceptor
class, whose constructor modifies the __dict__ of a specified
class, to 'wrap' its (callable) instance around the specified
method.

I wanted to make sure, though, that I'm not re-inventing the
wheel, and perhaps hear whether anyone would be interested in
such a package.

So, comments are invited!

Regards,
Fritz
 
P

Peter Otten

Fritz said:
What I'm looking for is an Interceptor class whose
constructor would interpose its (callable) instance
between a caller and a specified function or method,
and whose 'destroy' method would remove the interposition.
I have actually started to experiment with an Interceptor
class, whose constructor modifies the __dict__ of a specified
class, to 'wrap' its (callable) instance around the specified
method.
So, comments are invited!

Maybe there isn't such a package because the following is often sufficient:

def wrap(method):
def wrapped(self, *args, **kw):
print "begin"
method(self, *args, **kw)
print "end"
return wrapped


class Test(object):
def method(self, name):
print "method(%r)" % name

t = Test()
t.method("pure")
Test.method = wrap(Test.method)
t.method("wrapped")

Peter
 
D

Dominic

I've attached two source fragments which
I had used to wrap/intercept objects.
"weave" can be used to plug between an
object (like a proxy).
It temporarily modifies "self" so that even
method calls inside an object can
be trapped from the outside.
(Sorry could not find a use
case and I'm too lazy to write one
right now.)
The "trace_all" just wraps functions
inside a module.
Well, it probably won't help you much.
But who knows. Maybe you come up with an
idea :)

Ciao,
Dominic

=============================================
=============================================

def weave(delegate, into, for_):
class X(object):
def __getattribute__(self, key):
def wrapper(*args,**kargs):
class XX(object):
def __getattribute__(self, key):
if getattr(delegate, key, None):
return getattr(delegate, key)
else:
return getattr(into, key)
def __setattr__(self, key, value):
if getattr(delegate, key, None):
setattr(delegate, key, value)
else:
setattr(into, key, value)
return getattr(into.__class__,key).im_func(XX(), *args, **kargs)


if key in for_: # catch attribute
if getattr(into, key, None): #
return getattr(into, key) #
else:
return wrapper # catch method
elif getattr(delegate, key, None):
return getattr(delegate, key)
else:
return getattr(into, key)

def __setattr__(self, key, value):
print key, value
setattr(into, key, value)

return X()

=============================================
=============================================

# wrap stuff here
from trace import trace_all
trace_all('fork', locals())

---------------------------------

@file ../prototype/trace.py

indent = 0
hash = {}

def trace(name, f):
def wrapper(*args,**kargs):
"""wrapped"""
global indent, hash
print " "*indent + "%s:%s(%s,%s)" % (name, f.func_name, args, kargs)
indent = indent + 1
result = f(*args,**kargs)
indent = indent - 1
hash[name] = hash.get(name,{})
hash[name][f.func_name] = hash[name].get(f.func_name,0) + 1
return result
return wrapper

from types import FunctionType

def trace_all(name, d):
for k,v in d.items():
if type(v) == FunctionType and k != 'trace_all' and v.func_doc !=
'wrapped':
#print "Wrap %s" % k
d[k] = trace(name, v)
 

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
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top