1

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?

karlw
  • 668
  • 4
  • 13
  • Using a debugger such as `pudb` is an option? – arturhoo Oct 21 '12 at 15:59
  • I don't thing so. From a very brief look at what pudb is, it seems that it very similar to pdb, but with a nicer gui. I'm not interested in single step code execution/debugging, but hooking code execution, and displaying event that occur. I could be wrong on what pudb offers, so please correct me. – karlw Oct 21 '12 at 16:24

1 Answers1

3

The iterator created by a for loop is private to the loop and is kept on the Python stack, left there by GET_ITER and picked up by each execution of FOR_ITER—which is why you don't see it among locals.

FOR_ITER does terminate the loop by catching StopIteration raised by the iterator, but this is tested by directly checking whether tp_iternext returned NULL, 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 for block is entered or left, it appears you'll need to resort to hacks such as processing line events to find that the loop is exited.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • Great answer. I'm going to be walking down the "hacks such as processing line events" route. – karlw Oct 21 '12 at 23:10
  • Could you take a look at my question here?https://stackoverflow.com/questions/30770628/how-can-i-tell-if-a-function-is-called-in-a-loop-within-pythons-tracer-function – NeoWang Jun 12 '15 at 10:41