I'm interested in tracing the execution of a for loop. Specifically, I would like to print a message to the screen when the for loop exits. The cavait is that I do not have control over the for loop, that is, someone else is going to be pass my code the for loop to trace. Here is a some example code:
import sys
import linecache
def trace_calls(frame, event, arg):
if event != 'call':
return
co = frame.f_code
func_name = co.co_name
if func_name == 'write':
return
return trace_lines
def trace_lines(frame, event, arg):
if event != 'line':
return
co = frame.f_code
line_no = frame.f_lineno
filename = co.co_filename
print("%d %s"%(line_no, linecache.getline(filename, line_no)[:-1]))
def some_elses_code():
j = 0
for i in xrange(0,5):
j = j + i
return j
if __name__ == "__main__":
sys.settrace(trace_calls)
some_elses_code()
And the output for the code:
22 j = 0
23 for i in xrange(0,5):
24 j = j + i
23 for i in xrange(0,5):
24 j = j + i
23 for i in xrange(0,5):
24 j = j + i
23 for i in xrange(0,5):
24 j = j + i
23 for i in xrange(0,5):
24 j = j + i
23 for i in xrange(0,5):
25 return j
I know I could just look at the line number, see that the line that had been executed was not the next line, but that feels wrong. I would like to inspect the frame object that is passed into the trace_lines method and see that the iterator that is used in the for loop has no more items. This link says that the for loop catches an exception, then it knows that the iterator is all used up, but I am not able to see the any exception objects populated on the frame object (ie frame.f_exc_value is always None). Furthermore, I see no var in my local scope that is the iterator used in the for loop. Is something like this possible?