handling uncaught exceptions with pdb?

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

    handling uncaught exceptions with pdb?

    I think I've asked about this before, but is there a way to set up
    Python to handle uncaught exceptions with pdb? I know about setting
    sys.except_hook to something that calls pdb, but this is normally done
    at the outer level of a program, and by the time that hook gets
    called, the exception has already unwound the stack to the outermost
    level. My situation is I run a multi-hour or multi-day computation
    that eventually crashes due to some unexpected input and I'd like to
    break to the debugger at the innermost level, right when the exception
    is encountered, so I can fix the error with pdb commands and resume
    processing. Of course this presumes a certain semantics for Python
    exceptions (i.e. handling one involves scanning the stack twice, once
    to look for a handler and again to actually unwind the stack) and I'm
    not sure if it's really done that way. I do know that Lisp has a
    requirement like that, though; so maybe there is hope.
  • R. Bernstein

    #2
    Re: handling uncaught exceptions with pdb?

    Paul Rubin <http://phr.cx@NOSPAM.i nvalidwrites:
    I think I've asked about this before, but is there a way to set up
    Python to handle uncaught exceptions with pdb? I know about setting
    sys.except_hook to something that calls pdb, but this is normally done
    at the outer level of a program, and by the time that hook gets
    called, the exception has already unwound the stack to the outermost
    level. My situation is I run a multi-hour or multi-day computation
    that eventually crashes due to some unexpected input and I'd like to
    break to the debugger at the innermost level, right when the exception
    is encountered, so I can fix the error with pdb commands and resume
    processing.
    ....

    Why not use the traceback you get to show you where to change the code
    around that point to add an exception handler there which calls the
    debugger?

    Comment

    • Diez B. Roggisch

      #3
      Re: handling uncaught exceptions with pdb?

      R. Bernstein schrieb:
      Paul Rubin <http://phr.cx@NOSPAM.i nvalidwrites:
      >
      >I think I've asked about this before, but is there a way to set up
      >Python to handle uncaught exceptions with pdb? I know about setting
      >sys.except_hoo k to something that calls pdb, but this is normally done
      >at the outer level of a program, and by the time that hook gets
      >called, the exception has already unwound the stack to the outermost
      >level. My situation is I run a multi-hour or multi-day computation
      >that eventually crashes due to some unexpected input and I'd like to
      >break to the debugger at the innermost level, right when the exception
      >is encountered, so I can fix the error with pdb commands and resume
      >processing.
      ...
      >
      Why not use the traceback you get to show you where to change the code
      around that point to add an exception handler there which calls the
      debugger?
      Because he wants to fix the issue directly in-place, and then continue,
      instead of losing hours or even days of computation.

      However, it's not possible AFAIK.

      Diez

      Comment

      • Thomas Heller

        #4
        Re: handling uncaught exceptions with pdb?

        Paul Rubin schrieb:
        I think I've asked about this before, but is there a way to set up
        Python to handle uncaught exceptions with pdb? I know about setting
        sys.except_hook to something that calls pdb, but this is normally done
        at the outer level of a program, and by the time that hook gets
        called, the exception has already unwound the stack to the outermost
        level. My situation is I run a multi-hour or multi-day computation
        that eventually crashes due to some unexpected input and I'd like to
        break to the debugger at the innermost level, right when the exception
        is encountered, so I can fix the error with pdb commands and resume
        processing. Of course this presumes a certain semantics for Python
        exceptions (i.e. handling one involves scanning the stack twice, once
        to look for a handler and again to actually unwind the stack) and I'm
        not sure if it's really done that way. I do know that Lisp has a
        requirement like that, though; so maybe there is hope.
        Would this work (although it probably will slow down the program)?

        <snip>
        import sys, pdb

        def tracefunc(frame , event, arg):
        if event == "exception" :
        pdb.set_trace()
        return tracefunc

        sys.settrace(tr acefunc)

        def test(arg):
        if arg 20:
        raise ValueError(arg)
        return test(arg+1)

        test(0)
        <snip>

        Thomas

        Comment

        • R. Bernstein

          #5
          Re: handling uncaught exceptions with pdb?

          "Diez B. Roggisch" <deets@nospam.w eb.dewrites:
          R. Bernstein schrieb:
          >Paul Rubin <http://phr.cx@NOSPAM.i nvalidwrites:
          >>
          >>I think I've asked about this before, but is there a way to set up
          >>Python to handle uncaught exceptions with pdb? I know about setting
          >>sys.except_ho ok to something that calls pdb, but this is normally done
          >>at the outer level of a program, and by the time that hook gets
          >>called, the exception has already unwound the stack to the outermost
          >>level. My situation is I run a multi-hour or multi-day computation
          >>that eventually crashes due to some unexpected input and I'd like to
          >>break to the debugger at the innermost level, right when the exception
          >>is encountered, so I can fix the error with pdb commands and resume
          >>processing.
          >...
          >>
          >Why not use the traceback you get to show you where to change the code
          >around that point to add an exception handler there which calls the
          >debugger?
          >
          Because he wants to fix the issue directly in-place, and then
          continue, instead of losing hours or even days of computation.
          Then proactively add exception handlers ;-)

          Comment

          • Simon Brunning

            #6
            Re: handling uncaught exceptions with pdb?

            2008/9/10 Paul Rubin <"http://phr.cx"@nospam. invalid>:
            I think I've asked about this before, but is there a way to set up
            Python to handle uncaught exceptions with pdb?


            --
            Cheers,
            Simon B.
            simon@brunningo nline.net

            GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues | Twitter: brunns

            Comment

            Working...