Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8724545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:51:18+00:00 2026-06-13T07:51:18+00:00

I’m interested in tracing the execution of a for loop. Specifically, I would like

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:51:19+00:00Added an answer on June 13, 2026 at 7:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
I need a function that will clean a strings' special characters. I do NOT
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.