aborting without killing the python interpreter

R

Russ

I wrote a simple little function for exiting with an error message:

def error ( message ): print_stack(); exit ("\nERROR: " + message +
"\n")

It works fine for executing as a script, but when I run it
interactively in the python interpreter it kills the interpreter.
That's not what I want. Is there a simple way to have a script
terminate but not have it kill the python interpreter when I run it
interactively? I suspect I may need to use exceptions, but I'm hoping
not to need them. Thanks.
 
T

Terry Reedy

Russ said:
I wrote a simple little function for exiting with an error message:

def error ( message ): print_stack(); exit ("\nERROR: " + message +
"\n")

It works fine for executing as a script,

How? In the standard interpreter, 'exit' is bound to the string
'Use Ctrl-Z plus Return to exit.'
so trying to call it as a function fails.

but when I run it
interactively in the python interpreter it kills the interpreter.
That's not what I want. Is there a simple way to have a script
terminate but not have it kill the python interpreter when I run it
interactively? I suspect I may need to use exceptions, but I'm hoping
not to need them. Thanks.

The interactive interpreter runs a statement at a time and gives a prompt
after any output. From a command shell, you can use a flag (-i I think) to
enter interactive mode after the script end.

Terry Jan Reedy
 
T

Terry Reedy

<correction>

Terry Reedy said:
How? In the standard interpreter, 'exit' is bound to the string
'Use Ctrl-Z plus Return to exit.'

This is, of course, Windows specific. Other systems have other strings.
 
E

Erik Max Francis

Terry said:
How? In the standard interpreter, 'exit' is bound to the string
'Use Ctrl-Z plus Return to exit.'
so trying to call it as a function fails.

I'm _presuming_ there was a hidden `from sys import *` in there. Hence
calling exit with the string (the help for sys.exit shows that if a
string is passed in, it will be printed before the process exits with
failure -- something I wasn't aware of actually).
 
R

Robert Kern

Russ said:
I wrote a simple little function for exiting with an error message:

def error ( message ): print_stack(); exit ("\nERROR: " + message +
"\n")

It works fine for executing as a script, but when I run it
interactively in the python interpreter it kills the interpreter.
That's not what I want. Is there a simple way to have a script
terminate but not have it kill the python interpreter when I run it
interactively? I suspect I may need to use exceptions, but I'm hoping
not to need them. Thanks.

Exceptions do *exactly* what you want in a very clean and simple way. They are a
fundamental feature of Python. Do not fear them. They are your friends.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
F

Fabrizio Milo

import sys

def main():
print 'exiting'
sys.exit()

try:
main()
except SystemExit:
pass

I suspect I may need to use exceptions, but I'm hoping
not to need them. Thanks.

Use the Exceptions!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top