Wrap and intercept function calls

Dan Yamins dyamins at gmail.com
Wed Feb 17 11:04:24 EST 2010


Really, nobody has any idea about this?   (Sorry to repost.)

On Tue, Feb 16, 2010 at 7:29 PM, Dan Yamins <dyamins at gmail.com> wrote:

> Hi:
>
> I'm wondering what the best way to wrap and modify function calls is.
> Essentially what I want to achieve is to have a function like this:
>
> def Wrap(frame,event,arg):
>      if event == 'call':
>         result = PerformCheck(GetArgumentsFromFrame(frame))
>         if Condition(result):
>             return result
>         else:
>             return [normal function call]
>
> called whenever a "call" event is about to occur.
>
> When I say "return result" I mean:  return that data object instead of what
> the function would have returned, and prevent execution of the function.
>
>
> Is there any way to do this using sys.settrace?  Or perhaps something from
> the bdb or pbd module?
>
>
In other words, what I'm looking for is a way to intercept all function
calls with a "wrapper" --  like one can do using settrace and other
debugging methods -- but, unlike the debugging methods, have the "wrapping"
function actually be able to intervene in the stack and, for instance,
conditionally replace the function call's return with something determined
in the wrapping function and prevent the function call's execution.   I want
to be able to do this by setting a single system-wide (or at any rate,
thread-wide) value, like with settrace, and not have to modify individual
functions one by one.

Could I, for example, set a settrace function that somehow modifies the
stack?  Or is there some much better way to do this?  Or, if someone can
tell me that this can't be done without having to roll my own implementation
of the Python interpreter, that would be great to know too.

Thanks again,
Dan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100217/ea79507b/attachment-0001.html>


More information about the Python-list mailing list