Wrap and intercept function calls
debatem1 at gmail.com
Thu Feb 18 02:44:11 CET 2010
On Wed, Feb 17, 2010 at 11:04 AM, Dan Yamins <dyamins at gmail.com> wrote:
> 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:
>> 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
>> 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
>> 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,
You could drill down through everything in globals() etc, replacing
functions as you went,
but its fragile (there are probably unworkable corner cases), ugly,
and likely to be slow.
What exactly is it you're trying to do?
More information about the Python-list