8

I want to utilize introspection capability of python for debugging/development, but cannot find appropriate tool for this.

I need to enter into shell (IPython for example) at specific position or at specific event (like exception), with locals and globals of shell being set to the frame's ones.

My own quick hack to illustrate it:

import inspect
from IPython.Shell import IPShellEmbed
def run_debug():
    stack = inspect.stack()
    frame = stack[1][0]
    loc = frame.f_locals
    glob = frame.f_globals
    shell = IPShellEmbed()
    shell(local_ns=loc, global_ns=glob)

With according run_debug() call from 'breakpoint' or try/except. But, obviously, this needs alot of polishing, esp to work with threaded apps properly.

winpdb has breakpoints with console, but I found no way to quickly run proper python shell from it, and eval()/exec() are not very handy for long debug.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Daniel Kluev
  • 11,025
  • 2
  • 36
  • 36

3 Answers3

3

Similar to what you're already doing, there's ipdb. Effectively, it's pdb with ipython's shell (i.e. tab completion, all the various magic functions, etc).

It's actually doing exactly what the little code snipped you posted in your question does, but wraps it into a simple "ipdb.set_trace()" call.

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • 1
    Yes, thats almost what I need, but it does not play well with threads, like my own example. When I call it from one of child threads, other ones are still running, altering data, sending messages to halted thread, and keeping outputting data to console. – Daniel Kluev Jul 23 '10 at 01:55
2

For personal/education purposes you can use WingIDE - they have some pretty solid debugging capabilities.

Of course if you're just worried about changing values you can always just use raw_input() - but that may not be advanced enough for your needs.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
  • Thanks, their Pro version indeed has needed functionality and it halts process execution before running shells, so threading is not problem. Quite sad its proprietary, but seems its the case of getting what are you paying for. – Daniel Kluev Jul 23 '10 at 02:11
  • I guess that's one of the benefits to charging - they can afford to hire people and *pay* them to work on problems like that ;) Of course if it's too pricey you can always look around for a local python conference and suggest they ask Wing for some give-away copies. They've given us 3-4 each year that we've asked, and every other dealing I've heard with them has been impressive. – Wayne Werner Jul 23 '10 at 12:26
0

If you run your code from ipython and hit an exception, you can call %debug afterwards to drop into a pdb at the exception. This should give you what you want. Or if you run ipython -pdb, you will automatically be dropped into pdb when an uncaught exception occurs.

macedoine
  • 347
  • 1
  • 3