473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Python interceptor package

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
Jul 18 '05 #1
4 3648
Fritz Bosch wrote:
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

Thanks
Fritz

Jul 18 '05 #2
Dominic <no****@nospam.no> wrote in message news:<ca**********@news.uni-kl.de>..
....
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
Jul 18 '05 #3
Fritz Bosch wrote:
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
Jul 18 '05 #4
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)
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
8
by: Georg Brandl | last post by:
Hello c.l.py, what features would you expect of a Python package manager, similar to CPAN or rubygems? I am currently planning to write such a thing, at first privately for myself, and if...
0
by: oliver.wulff | last post by:
I've developed a framework for ASP.NET with a custom authentication module (HttpModule). This module is already working. This framework should also delegate the authenticated principal...
0
by: | last post by:
Greetings. In an effort to get python2.4 on my Centos 3.7, I installed the python bootstrap rpm. This installed 2.4 alongside 2.2 and updated yum to 2.4.0. Oddly, it didn't create a symlink...
4
by: Alia Khouri | last post by:
Can we open up the discussion here about how to improve setuptools which has become the de facto standard for distributing / installing python software. I've been playing around with ruby's gems...
0
by: Steven Samuel Cole | last post by:
Hi Stephane, thanks for your reply! :-) I do not get any notification or warning or whatever from dpkg, all output I get when running # sudo dpkg -i python-<package name>_0.0.1-4927-1_all.deb...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.