3 ms·
Is there an equivalent of Common Lisp's TRACE in the Python world? IMO it is the most value for money (i.e. time and convenience) debugging tool I've used till
by chaitanya 7y ago
Is there an equivalent of Common Lisp's TRACE in the Python world?
IMO it is the most value for money (i.e. time and convenience) debugging tool I've used till now. Simply write (TRACE function1 function2 ...) in the REPL and you will get a nicely formatted output of arguments passed and value(s) returned for each invocation of the given functions. Another nice feature is that a deeper an invocation is in the stack the more it is indented -- so recursive functions are fairly easy to debug too.
You can't use it for everything but its sufficient most of the time.
PySnooper looks good, but it is inconvenient in a couple of ways:
1. It prints every line of the traced function -- most of the time this is overkill and not what one needs.
2. To snoop on a function you need to modify the source file. Not a deal breaker but you still have to remember to revert this.
- yingw787 7y agoI use this snippet: ``` import ipdb ipdb.set_trace() ``` It drops me into IPython, an interactive Python shell, with the interactive Python debugger. You can type variables and use `pdb` primitives like (c)ontinue, (u)p call stack, (n)ext line, etc. I really like it, but ofc YMMV.
- jmuhlich 7y agoTry pdb++ (pdbpp) instead — it's like a much improved ipdb. It monkeypatches itself into pdb rather than defining a new package, so it works from any context that drops into the debugger. Its “sticky mode” alone is worth the switch.
- chaitanya 7y agoYeah but the idea is that, with TRACE, most of the time you shouldn't need to drop into the debugger (all CLs come with an interactive debugger too). If TRACE is not sufficient, only then do I go down the debugger route (or strategic print statements).
- spc476 7y agoI've done something similar (but not exact) for Lua [1]. I was testing code to trace Lua execution and found some code that was executed more than I expected [2]. Adding a trace like you described should be easy as well in Lua. [1] http://boston.conman.org/2015/02/12.1 http://boston.conman.org/2015/02/12.1 [2] http://boston.conman.org/2015/02/13.1 http://boston.conman.org/2015/02/13.1
- meken 7y ago%xmode in ipython does something similar, but only when an exception is hit. https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-xmode https://ipython.readthedocs.io/en/stable/interactive/magics....
- chaitanya 7y agoNice. Does CPython have something like this?
- fulafel 7y agoThere's sys.settrace(mytracefun), so something like https://stackoverflow.com/a/32607930 https://stackoverflow.com/a/32607930
- chaitanya 7y agoThanks! Yeah this should do the trick I think.