Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

2.1 loop with try & continue -> bug?

0 views
Skip to first unread message

Marco Beri

unread,
Nov 30, 2001, 5:33:45 PM11/30/01
to
Hi,
look at this simple program:

print "Running..."
cont=0
while -1 :
print cont
cont = cont + 1
try:
if cont == 2:
continue
if cont == 3:
break
except:
pass

If I run it with python 2.1, this is the output:
Running...
0
1
2
Running...
0
1
2
.
.
.

And so on, so it's clear that the "break" statement is never reached.
It seems that the "continue" included into the "try" statemente break
to an understood "while".
Why?
Thanks for your time.
Ciao.
Marco.
P.S. If I run it with python 1.5.2 at least I got an error.

Gerhard Häring

unread,
Nov 30, 2001, 5:47:16 PM11/30/01
to

It's a known and fixed problem:

http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=462937

Gerhard
--
mail: gerhard <at> bigfoot <dot> de registered Linux user #64239
web: http://www.cs.fhm.edu/~ifw00065/ OpenPGP public key id 86AB43C0
public key fingerprint: DEC1 1D02 5743 1159 CD20 A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))

Fernando Pérez

unread,
Nov 29, 2001, 11:49:41 AM11/29/01
to
Fernando Pérez wrote:


> Am I missing something?
>

Yes, I missed the bug report :)

Well, time for 2.2 I guess.

f.

Fernando Pérez

unread,
Nov 29, 2001, 11:46:37 AM11/29/01
to
Marco Beri wrote:

> Hi,
> look at this simple program:
>

[snip]

Very strange, and I'd say there appears to be a bug. Digging deeper:
from the docs we get:

******************
6.10 The continue statement

continue_stmt: "continue"

continue may only occur syntactically nested in a for or while
loop, but
not nested in a function or class definition or try statement within
that loop.6.1[1]It continues with the next cycle of the nearest
enclosing loop.


---------

Footnotes

... loop.6.1[2]
It may occur within an except or else clause. The restriction on
occurring in the try clause is implementor's laziness and will
eventually be lifted.

******************

So we know that technically you shouldn't be putting that continue in
your try. However, I found something quite bizarre. I put an extra
print in it and here's the code I ran:

print "Running..."
cont=0
while cont < 5 :


print cont
cont = cont + 1
try:
if cont == 2:
continue
if cont == 3:

print 'Break!'
break
except:
pass

Now for the strangeness: if this is run at the interactive
interpreter, the results are:

>>> print "Running..."
Running...
>>> cont=0
>>> while cont < 5 :
... print cont
... cont = cont + 1
... try:
... if cont == 2:
... continue
... if cont == 3:
... print 'Break!'
... break
... except:
... pass
...
0
1
2
Break!
3
4

However, running it at the command line with python:
Running...
0
1
2
Break!
Running...
0
1
2
Break! .... keeps going on forever, killed it with ctrl-C.

So, I suspect that the continue in the try is triggering abnormal
behavior in the python interpreter which for some reason isn't
triggered at the interactive prompt. It's ok if continue is invalid
inside try, but then an error should be raised. Having the *same*
code behave so differently between the interpreter and the
interactive prompts is a bug in my book.

Am I missing something?

Cheers,

f

Michael Hudson

unread,
Dec 3, 2001, 6:17:38 AM12/3/01
to
Fernando Pérez <fper...@yahoo.com> writes:

Or 2.1.2.

Cheers,
M.

--
If I had wanted your website to make noise I would have licked
my finger and rubbed it across the monitor.
-- signature of "istartedi" on slashdot.org

0 new messages