sys.exit()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Voras

    sys.exit()

    In a code such as:

    if len(sys.argv) < 2:
    print "I need arguments!"
    sys.exit(1)

    Is sys.exit() really a good choice? Is there something more elegant? (I
    tried return but it is valid only in a function)


    --
    --
    Every sufficiently advanced magic is indistinguishab le from technology
    - Arthur C Anticlarke


  • Duncan Booth

    #2
    Re: sys.exit()

    "Ivan Voras" <ivoras@fer.h r> wrote in news:bm3msp$fjo $1@bagan.srce.h r:
    [color=blue]
    > In a code such as:
    >
    > if len(sys.argv) < 2:
    > print "I need arguments!"
    > sys.exit(1)
    >
    > Is sys.exit() really a good choice? Is there something more elegant? (I
    > tried return but it is valid only in a function)[/color]

    More elegant might be to put your code in a function which would let you
    use return, although even then you need to call sys.exit somewhere if you
    want to set a return code. Remember that sys.exit just throws an exception,
    so you can always catch it further out if you need to. Also you can combine
    the print into the call to sys.exit and save a line:

    import sys

    def main(args):
    if len(args) < 2:
    sys.exit("I need arguments!")

    print "rest of program..."

    if __name__=='__ma in__':
    main(sys.argv)

    --
    Duncan Booth duncan@rcp.co.u k
    int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
    "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

    Comment

    • Peter Hansen

      #3
      Re: sys.exit()

      Ivan Voras wrote:[color=blue]
      >
      > In a code such as:
      >
      > if len(sys.argv) < 2:
      > print "I need arguments!"
      > sys.exit(1)
      >
      > Is sys.exit() really a good choice? Is there something more elegant? (I
      > tried return but it is valid only in a function)[/color]

      sys.exit() is the proper, defined, cross-platform way to exit from
      a program and return a value to the calling program. Change your
      definition of elegant and you could consider it easily the most elegant
      of all solutions. ;-)

      -Peter

      Comment

      • Ivan Voras

        #4
        Re: sys.exit()

        Peter Hansen wrote:
        [color=blue]
        > Ivan Voras wrote:[color=green]
        >> Is sys.exit() really a good choice? Is there something more elegant?
        >> (I tried return but it is valid only in a function)[/color][/color]
        [color=blue]
        > sys.exit() is the proper, defined, cross-platform way to exit from
        > a program and return a value to the calling program. Change your
        > definition of elegant and you could consider it easily the most
        > elegant of all solutions. ;-)[/color]

        Ok. :)

        (Just for the record: I was looking for something that doesn't require a
        module import. But it is not important.)

        --
        --
        Every sufficiently advanced magic is indistinguishab le from technology
        - Arthur C Anticlarke


        Comment

        • Gerrit Holl

          #5
          Re: sys.exit()

          Ivan Voras wrote:[color=blue]
          > In a code such as:
          >
          > if len(sys.argv) < 2:
          > print "I need arguments!"
          > sys.exit(1)
          >
          > Is sys.exit() really a good choice? Is there something more elegant? (I
          > tried return but it is valid only in a function)[/color]

          An alternative that I often choose is:

          raise SystemExit("I need arguments!")

          This is the same in one line, and I think it is more elegant, because it
          is higher-level: you are not using the low-level interface of error codes,
          a non-programmer may not understand what '1' means, usually it means success
          so that can be very confusing. I prefer raise SystemExit.

          Gerrit.

          --
          30. If a chieftain or a man leave his house, garden, and field and
          hires it out, and some one else takes possession of his house, garden, and
          field and uses it for three years: if the first owner return and claims
          his house, garden, and field, it shall not be given to him, but he who has
          taken possession of it and used it shall continue to use it.
          -- 1780 BC, Hammurabi, Code of Law
          --
          Asperger Syndroom - een persoonlijke benadering:

          Kom in verzet tegen dit kabinet:
          De website van de Socialistische Partij (SP) in Nederland: Informatie, nieuws, agenda en publicaties.


          Comment

          • Ivan Voras

            #6
            Re: sys.exit()

            Gerrit Holl wrote:
            [color=blue]
            > An alternative that I often choose is:
            >
            > raise SystemExit("I need arguments!")
            >
            > This is the same in one line, and I think it is more elegant, because
            > it
            > is higher-level: you are not using the low-level interface of error[/color]

            Yes, I agree. This is what I was looking for (as always, it was obvious
            :) ), thanks. Only, what error code is returned for this termination method?

            --
            --
            Every sufficiently advanced magic is indistinguishab le from technology
            - Arthur C Anticlarke


            Comment

            • Gerrit Holl

              #7
              Re: sys.exit()

              Ivan Voras wrote:[color=blue]
              > Gerrit Holl wrote:[color=green]
              > > An alternative that I often choose is:
              > >
              > > raise SystemExit("I need arguments!")
              > >
              > > This is the same in one line, and I think it is more elegant, because
              > > it
              > > is higher-level: you are not using the low-level interface of error[/color]
              >
              > Yes, I agree. This is what I was looking for (as always, it was obvious
              > :) ), thanks. Only, what error code is returned for this termination method?[/color]

              For a string, I believe it is 1, although I don't know when this holds and
              when it doesn't - I don't care for myself, so I never tried to find out, really ;)

              Gerrit.

              --
              258. If any one hire an ox-driver, he shall pay him six gur of corn per
              year.
              -- 1780 BC, Hammurabi, Code of Law
              --
              Asperger Syndroom - een persoonlijke benadering:

              Kom in verzet tegen dit kabinet:
              De website van de Socialistische Partij (SP) in Nederland: Informatie, nieuws, agenda en publicaties.


              Comment

              • George Young

                #8
                Re: sys.exit()

                On Thu, 09 Oct 2003 20:44:55 +0200, Gerrit Holl wrote:
                [color=blue]
                > Ivan Voras wrote:[color=green]
                >> Gerrit Holl wrote:[color=darkred]
                >> > An alternative that I often choose is:
                >> >
                >> > raise SystemExit("I need arguments!")
                >> >
                >> > This is the same in one line, and I think it is more elegant, because
                >> > it
                >> > is higher-level: you are not using the low-level interface of error[/color]
                >>
                >> Yes, I agree. This is what I was looking for (as always, it was obvious
                >> :) ), thanks. Only, what error code is returned for this termination method?[/color]
                >
                > For a string, I believe it is 1, although I don't know when this holds and
                > when it doesn't - I don't care for myself, so I never tried to find out, really ;)[/color]

                Actually, SystemExit is a *lower* level operation. From the docs:
                [ http://www.python.org/doc/current/lib/module-sys.html ]
                =============== =============== =============== =============== ====
                exit( [arg])

                Exit from Python. This is implemented by raising the SystemExit
                exception, so cleanup actions specified by finally clauses of try
                statements are honored, and it is possible to intercept the exit
                attempt at an outer level. The optional argument arg can be an integer
                giving the exit status (defaulting to zero), or another type of
                object. If it is an integer, zero is considered ``successful
                termination'' and any nonzero value is considered ``abnormal
                termination'' by shells and the like. Most systems require it to be in
                the range 0-127, and produce undefined results otherwise. Some systems
                have a convention for assigning specific meanings to specific exit
                codes, but these are generally underdeveloped; Unix programs generally
                use 2 for command line syntax errors and 1 for all other kind of
                errors. If another type of object is passed, None is equivalent to
                passing zero, and any other object is printed to sys.stderr and
                results in an exit code of 1. In particular, sys.exit("some error
                message") is a quick way to exit a program when an error occurs.
                =============== =============== =============== =============== ====

                So SystemExit is called by sys.exit. And one can use:

                sys.exit('I need arguments!')

                Thus it would seem that sys.exit is higher level, and probably a bit more
                stable and portable.


                -- George

                Comment

                • Gerrit Holl

                  #9
                  Re: sys.exit()

                  George Young wrote:[color=blue]
                  > So SystemExit is called by sys.exit. And one can use:
                  >
                  > sys.exit('I need arguments!')
                  >
                  > Thus it would seem that sys.exit is higher level, and probably a bit more
                  > stable and portable.[/color]

                  Ah, yes. I was probably confused with another function, I think with
                  os._exit. I don't really understand why _exit is in the os module
                  while exit is in the sys module; I have grown accostumed to raising
                  SystemExit, but I can de-grow it again I guess ;)

                  Gerrit.

                  --
                  240. If a merchantman run against a ferryboat, and wreck it, the master
                  of the ship that was wrecked shall seek justice before God; the master of
                  the merchantman, which wrecked the ferryboat, must compensate the owner
                  for the boat and all that he ruined.
                  -- 1780 BC, Hammurabi, Code of Law
                  --
                  Asperger Syndroom - een persoonlijke benadering:

                  Kom in verzet tegen dit kabinet:
                  De website van de Socialistische Partij (SP) in Nederland: Informatie, nieuws, agenda en publicaties.


                  Comment

                  • DogWalker

                    #10
                    Re: sys.exit()

                    BDFL has said: http://www.artima.com/weblogs/viewpost.jsp?thread=4829

                    "Ivan Voras" <ivoras@fer.h r> wrote in message
                    news:bm3msp$fjo $1@bagan.srce.h r...
                    In a code such as:

                    if len(sys.argv) < 2:
                    print "I need arguments!"
                    sys.exit(1)

                    Is sys.exit() really a good choice? Is there something more elegant? (I
                    tried return but it is valid only in a function)


                    --
                    --
                    Every sufficiently advanced magic is indistinguishab le from technology
                    - Arthur C Anticlarke


                    Comment

                    • Donn Cave

                      #11
                      Re: sys.exit()

                      Quoth "Ivan Voras" <ivoras@fer.hr> :
                      | In a code such as:
                      |
                      | if len(sys.argv) < 2:
                      | print "I need arguments!"
                      | sys.exit(1)
                      |
                      | Is sys.exit() really a good choice? Is there something more elegant? (I
                      | tried return but it is valid only in a function)

                      Your question has already been answered many times over, so I will
                      instead note something about the above that is rather common but
                      wrong. "print" writes to sys.stdout (unless instructed otherwise),
                      and in the present case this output should certainly go to sys.stderr.
                      The easiest way to do that in recent versions of python (2.0 and
                      later) is "print >> sys.stderr".

                      Donn Cave, donn@drizzle.co m

                      Comment

                      • Duncan Booth

                        #12
                        Re: sys.exit()

                        "Donn Cave" <donn@drizzle.c om> wrote in news:1065757907 .371391@yasure:
                        [color=blue]
                        >| Is sys.exit() really a good choice? Is there something more elegant? (I
                        >| tried return but it is valid only in a function)
                        >
                        > Your question has already been answered many times over, so I will
                        > instead note something about the above that is rather common but
                        > wrong. "print" writes to sys.stdout (unless instructed otherwise),
                        > and in the present case this output should certainly go to sys.stderr.
                        > The easiest way to do that in recent versions of python (2.0 and
                        > later) is "print >> sys.stderr".[/color]

                        Note that the suggestions to use "sys.exit(messa ge)" or "raise
                        SystemExit(mess age)" will send the message to sys.stderr. This is probably
                        easier than doing it explicitly in a separate print statement, plus it has
                        the advantage that if you want to catch the exit for any reason you also
                        catch the error message.

                        I've actually ended up doing this on occasion in unit tests, if you are
                        testing something that should cause a fatal error its a lot easier if the
                        error message is encapsulated in the SystemExit exception.

                        --
                        Duncan Booth duncan@rcp.co.u k
                        int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
                        "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

                        Comment

                        • Donn Cave

                          #13
                          Re: sys.exit()

                          Quoth Duncan Booth <duncan@NOSPAMr cp.co.uk>:
                          | "Donn Cave" <donn@drizzle.c om> wrote in news:1065757907 .371391@yasure:
                          ....
                          |> Your question has already been answered many times over, so I will
                          |> instead note something about the above that is rather common but
                          |> wrong. "print" writes to sys.stdout (unless instructed otherwise),
                          |> and in the present case this output should certainly go to sys.stderr.
                          |> The easiest way to do that in recent versions of python (2.0 and
                          |> later) is "print >> sys.stderr".
                          |
                          | Note that the suggestions to use "sys.exit(messa ge)" or "raise
                          | SystemExit(mess age)" will send the message to sys.stderr. This is probably
                          | easier than doing it explicitly in a separate print statement, plus it has
                          | the advantage that if you want to catch the exit for any reason you also
                          | catch the error message.
                          |
                          | I've actually ended up doing this on occasion in unit tests, if you are
                          | testing something that should cause a fatal error its a lot easier if the
                          | error message is encapsulated in the SystemExit exception.

                          That's right, it does work and it is easier. But please don't think
                          of print >> sys.stderr as something that's hard, lest that lead to
                          code like distutils that just neglects to do it.

                          distutils perhaps has the excuse that it had this problem before there
                          was a solution, but it's a good example of the problem. It's natural
                          to want to save the output of distutils to disk - I think it's really a
                          good practice to do this routinely so later you know how an application
                          was built, what went into it and where it got installed. In this case,
                          you'd always redirect both stdout and stderr, with a shell command line
                          like 'python setup.py > make.log 2>&1', so it would seem that the choice
                          between stdout and stderr would not matter. But it matters a lot, because
                          when stdout is a disk file, it's block buffered. That means the actual
                          flush to disk may be deferred for some time. Meanwhile, the applications
                          and processes that distutils invokes (cc, etc.) write immediately to disk
                          because if nothing else their output buffers flush when they exit. So
                          your log file starts with a bunch of diagnostic output from cc, and only
                          later gets to the distutils output that announces it's going to run cc.
                          Completely unreadable. stderr is always line buffered.

                          Donn Cave, donn@drizzle.co m

                          Comment

                          Working...