Python Forum
diagnosing break not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
diagnosing break not working
#1
i have this code in a large loop:
    for retry in range(128):

        ... lots of code to set up a cloud API call, do the call, and scan the results.

        print('usevpcs =',repr(usevpcs))
        if usevpcs:
            print('break out of the retry loop')
            break # this should break out of for retry but it does not
        print('staying in the retry loop',retry)

        ... more code to react to a failed call attempt
variable usevpcs is an empty list unless a vpc is found and appended to it by previous code. when i run this script i get output like:
Output:
usevpcs = ['vpc-028f01f264af6bb1b'] break out of the retry loop staying in the retry loop 29
so it looks like usevpcs has a string in it (a successful cloud API call) and tests True by the if statement, but the break statement doesn't break out.

how can i diagnose what is going on with break?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You can investigate what's going on using pdb like this:
    print('usevpcs =',repr(usevpcs))
    if usevpcs:
        print('break out of the retry loop')

        import pdb; pdb.set_trace()   # the code will stop here allowing you to enter "n" or "s" command

        break # this should break out of for retry but it does not
    print('staying in the retry loop',retry)
After the code stops you can input "n" or "s" and press enter to execute next line.

Quote:s(tep)
Execute the current line, stop at the first possible occasion
(either in a function that is called or in the current
function).

n(ext)
Continue execution until the next line in the current function
is reached or it returns.
Reply
#3
i have added a print('after break') call right after the break statement, indented exactly as break is, so it is in the same if body as break is. running that does get the output "after break". break is acting like pass.

i next tried a raise RuntimeError after the break and it successfully raises the exception and ends the execution of the script. maybe i can wrap the loop in a try: and let except RuntimeError: get me out of the loop.

i tried with pdb.set_trace(). it stopped and output the file name and line number of the break statement. i typed "n" and pressed enter. it then stopped at the print() call that i have after the end of the loop. so, break works when tracing it, but not when not tracing it. seems like an interpreter bug somewhere.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
i put the whole thing inside another loop for foo in range(1): and it is working fine. i changed tHE loop to if True: and it still works. lots of other stuff has also been changed as part of development and now it works just fine w/o the for or if surrounding it. i have no idea what could have caused this. it could be a cPython bug. and i can't rule out a system or CPU or RAM problem. very possibly some consistent systemic memory corruption event caused by something.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020