Overriding traceback print_exc()?

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

    #1

    Overriding traceback print_exc()?

    I want to cause any traceback output from my applications to show up in one
    of my dialog boxes, instead of in the command or terminal window (between
    running on Solaris, Linux, OSX and Windows systems there might not be any
    command window or terminal window to show the traceback messages in). Do I
    want to do something like override the print_exc (or format_exc?) method of
    traceback to get the text of the message and call my dialog box routine? If
    that is right how do I do that (monkeying with classes is all still a grey
    area to me)?

    I kind of understand using the traceback module to alter the output of
    exceptions that I am looking for, but I'm after those pesky ones that should
    never happen. :)

    Thanks!

    Bob


  • Fredrik Lundh

    #2
    Re: Overriding traceback print_exc()?

    Bob Greschke wrote:
    I want to cause any traceback output from my applications to show up in one
    of my dialog boxes, instead of in the command or terminal window (between
    running on Solaris, Linux, OSX and Windows systems there might not be any
    command window or terminal window to show the traceback messages in). Do I
    want to do something like override the print_exc (or format_exc?) method of
    traceback to get the text of the message and call my dialog box routine?
    one way to do that is to put a big try/except around your main program,
    and display the dialogue box in the except clause:

    import traceback

    def main():
    raise RuntimeError("o ops!")

    try:
    main()
    except (KeyboardInterr upt, SystemExit):
    raise
    except:
    print "ERROR", repr(traceback. format_exc())

    another approach is to install an exit-handler that uses last_traceback
    and friends to generate a traceback:

    import atexit, traceback, sys

    def postmortem():
    if hasattr(sys, "last_traceback "):
    print "ERROR", repr(traceback. format_exceptio n(
    sys.last_type, sys.last_value, sys.last_traceb ack
    ))

    atexit.register (postmortem)

    def main():
    raise RuntimeError("o ops!")

    main()

    the latter is less intrusive, and can be squirreled away in a support
    module. also, the original exception is still reported to the console,
    as usual.

    </F>

    Comment

    • dakman@gmail.com

      #3
      Re: Overriding traceback print_exc()?

      You could always override sys.stderr with a instance of your own with a
      write() method. Though you will still have to catch the exceptions.

      Bob Greschke wrote:
      I want to cause any traceback output from my applications to show up in one
      of my dialog boxes, instead of in the command or terminal window (between
      running on Solaris, Linux, OSX and Windows systems there might not be any
      command window or terminal window to show the traceback messages in). Do I
      want to do something like override the print_exc (or format_exc?) method of
      traceback to get the text of the message and call my dialog box routine? If
      that is right how do I do that (monkeying with classes is all still a grey
      area to me)?
      >
      I kind of understand using the traceback module to alter the output of
      exceptions that I am looking for, but I'm after those pesky ones that should
      never happen. :)
      >
      Thanks!
      >
      Bob

      Comment

      • Ziga Seilnacht

        #4
        Re: Overriding traceback print_exc()?

        Bob Greschke wrote:
        I want to cause any traceback output from my applications to show up in one
        of my dialog boxes, instead of in the command or terminal window (between
        running on Solaris, Linux, OSX and Windows systems there might not be any
        command window or terminal window to show the traceback messages in). Do I
        want to do something like override the print_exc (or format_exc?) method of
        traceback to get the text of the message and call my dialog box routine? If
        that is right how do I do that (monkeying with classes is all still a grey
        area to me)?
        You can overwrite the sys.exepthook() with your own function:


        import sys
        from traceback import format_exceptio n

        def my_excepthook(e xctype, value, traceback):
        details = "".join(format_ exception(excty pe, value, traceback))
        # now show the details in your dialog box

        sys.excepthook = my_excepthook


        See the documentation for details:
        This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available. Unless explicitly noted oth...


        Hope this helps,
        Ziga

        Comment

        • draghuram@gmail.com

          #5
          Re: Overriding traceback print_exc()?

          I usually have a function like this:

          def get_excinfo_str ():
          """return exception stack trace as a string"""

          (exc_type, exc_value, exc_traceback) = sys.exc_info()
          formatted_excin fo = traceback.forma t_exception(exc _type, exc_value,
          exc_traceback)
          excinfo_str = "".join(formatt ed_excinfo)

          del exc_type
          del exc_value
          del exc_traceback

          return(excinfo_ str)


          I can then call it from within "except" and print it to a log file.

          Raghu.

          Bob Greschke wrote:
          I want to cause any traceback output from my applications to show up in one
          of my dialog boxes, instead of in the command or terminal window (between
          running on Solaris, Linux, OSX and Windows systems there might not be any
          command window or terminal window to show the traceback messages in). Do I
          want to do something like override the print_exc (or format_exc?) method of
          traceback to get the text of the message and call my dialog box routine? If
          that is right how do I do that (monkeying with classes is all still a grey
          area to me)?
          >
          I kind of understand using the traceback module to alter the output of
          exceptions that I am looking for, but I'm after those pesky ones that should
          never happen. :)
          >
          Thanks!
          >
          Bob

          Comment

          • Scott David Daniels

            #6
            Re: Overriding traceback print_exc()?

            draghuram@gmail .com wrote:
            I usually have a function like this:
            >
            def get_excinfo_str ():
            """return exception stack trace as a string"""
            (exc_type, exc_value, exc_traceback) = sys.exc_info()
            The parens here can be skipped:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            formatted_excin fo = traceback.forma t_exception(exc _type, exc_value,
            exc_traceback)
            excinfo_str = "".join(formatt ed_excinfo)
            del exc_type
            del exc_value
            del exc_traceback
            The three del lines above don't do anything (the return decrefs the locals).
            return(excinfo_ str)
            The parens here can be skipped as well:
            return excinfo_str
            --
            --Scott David Daniels
            scott.daniels@a cm.org

            Comment

            Working...