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?
The iterator created by a
forloop is private to the loop and is kept on the Python stack, left there byGET_ITERand picked up by each execution ofFOR_ITER—which is why you don’t see it among locals.FOR_ITERdoes terminate the loop by catchingStopIterationraised by the iterator, but this is tested by directly checking whethertp_iternextreturnedNULL, so the exception is caught and cleared before it gets a chance to propagate to a Python frame. But even if you had access to the iterator, there is little you’d be able to do with it, since Python iterators don’t support peeking.Since Python’s tracing mechanism doesn’t invoke the trace callback when a
forblock is entered or left, it appears you’ll need to resort to hacks such as processinglineevents to find that the loop is exited.