How to terminate a main script?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Helmut Jarausch

    #1

    How to terminate a main script?

    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
  • Fredrik Lundh

    #2
    Re: How to terminate a main script?

    Helmut Jarausch wrote:
    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
    >>import sys
    >>sys.exit(0)
    $

    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, KeyboardInterup t):
    raise
    except:
    print "OMG! Ponies!"

    </F>



    Comment

    • Ganesan Rajagopal

      #3
      Re: How to terminate a main script?

      >>>>Helmut Jarausch <jarausch@igpm. rwth-aachen.dewrites :
      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

      --
      Ganesan Rajagopal

      Comment

      • Helmut Jarausch

        #4
        Re: How to terminate a main script?

        Fredrik Lundh wrote:
        Helmut Jarausch wrote:
        >
        >Using sys.exit(0) produces an error
        >message which looks dangerous to an
        >uninitiated user.
        >
        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

        Comment

        • Helmut Jarausch

          #5
          Re: How to terminate a main script?

          Fredrik Lundh wrote:
          Helmut Jarausch wrote:
          >
          >Using sys.exit(0) produces an error
          >message which looks dangerous to an
          >uninitiated user.
          >
          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

          Comment

          • Tal Einat

            #6
            Re: How to terminate a main script?

            Helmut Jarausch wrote:
            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

            Comment

            Working...