Skip to content

Instantly share code, notes, and snippets.

@kgriffs
Last active August 2, 2021 14:30
Show Gist options
  • Save kgriffs/9004906 to your computer and use it in GitHub Desktop.
Save kgriffs/9004906 to your computer and use it in GitHub Desktop.
How to trace function entries and exits in Python without using decorators. Excerpt from: http://blog.dscpl.com.au/2014/02/performance-overhead-of-using-decorators.html

In order to trace the execution of our code we can use Python's profile hooks mechanism.

import sys 

def tracer(frame, event, arg):
    print(frame.f_code.co_name, event) 

sys.setprofile(tracer) 

call_something(...)

The purpose of the profile hook is to allow you to register a callback function which is called on the entry and exit of all functions. Using this was can trace the sequence of function calls that are being made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment