Skip to content

Instantly share code, notes, and snippets.

@jasonbot
Created January 29, 2014 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonbot/8679558 to your computer and use it in GitHub Desktop.
Save jasonbot/8679558 to your computer and use it in GitHub Desktop.
A terrible thing: print the source of every line of Python code as it executes in your scripts. Please don't actually use it. Please.
import inspect
import sys
def tracefunction(frame, event, arg):
if event == "line":
info = inspect.getframeinfo(frame)
fname, lineno, fn = info.filename, info.lineno, info.function
with open(fname, 'rb') as f:
line = [line.rstrip() for line in f][lineno - 1]
print "Function: {} (in file {}:{}) | {}".format(fname, fn, lineno, line)
return tracefunction
def registertracefunction():
sys.settrace(tracefunction)
registertracefunction()
def mainfunction():
for x in xrange(10):
print x * 5
if __name__ == "__main__":
mainfunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment