How to terminate a main script?

H

Helmut Jarausch

Hi,

I'm still looking for an elegant and clear means to
terminate the main script in Python.

Unfortunately, Python doesn't allow a 'return'
instruction in the main script.

Using sys.exit(0) produces an error
message which looks dangerous to an
uninitiated user.

The same is true for an exception.

And setting a 'flag' and testing
at several place for 'fall through'
is ugly and error-prone.

So what is a good choice?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
F

Fredrik Lundh

Helmut said:
Using sys.exit(0) produces an error
message which looks dangerous to an
uninitiated user.

sys.exit(0) doesn't print anything at all.

$ python$

however, sys.exit() raises an exception to tell the runtime that it wants
to terminate the program, so if you're using a catch-all exception handler
that treats all exceptions as dangerous errors, it'll look like a dangerous
error too.

the solution is simple: don't do that. instead of writing:

try:
...
except:
print "OMG! Ponies!"

write

try:
...
except (SystemExit, KeyboardInterupt):
raise
except:
print "OMG! Ponies!"

</F>
 
G

Ganesan Rajagopal

Helmut Jarausch said:
Using sys.exit(0) produces an error message which looks dangerous to an
uninitiated user.

What message? Your program should exit silently when you call sys.exit(0).

Ganesan
 
H

Helmut Jarausch

Fredrik said:
sys.exit(0) doesn't print anything at all.

Yes, sorry, I was trying in in 'idle'
There you get
Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
sys.exit(0)
SystemExit: 0


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
H

Helmut Jarausch

Fredrik said:
sys.exit(0) doesn't print anything at all.

Yes, sorry, I was trying in in 'idle'
There you get
Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
sys.exit(0)
SystemExit: 0


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
D

Dennis Lee Bieber

Yes, sorry, I was trying in in 'idle'
There you get
Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
sys.exit(0)
SystemExit: 0

Reasonable, as IDLE is a Python program itself, and probably
implements a top-level handler to report anything that is unhandled by a
test-run of a script.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
T

Tal Einat

Helmut said:
Hi,

I'm still looking for an elegant and clear means to
terminate the main script in Python.

Unfortunately, Python doesn't allow a 'return'
instruction in the main script.
It is quite a common practice for Python scripts to define a main()
function which contains the actual code, and have "main()" on the last
line of the file. This allows you to just 'return' from wherever you
like in your code.

It is even more common to use this in the end instead of "main()":
if __name__ == "__main__":
main()

This way, if the file is imported as a module, the main() function
won't execute, but if it is run directly from a command line or using
execfile() it will execute.

Enjoy!

- Tal
 

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
474,470
Messages
2,571,807
Members
48,797
Latest member
PeterSimpson
Top