tracing function calls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stormbringer

    #1

    tracing function calls

    Greetings,

    I've been wondering if there is a mechanism similar to trace/untrace
    found in lisp, for example call trace(function-name) and whenever this
    function is called it will show its parameters to stdout ....

  • Fredrik Lundh

    #2
    Re: tracing function calls

    "Stormbring er" wrote:
    [color=blue]
    > I've been wondering if there is a mechanism similar to trace/untrace
    > found in lisp, for example call trace(function-name) and whenever this
    > function is called it will show its parameters to stdout ....[/color]

    def trace(func):
    def tracer(*args, **kwargs):
    print func.__name__, args, kwargs
    result = func(*args, **kwargs)
    print func.__name__, "return", result
    return result
    return tracer

    def myfunc(a, b, c):
    return a + b + c

    myfunc = trace(myfunc)

    myfunc(1, 2, 3)

    (tweak as necessary)

    </F>

    Comment

    • Stormbringer

      #3
      Re: tracing function calls

      Thank you Fredrik !
      With a little tweaking for the right indentation it should prove useful
      :)

      Fredrik Lundh wrote:[color=blue]
      > "Stormbring er" wrote:
      >[color=green]
      > > I've been wondering if there is a mechanism similar to[/color][/color]
      trace/untrace[color=blue][color=green]
      > > found in lisp, for example call trace(function-name) and whenever[/color][/color]
      this[color=blue][color=green]
      > > function is called it will show its parameters to stdout ....[/color]
      >
      > def trace(func):
      > def tracer(*args, **kwargs):
      > print func.__name__, args, kwargs
      > result = func(*args, **kwargs)
      > print func.__name__, "return", result
      > return result
      > return tracer
      >
      > def myfunc(a, b, c):
      > return a + b + c
      >
      > myfunc = trace(myfunc)
      >
      > myfunc(1, 2, 3)
      >
      > (tweak as necessary)
      >
      > </F>[/color]

      Comment

      Working...