Skip to content

Instantly share code, notes, and snippets.

@prasoon2211
Last active August 29, 2015 13:56
Show Gist options
  • Save prasoon2211/9285630 to your computer and use it in GitHub Desktop.
Save prasoon2211/9285630 to your computer and use it in GitHub Desktop.
pytrace
import sys, linecache
def tracefunc(frame, event, arg, indent=[0]):
if event == "call" or event == "return":
lineno = frame.f_lineno
filename = frame.f_globals["__file__"]
if filename == "<stdin>":
filename = "traceit.py"
if (filename.endswith(".pyc") or
filename.endswith(".pyo")):
filename = filename[:-1]
if event == "call":
indent[0] += 2
print "-" * indent[0] + "> call function", frame.f_code.co_name, filename, lineno
elif event == "return":
print "<" + "-" * indent[0], "exit function", frame.f_code.co_name, filename, lineno
indent[0] -= 2
return tracefunc
sys.settrace(tracefunc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment