Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Variables not showing on terminate

anstey
2006-05-29
2013-03-15
  • anstey
    anstey
    2006-05-29

    When a python script terminates due to an error (in Eclipse with PyDev Extensions), no variables are shown in the debug console, unlike standard IDEs like Wing and Komodo. Is there a way to make this happen?

     
    • Fabio Zadrozny
      Fabio Zadrozny
      2006-05-29

      Pydev currently does no exception-handling, however, you can add your own if you want to do so by changing the sys.__excepthook__

      E.g.:

      def myHook(*args, **kwargs):
          sys._original_excepthook(*args, **kwargs)

      sys._original_excepthook = sys.__excepthook__
      sys.__excepthook__ = myHook

      And then you can put a breakpoint in the 'sys._original_excepthook(*args, **kwargs)' line, so that whenever you have an unhandled exception it will stop there. An implementation of exception-handling in the debugger is planned, but it is still not implemented, so, you can add your own in this way.

      -- Fabio

       
      • anstey
        anstey
        2006-05-30

        Fabio,

        I'm a python newb so I need more assistance with this. Where do I put the code you suggested, and how do I change the sys.__excepthook__  ? I don't understand unfortunately.

        Will this give me the value of all by global and local variables when the program terminates with an exception, the way Wing and Komodo do? I rely heavily on this to work out what is going wrong with my programs.

        Cheers,
        matthew

         
        • Fabio Zadrozny
          Fabio Zadrozny
          2006-05-30

          You can put that anywhere, as long as you call it before getting into that exception.

          And it should give you the globals and the locals in the stack-trace. It is not the same as stopping at that exact time, but it gives you the complete info on the stack.

          Actually, I passed one thing wrong, you should change the sys.excepthook and not the sys.__excepthook__.

          Below is a complete example showing how you'd get the locals in the stack-trace.

          Cheers,

          Fabio

          ----------------- Example ------------

          import sys

          def m1(a = 10):
              raise RuntimeError('here')

          if __name__ == '__main__':
              def myHook(type, value, traceback):
                  initial_ctx = traceback.tb_next
                  while initial_ctx.tb_next is not None:
                      initial_ctx = initial_ctx.tb_next
                     
                  print 'locals on exception:', initial_ctx.tb_frame.f_locals
                  #now, call the original.
                  sys.__excepthook__(type, value, traceback)
             
              sys.excepthook = myHook

              m1()