Solved! Go to Solution.
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()
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()
Isn't that what a debugger is for?
You could do this, but it's a terrible idea and will slow your program down by orders of magnitude: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()
That is, paste the code up through registertracefunction() line to the top of your script and it will print out every line it's on.