PDB how to define a global inspection function?

Charles Fox (Sheffield) charles.fox at gmail.com
Thu Feb 10 05:48:08 EST 2011




On Feb 8, 11:37 am, Peter Otten <__pete... at web.de> wrote:
> CharlesFox(Sheffield) wrote:
> > Hi guys, I'm new to this group and have a question about debugging.
> > I'm stepping through my code (using emacs pdbtrack and python-mode.el)
> > and would like to isnpect objects as I go.  So I've defined a little
> > object print function,
>
> > def p(obj):
> >     print obj
> >     print obj.__class__
> >     d=dir(obj)
> >     for a in d:
> >         print a, "=", getattr(obj, a)
>
> > however it only works if the function is imported by whatever module I
> > am currently debugging.  I'd like it to be available globally so I can
> > stop and inspect anything as I step through various modules (including
> > external libraries).  Is there a way to put it in the global scope for
> > pdb to use?   Also is there a way to automatically import it whenever
> > pdb starts up (like a matlab startup file)?     (I'm not using ipython
> > as it's not happy with pdbtrack in emacs, so am launching from emacs M-
> > x pdb command).
>
> For debugging purposes it's OK to put your function into the __builtin__
> namespace:
>
>
>
> >>> def p(): print 42
> ...
> >>> import __builtin__
> >>> __builtin__.p = p
>
> Try it out:
>
> >>> with open("tmp.py", "w") as f:
>
> ...     f.write("p()\n")
> ...>>> import tmp
>
> 42


Thanks very much for your help, Peter, that's exactly what I was
after :-)
Charles



More information about the Python-list mailing list