'break' Causes Execution of Procedure?

Jeff Shannon jeff at ccvcorp.com
Wed Aug 11 14:42:29 EDT 2004


Scott Brady Drummonds wrote:

>Hi, everyone,
>
>I have a bug in a script of several hundred lines of code that I cannot
>figure out.  I have attempted (unsuccessfully) to duplicate this problem in
>a smaller script that I can post here but have been unsuccessful.  As such,
>I'm posting code snippets here in the hopes that someone recognizes a very
>basic mistake I've made and can straighten me out.
>
>First, the portion of my text file that executes my code:
>
><quote>
>if __name__ == '__main__':
>  if len(sys.argv) != 2:
>    print('usage: %s <config_file>' % sys.argv[0])
>    sys.exit()
>
>  main(sys.argv[1])
></quote>
>
>Here are the first few lines of my 'main' procedure:
><quote>
>def main(fileName):
>  print('DEBUG: begin main procedure')
>  ...
></quote>
>
>And here is the part that is causing problems:
><quote>
>    #  'keys' is a list of integers returned from dict.keys()
>    for cycle in keys:
>      if key in skipList:
>        print ('skipping %d/%d' % (cycle, cycleMap[cycle]))
>        continue
>      try:
>        print('DEBUG: advancing both simulators to %d' % cycle)
>        procedureThatCouldRaiseEOFError()
>      except EOFError:
>        print('DEBUG: encountered EOFError; breaking loop')
>        break
>    print('DEBUG: All cycles processed')
></quote>
>
>Here's what I can't figure out.  I'm seeing the following text generated
>during this script's execution:
><quote>
>DEBUG: advancing both simulators to 20178
>DEBUG: encountered EOFError; breaking loop
>DEBUG: begin main procedure
></quote>
>
>How is it possible that the call to 'break' is seemingly being replaced with
>a call to 'main'?
>  
>

I suspect that the answer is in the context of the for loop.  Try 
posting a bit more of the surrounding code.  (For example, it's not at 
all clear how your loop relates to the main() snippet that you posted...)

Actually, I'm wondering why you catch the exception inside the loop, and 
then exit the loop.  If you wrapped that entire loop in the try/except, 
then you wouldn't need to worry about using break -- an exception would 
end the loop and *then* get dealt with.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list