[Tutor] import sys; sys.exit()

Andrew Volmensky warewerks at verizon.net
Thu Jan 10 22:17:20 CET 2008


Thanks Alan,

When typing the commands [statements?] in the terminal I get this:

Last login: Sat Jan  5 22:20:44 on ttyp2
Welcome to Darwin!
warewerks-01:~ andrew$ python
Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> import sys
 >>> sys.exit()
warewerks-01:~ andrew$

Does that behavior look correct? Apparently I have exit out of the  
Python interpreter; yes?


Kent - also thank you! I hope "exceptions" are covered in a following  
tutorial!

> ...and get an error. I have tried saving as exit.py from the  
> editor  and trying to run it and also entering the commands  
> directly into the  shell. My understanding is that this is supposed  
> to exit the program,  but that does not appear to be happening.
>

sys.exit() doesn't do what you might think - it doesn't force an exit  
from the current process. All it does is raise a SystemExit  
exception. How this is handled depends on the context - SystemExit  
can be caught by an enclosing except handler just like any other  
exception.

If you are running a program directly, SystemExit will probably  
propagate to the interpreter and cause the program to exit, just as  
any other uncaught exception would.

Running in IDLE or another shell, the exception is caught and logged  
but it doesn't abort the process.

Kent


On Jan 10, 2008, at 9:33 AM, Alan Gauld wrote:

> "Andrew Volmensky" <warewerks at verizon.net> wrote
>
>> I am going through the tutorial "Simple Sequences" here: http://
>> www.freenetpages.co.uk/hp/alan.gauld/
>
> Good choice ;-)
>
>> ...and get an error. I have tried saving as exit.py from the editor
>> and trying to run it
>
> How are you running it?
> You need to run it from the Terminal application.
> If you are using an IDE environment like IDLE the tool will
> catch the attempt to exit and display a warning - which
> is what you appear to be seeing.
>
>> and also entering the commands directly into the
>> shell.
>
> Again if the shell is the one you get by typing python
> at the Terminal prompt it should work and exit python.
> But if you are using an IDE prompt such as IDLE then
> IDLE will catch the attempt to exit.
>
>> This is with MacPython 2.4 - Thanks!
>>
>> IDLE 1.1.4
>>>>> ================================ RESTART
>> ================================
>>>>>
>>
>> Traceback (most recent call last):
>>   File "/Users/andrew/python/exit.py", line 2, in -toplevel-
>>     sys.exit( )
>> SystemExit
>>
>>
>>>>> import sys
>>>>> sys.exit()
>>
>> Traceback (most recent call last):
>>   File "<pyshell#1>", line 1, in -toplevel-
>>     sys.exit()
>> SystemExit
>>>>>
>
> This looks like the expected behaviour inside IDLE.
>
> I'll add a note to that effect to the web page, thanks for
> pointing it out.
>
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list