This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: continue inside try confuses while loop
Type: Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: twouters Nosy List: amitp, ghaering, gvanrossum, twouters
Priority: normal Keywords:

Created on 2001-09-19 17:46 by amitp, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
python-continue-bug.py amitp, 2001-09-19 17:50 Example code illustrating bug
python-continue-bug-2.py amitp, 2001-09-19 20:18 Example code causing SystemError
Messages (6)
msg6590 - (view) Author: Amit Patel (amitp) Date: 2001-09-19 17:46
If there's a continue inside a try inside a while with
a break inside a function, well, things go wacky.  I'll
post code and give a description:

#!/usr/bin/env python2

def bug(n):
  print 'Top'
  x = 0
  while 1:
    print 'x = %d' % x
    x = x + 1
    if x > n: break
    try:
      y = float(42)
      if y > 10:
        continue
    except ValueError:
      print 'Error'
  print 'Bottom'

bug(4)  # no loop
bug(3)  # loops



When you run bug(4), it does what you expect -- prints
Top, prints x= for various values of x, then prints
Bottom, and returns.

When you run bug(3), it prints Top, various values of
x, and then instead of 'break' getting out of the loop,
it jumps to the top of the function and prints Top again !!

This seems really weird to me.  Odd values of n behave
differently than even values of n !

msg6591 - (view) Author: Amit Patel (amitp) Date: 2001-09-19 17:50
Logged In: YES 
user_id=327765

Bah, it destroyed my formatting.  I'll upload a separate file.
msg6592 - (view) Author: Amit Patel (amitp) Date: 2001-09-19 20:18
Logged In: YES 
user_id=327765

I'm attaching a variant of the program which runs and then
causes "SystemError: unknown opcode".
msg6593 - (view) Author: Gerhard Häring (ghaering) * (Python committer) Date: 2001-09-20 02:42
Logged In: YES 
user_id=163326

FYI: Python 1.5.2 and 2.0.1 say:

gerhard@lilith:/tmp > /opt/python20/bin/python 
python-continue-bug-2.py
SyntaxError: 'continue' not supported inside 'try' clause 
(python-continue-bug-2.py, line 10)

This problem seems to have started with the 2.1 branch.
msg6594 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-09-20 04:50
Logged In: YES 
user_id=6380

I can reproduce this in 2.2 too.  Thomas, can you look at
this? I believe you wrote the continue-in-try code...
msg6595 - (view) Author: Thomas Wouters (twouters) * (Python committer) Date: 2001-09-24 19:35
Logged In: YES 
user_id=34209

Gah. found it. How embarrassing (as I suspected.) My only solace 
is that Jeremy accepted the patch <wink>. The ugly hack of 
re-creating the just-popped frame-block in the case of a continue 
swapped the 'handler' and 'stacklevel' arguments to 
PyBlock_BlockSetup! And any subsequent break or exception would 
use the wrong handler and the wrong stack level to pop to.

Don't ask me *how* I found it; I'm afraid something will go wrong 
with the fabric of space if I admit I found such an obscure bug 
with such little effort :)

Fixed in revision 2.277 of Python/ceval.c.

History
Date User Action Args
2022-04-10 16:04:27adminsetgithub: 35206
2001-09-19 17:46:12amitpcreate