Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   I don't get why sys.exit(1) doesn't exit the while loop in the follow case (http://www.velocityreviews.com/forums/t734744-i-dont-get-why-sys-exit-1-doesnt-exit-the-while-loop-in-the-follow-case.html)

chad 10-05-2010 02:38 AM

I don't get why sys.exit(1) doesn't exit the while loop in the follow case
 
Given the following..

#!/usr/bin/python

import urllib2
import sys
import time

while 1:
try:
con = urllib2.urlopen("http://www.google.com")
data = con.read()
print "connected"
#The loop doesn't exit if I use sys.exit(1)
break
except:
time.sleep(2)


If I would replace 'break' with 'sys.exit(1)', the while loop will
keep printing connected every 2 seconds? Why I this? I thought exit
meant exit.

Justin Ezequiel 10-05-2010 02:57 AM

Re: I don't get why sys.exit(1) doesn't exit the while loop in thefollow case
 
your bare except is catching the SystemExit raised by sys.exit(1)

Terry Reedy 10-05-2010 03:02 AM

Re: I don't get why sys.exit(1) doesn't exit the while loop in thefollow case
 
On 10/4/2010 10:38 PM, chad wrote:
> Given the following..
>
> #!/usr/bin/python
>
> import urllib2
> import sys
> import time
>
> while 1:
> try:
> con = urllib2.urlopen("http://www.google.com")
> data = con.read()
> print "connected"
> #The loop doesn't exit if I use sys.exit(1)


Guess how sys.exit is implemented (or check the fine library manual
chapter on the sys module, .exit entry, 2nd sentence).

> break
> except:


and guess what this does, and why bare excepts are not recommended
unless you mean what you say...

> time.sleep(2)


--
Terry Jan Reedy


Von 10-05-2010 03:13 AM

Re: I don't get why sys.exit(1) doesn't exit the while loop in thefollow case
 
Try to use sys.exit(0)
Maybe you should print out the error in your except block.


2010/10/5, chad <cdalten@gmail.com>:
> Given the following..
>
> #!/usr/bin/python
>
> import urllib2
> import sys
> import time
>
> while 1:
> try:
> con = urllib2.urlopen("http://www.google.com")
> data = con.read()
> print "connected"
> #The loop doesn't exit if I use sys.exit(1)
> break
> except:
> time.sleep(2)
>
>
> If I would replace 'break' with 'sys.exit(1)', the while loop will
> keep printing connected every 2 seconds? Why I this? I thought exit
> meant exit.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Steven D'Aprano 10-05-2010 04:42 AM

Re: I don't get why sys.exit(1) doesn't exit the while loop in thefollow case
 
On Tue, 05 Oct 2010 13:57:11 +1100, Ben Finney wrote:

> chad <cdalten@gmail.com> writes:
>
>> while 1:

>
> A minor point: this is more explanatory and less misleading if you write
> it as ‘while True’.


Why is it misleading? Is there some circumstance in Python where the
literal 1 could have a false value?

"while 1" was the accepted idiom for infinite loops in Python for many
years, before the introduction of bools in (I think) Python 2.2. "while
1" is used used as a micro-optimization in versions of Python below (I
think) 2.7. You might prefer "while True" as nicer or even more Pythonic,
but I disagree that "while 1" is misleading.




--
Steven

Lawrence D'Oliveiro 10-05-2010 05:48 AM

Re: I don't get why sys.exit(1) doesn't exit the while loop in the follow case
 
In message <87iq1hz6rc.fsf@benfinney.id.au>, Ben Finney wrote:

> Don't ever use a bare ‘except’ unless you know exactly why you're doing
> so.


In other news, don’t ever put a loaded gun in your mouth and pull the
trigger unless you know exactly why you’re doing so.

Some people have a problem. They say, “I know, I’ll use an exception”. Now
they have Some people have a problem. They say, “I know, I’ll use an
exception”. Now they have ...

Seebs 10-05-2010 06:07 AM

Re: I don't get why sys.exit(1) doesn't exit the while loop in thefollow case
 
On 2010-10-05, Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> wrote:
> In message <87iq1hz6rc.fsf@benfinney.id.au>, Ben Finney wrote:
>> Don't ever use a bare ???except??? unless you know exactly why you're doing
>> so.


> In other news, don???t ever put a loaded gun in your mouth and pull the
> trigger unless you know exactly why you???re doing so.


I don't think those are at all comparable, I've heard of people who had
plausible arguments for the gun.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Lawrence D'Oliveiro 10-05-2010 07:26 AM

Re: I don't get why sys.exit(1) doesn't exit the while loop in the follow case
 
In message <slrnialg8v.28c5.usenet-nospam@guild.seebs.net>, Seebs wrote:

> On 2010-10-05, Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand>
> wrote:
>
>> In message <87iq1hz6rc.fsf@benfinney.id.au>, Ben Finney wrote:
>>
>>> Don't ever use a bare ‘except’ unless you know exactly why you're doing
>>> so.

>>
>> In other news, don’t ever put a loaded gun in your mouth and pull the
>> trigger unless you know exactly why you’re doing so.

>
> I don't think those are at all comparable, I've heard of people who had
> plausible arguments for the gun.


:)

Steven D'Aprano 10-05-2010 07:41 AM

Re: I don't get why sys.exit(1) doesn't exit the while loop in thefollow case
 
On Tue, 05 Oct 2010 11:13:53 +0800, Von wrote:

> Try to use sys.exit(0)
> Maybe you should print out the error in your except block.


Not exiting with a status-code of 0 is no more helpful than not exiting
with a status-code of 1.

It's actually *less* helpful, if the intention is actually to exit with a
non-zero exit status.



--
Steven

Nobody 10-05-2010 08:45 PM

Re: I don't get why sys.exit(1) doesn't exit the while loop in the follow case
 
On Tue, 05 Oct 2010 13:57:11 +1100, Ben Finney wrote:

> Here's your problem. Don't ever use a bare ‘except’ unless you know
> exactly why you're doing so. Rather, figure out what exception types you
> want to catch, and catch *only* those types.


If I use a bare except, I usually have a good reason, namely that the
documentation doesn't actually mention which exceptions can be raised.
This is usually because the code doesn't actually know which exceptions
can be raised.

If a module defines:

def foo(f):
f.bar()
...

the set of exceptions which foo() can raise is limitless. The user can
pass whatever they like as "f", and its bar() method can raise anything.

Knowing which exceptions a function or method can raise is the exception
rather than the rule.

If I'm catching exceptions in order to perform clean-up, I'll use a bare
except and re-raise the exception afterwards. In that situation, a bare
except is usually the right thing to do.

If I'm not going to re-raise the exception, I'll either catch Exception or
add explicit catches for SystemExit and KeyboardInterrupt which re-raise
the exception.



All times are GMT. The time now is 10:20 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.