FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Gentoo > Gentoo Alt

 
 
LinkBack Thread Tools
 
Old 09-25-2012, 03:04 PM
Will Woods
 
Default Coding Style: exiting python programs

Hey all - here is a quick note about the various ways to exit python
programs. Consider this a suggestion for a future CodingStyle / New
Developer FAQ, or something.


sys.exit(returncode) really just does:
raise SystemExit(returncode)
So we can use that rather than importing sys just for sys.exit().

Note that SystemExit is a subclass of Exception, so if you're handling
Exception anywhere, you're going to catch SystemExit/sys.exit().
(This is another reason 'except Exception: ..." is a bad idea.)

As pointed out elsewhere, if a thread catches SystemExit the thread will
exit, but not the entire program.

If you really want to exit immediately without dealing with exception
handling or cleanup or threading, there's os._exit(). This should cause
a threaded python program to exit even if called from a thread.


Hope that helps someone,

-w

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 
Old 09-26-2012, 12:06 PM
Amit Saha
 
Default Coding Style: exiting python programs

On Wed, Sep 26, 2012 at 1:04 AM, Will Woods <wwoods@redhat.com> wrote:
> Hey all - here is a quick note about the various ways to exit python
> programs. Consider this a suggestion for a future CodingStyle / New
> Developer FAQ, or something.
>
>
> sys.exit(returncode) really just does:
> raise SystemExit(returncode)
> So we can use that rather than importing sys just for sys.exit().
>
> Note that SystemExit is a subclass of Exception, so if you're handling
> Exception anywhere, you're going to catch SystemExit/sys.exit().
> (This is another reason 'except Exception: ..." is a bad idea.)

Thanks for the tip. Perhaps you should add that SystemExit is a
subclass of BaseException and not Exception starting Python 2.5 [1].

[1] http://docs.python.org/library/exceptions.html#exceptions.SystemExit

(Thanks to Nick Coghlan for pointing this to me when i shared your tip
on twitter).

Cheers,
Amit.

--
http://echorand.me

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 
Old 09-26-2012, 02:50 PM
David Lehman
 
Default Coding Style: exiting python programs

On Wed, 2012-09-26 at 22:06 +1000, Amit Saha wrote:
> On Wed, Sep 26, 2012 at 1:04 AM, Will Woods <wwoods@redhat.com> wrote:
> > Hey all - here is a quick note about the various ways to exit python
> > programs. Consider this a suggestion for a future CodingStyle / New
> > Developer FAQ, or something.
> >
> >
> > sys.exit(returncode) really just does:
> > raise SystemExit(returncode)
> > So we can use that rather than importing sys just for sys.exit().
> >
> > Note that SystemExit is a subclass of Exception, so if you're handling
> > Exception anywhere, you're going to catch SystemExit/sys.exit().
> > (This is another reason 'except Exception: ..." is a bad idea.)
>
> Thanks for the tip. Perhaps you should add that SystemExit is a
> subclass of BaseException and not Exception starting Python 2.5 [1].

Right. This is why we went from a bare 'except' to 'except Exception' in
much of anaconda a couple of years ago -- to not catch SystemExit.

>
> [1] http://docs.python.org/library/exceptions.html#exceptions.SystemExit
>
> (Thanks to Nick Coghlan for pointing this to me when i shared your tip
> on twitter).
>
> Cheers,
> Amit.
>


_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 
Old 09-26-2012, 03:06 PM
Martin Sivak
 
Default Coding Style: exiting python programs

Hi,

> > Thanks for the tip. Perhaps you should add that SystemExit is a
> > subclass of BaseException and not Exception starting Python 2.5
> > [1].
>
> Right. This is why we went from a bare 'except' to 'except Exception'
> in
> much of anaconda a couple of years ago -- to not catch SystemExit.
>

Which is also wrong, because SyntaxError is still inherited from Exception..

Martin

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 

Thread Tools




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

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org