Python style: exceptions vs. sys.exit()

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

    Python style: exceptions vs. sys.exit()

    I have a general question of Python style, or perhaps just good
    programming practice.

    My group is developing a medium-sized library of general-purpose
    Python functions, some of which do I/O. Therefore it is possible for
    many of the library functions to raise IOError Exceptions. The
    question is: should the library function be able to just dump to
    sys.exit() with a message about the error (like "couldn't open this
    file"), or should the exception propagate to the calling program which
    handles the issue?

    Thanks in advance for anyone who can either answer my question or
    point me to where this question has already been answered.

  • Larry Bates

    #2
    Re: Python style: exceptions vs. sys.exit()

    Drake wrote:
    I have a general question of Python style, or perhaps just good
    programming practice.
    >
    My group is developing a medium-sized library of general-purpose
    Python functions, some of which do I/O. Therefore it is possible for
    many of the library functions to raise IOError Exceptions. The
    question is: should the library function be able to just dump to
    sys.exit() with a message about the error (like "couldn't open this
    file"), or should the exception propagate to the calling program which
    handles the issue?
    >
    Thanks in advance for anyone who can either answer my question or
    point me to where this question has already been answered.
    >
    IMHO libraries should always just let the exception propagate up to the caller.
    That allows the caller the option of taking the appropriate action.

    -Larry

    Comment

    • Grant Edwards

      #3
      Re: Python style: exceptions vs. sys.exit()

      On 2008-09-23, Drake <cjdrake@gmail. comwrote:
      My group is developing a medium-sized library of general-purpose
      Python functions, some of which do I/O. Therefore it is possible for
      many of the library functions to raise IOError Exceptions. The
      question is: should the library function be able to just dump to
      sys.exit() with a message about the error (like "couldn't open this
      file"),
      No. A library module should never call sys.exit().
      or should the exception propagate to the calling program which
      handles the issue?
      Yes. Let the application handle the error if it wants to. If
      it's not handled, it'll end up causing the program to exit.

      --
      Grant Edwards grante Yow! It's a hole all the
      at way to downtown Burbank!
      visi.com

      Comment

      • Craig Allen

        #4
        Re: Python style: exceptions vs. sys.exit()

        The
        question is: should the library function be able to just dump to
        sys.exit() with a message about the error (like "couldn't open this
        file"), or should the exception propagate to the calling program which
        handles the issue?
        >
        my view is that the exceptions are there precisely to tell the calling
        program about the error and give the programmer a chance to do
        something smart about it. A library, properly, doesn't even know the
        context in which it is running, and sys.exit() is pretty heavy handed
        for a library to call and shows assumptions beyond what a library
        should assume about its running environment.

        imho

        Comment

        • Terry Reedy

          #5
          Re: Python style: exceptions vs. sys.exit()

          Drake wrote:
          I have a general question of Python style, or perhaps just good
          programming practice.
          >
          My group is developing a medium-sized library of general-purpose
          Python functions, some of which do I/O. Therefore it is possible for
          many of the library functions to raise IOError Exceptions. The
          question is: should the library function be able to just dump to
          sys.exit() with a message about the error (like "couldn't open this
          file"),
          No
          or should the exception propagate to the calling program which
          handles the issue?
          Yes -- with an informative error message.

          If the caller ignores the exception, the program will exit with a full
          stack trace anyway.

          Comment

          • Christian Heimes

            #6
            Re: Python style: exceptions vs. sys.exit()

            Drake wrote:
            I have a general question of Python style, or perhaps just good
            programming practice.
            >
            My group is developing a medium-sized library of general-purpose
            Python functions, some of which do I/O. Therefore it is possible for
            many of the library functions to raise IOError Exceptions. The
            question is: should the library function be able to just dump to
            sys.exit() with a message about the error (like "couldn't open this
            file"), or should the exception propagate to the calling program which
            handles the issue?
            Side note:

            sys.exit() is just another way to write raise SystemExit. The function
            is defined as:

            static PyObject *
            sys_exit(PyObje ct *self, PyObject *args)
            {
            PyObject *exit_code = 0;
            if (!PyArg_UnpackT uple(args, "exit", 0, 1, &exit_code))
            return NULL;
            /* Raise SystemExit so callers may catch it or clean up. */
            PyErr_SetObject (PyExc_SystemEx it, exit_code);
            return NULL;
            }

            Christian

            Comment

            • Steven D'Aprano

              #7
              Re: Python style: exceptions vs. sys.exit()

              On Tue, 23 Sep 2008 13:25:26 -0700, Drake wrote:
              I have a general question of Python style, or perhaps just good
              programming practice.
              >
              My group is developing a medium-sized library of general-purpose Python
              functions, some of which do I/O. Therefore it is possible for many of
              the library functions to raise IOError Exceptions. The question is:
              should the library function be able to just dump to sys.exit() with a
              message about the error (like "couldn't open this file"), or should the
              exception propagate to the calling program which handles the issue?
              >
              Thanks in advance for anyone who can either answer my question or point
              me to where this question has already been answered.

              Presumably somebody has suggested that calling sys.exit() was a good
              option. I'm curious to what possible reason they could give for such a
              poor choice.

              Hint: if a library function can't open a file, the application should be
              given the opportunity to open a different file. Or do something
              completely different instead. Whatever. That's not up to the library to
              decide, not even if the library is in such a broken state that it can't
              continue. Why not? Because the application might deal with that by
              unloading the library and continuing regardless.



              --
              Steven

              Comment

              • Asun Friere

                #8
                Re: Python style: exceptions vs. sys.exit()

                On Sep 24, 8:10 am, Christian Heimes <li...@cheimes. dewrote:
                Side note:
                >
                sys.exit() is just another way to write raise SystemExit. The function
                is defined as:
                >
                As can be seen if you were ever silly enough to call sys.exit() in
                IDLE. ;)

                Comment

                • Bruno Desthuilliers

                  #9
                  Re: Python style: exceptions vs. sys.exit()

                  Drake a écrit :
                  I have a general question of Python style, or perhaps just good
                  programming practice.
                  >
                  My group is developing a medium-sized library of general-purpose
                  Python functions, some of which do I/O. Therefore it is possible for
                  many of the library functions to raise IOError Exceptions. The
                  question is: should the library function be able to just dump to
                  sys.exit() with a message about the error (like "couldn't open this
                  file"),
                  Arrghll ! NO, DONT !
                  or should the exception propagate to the calling program which
                  handles the issue?
                  Yes, by all means and for God's sake.
                  Thanks in advance for anyone who can either answer my question or
                  point me to where this question has already been answered.
                  There have been numerous threads about this here.

                  Comment

                  • Carl Banks

                    #10
                    Re: Python style: exceptions vs. sys.exit()

                    On Sep 23, 4:25 pm, Drake <cjdr...@gmail. comwrote:
                    The
                    question is: should the library function be able to just dump to
                    sys.exit() with a message about the error (like "couldn't open this
                    file"),
                    I'm kind of curious what your library is for. Is it something where
                    exiting the app be the only appropriate action for an IO error?

                    Even if it is, I will echo other people's advice: a library should
                    never call exit, at least not by default. For your imagined use it
                    might make sense to exit upon any failure, but other people using the
                    library might not want to do that.

                    For that matter, a library should never print error or status
                    messages. Messages should either be sent to the caller somehow, or
                    handled using the logging facility.


                    Carl Banks

                    Comment

                    • Grant Edwards

                      #11
                      Re: Python style: exceptions vs. sys.exit()

                      On 2008-09-24, Steven D'Aprano <steven@REMOVE. THIS.cybersourc e.com.auwrote:
                      On Tue, 23 Sep 2008 13:25:26 -0700, Drake wrote:
                      >
                      >I have a general question of Python style, or perhaps just good
                      >programming practice.
                      >>
                      >My group is developing a medium-sized library of general-purpose Python
                      >functions, some of which do I/O. Therefore it is possible for many of
                      >the library functions to raise IOError Exceptions. The question is:
                      >should the library function be able to just dump to sys.exit() with a
                      >message about the error (like "couldn't open this file"), or should the
                      >exception propagate to the calling program which handles the issue?
                      >>
                      >Thanks in advance for anyone who can either answer my question or point
                      >me to where this question has already been answered.
                      >
                      >
                      Presumably somebody has suggested that calling sys.exit() was a good
                      option. I'm curious to what possible reason they could give for such a
                      poor choice.
                      Same here. It's like an automotive engine controls designer
                      asking if a failed O2 sensor should turn on the check engine
                      light or blow up the car.

                      --
                      Grant Edwards grante Yow! Mr and Mrs PED, can I
                      at borrow 26.7% of the RAYON
                      visi.com TEXTILE production of the
                      INDONESIAN archipelago?

                      Comment

                      • Tim Rowe

                        #12
                        Re: Python style: exceptions vs. sys.exit()

                        2008/9/24 Bruno Desthuilliers <bruno.42.desth uilliers@websit eburo.invalid>:
                        Drake a écrit :
                        >many of the library functions to raise IOError Exceptions. The
                        >question is: should the library function be able to just dump to
                        >sys.exit() with a message about the error (like "couldn't open this
                        >file"),
                        >
                        Arrghll ! NO, DONT !
                        Can I put in a vote *for* the questioner's library dumping to
                        sys.exit() on any abnormal condition? It would reduce employment
                        competition for the rest of us.

                        Why, yes, I am wearing my BOFH hat. How could you tell?

                        --
                        Tim Rowe

                        Comment

                        • Ross Ridge

                          #13
                          Re: Python style: exceptions vs. sys.exit()

                          Steven D'Aprano <steven@REMOVE. THIS.cybersourc e.com.auwrote:
                          Presumably somebody has suggested that calling sys.exit() was a good
                          option. I'm curious to what possible reason they could give for such a
                          poor choice.
                          Grant Edwards <invalid@invali dwrote:
                          >Same here. It's like an automotive engine controls designer
                          >asking if a failed O2 sensor should turn on the check engine
                          >light or blow up the car.
                          No, it's more like asking if the failed sensor should turn on a strange
                          and mysterious light on the dashboard and then blow up the car if the
                          driver doesn't immediately stop and check the engine. The owners manual
                          would only vaguely hint at the fact that this could happen.

                          Ross Ridge

                          --
                          l/ // Ross Ridge -- The Great HTMU
                          [oo][oo] rridge@csclub.u waterloo.ca
                          -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
                          db //

                          Comment

                          • Grant Edwards

                            #14
                            Re: Python style: exceptions vs. sys.exit()

                            On 2008-09-24, Ross Ridge <rridge@csclub. uwaterloo.cawro te:
                            Steven D'Aprano <steven@REMOVE. THIS.cybersourc e.com.auwrote:
                            >Presumably somebody has suggested that calling sys.exit() was a good
                            >option. I'm curious to what possible reason they could give for such a
                            >poor choice.
                            >
                            Grant Edwards <invalid@invali dwrote:
                            >>Same here. It's like an automotive engine controls designer
                            >>asking if a failed O2 sensor should turn on the check engine
                            >>light or blow up the car.
                            >
                            No, it's more like asking if the failed sensor should turn on
                            a strange and mysterious light on the dashboard
                            You're right. I had forgotten that sys.exit() is actually
                            raising the system exit exception, and that the application
                            calling the library could handle that exception.
                            and then blow up the car if the driver doesn't immediately
                            stop and check the engine. The owners manual would only
                            vaguely hint at the fact that this could happen.
                            --
                            Grant Edwards grante Yow! Why is it that when
                            at you DIE, you can't take
                            visi.com your HOME ENTERTAINMENT
                            CENTER with you??

                            Comment

                            • Ross Ridge

                              #15
                              Re: Python style: exceptions vs. sys.exit()

                              Steven D'Aprano <steven@REMOVE. THIS.cybersourc e.com.auwrote:
                              Presumably somebody has suggested that calling sys.exit() was a good
                              option. I'm curious to what possible reason they could give for such a
                              poor choice.
                              Grant Edwards <invalid@invali dwrote:
                              >Same here. It's like an automotive engine controls designer
                              >asking if a failed O2 sensor should turn on the check engine
                              >light or blow up the car.
                              Ross Ridge <rridge@csclub. uwaterloo.cawro te:
                              No, it's more like asking if the failed sensor should turn on
                              a strange and mysterious light on the dashboard
                              Grant Edwards <invalid@invali dwrote:
                              >You're right. I had forgotten that sys.exit() is actually
                              >raising the system exit exception, and that the application
                              >calling the library could handle that exception.
                              Well, my point was that exceptions in Python are a bit like a car's
                              check engine light. Few drivers know what this mysterious light means,
                              and aren't prepared to do anything about it when it goes on.

                              Ross Ridge

                              --
                              l/ // Ross Ridge -- The Great HTMU
                              [oo][oo] rridge@csclub.u waterloo.ca
                              -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
                              db //

                              Comment

                              Working...