msg47499 - (view) |
Author: Skip Montanaro (skip.montanaro) *  |
Date: 2005-01-09 01:59 |
I think it would be useful to sometimes have local variable values printed with tracebacks and stack frames. The attached patch is a first cut at that. I don't know if people would want that as the default, so I've defaulted argument printing to disabled. With arg printing enabled, executing this script: #!/usr/bin/env python import sys import traceback def exch(ty, val, tb): traceback.print_exception(ty, val, tb, args=True) sys.excepthook = exch def f(n,d): return n/d def g(a): return a/(a-1) for i in range(5,-1,-1): print g(i) displays this output: 1 1 1 2 Traceback (most recent call last): File "/Users/skip/tmp/tb.py", line 17, in ? print g(i) exch: <function exch at 0x4c46b0> f: <function f at 0x441330> g: <function g at 0x441370> i: 1 File "/Users/skip/tmp/tb.py", line 14, in g return a/(a-1) a: 1 ZeroDivisionError: integer division or modulo by zero |
|
|
msg47500 - (view) |
Author: Björn Lindqvist (sonderblade) |
Date: 2005-02-09 10:44 |
Logged In: YES user_id=51702 I like this idea alot! I usually debug by inserting print "somevar =",somevar around where I think the problem code is and this patch basically does it automagically for me. I can definitely image this patch saving me many hours of debug time. But: 1. It outputs to much information. An unhandled exception that bubbles up can be 3-4 stackframes long and seeing all the variables in those frames is excessive. 2. I think it would be better if the enhanced traceback was either default or activated with a commandline option to Python. Item 1 maybe can be solved by only outputting those names that are contained in the lines in which the exception is raised? So that the output in your example becomes: Traceback (most recent call last): File "exception.py", line 20, in ? print g(i) g: <function g at 0x402efed4> i: 1 File "exception.py", line 14, in g return a/(a-1) a: 1 ZeroDivisionError: integer division or modulo by zero It feels a little weird to have to hook sys.excepthook. I think this idea is to good to be "buried" like that. Hopefully, with some polishing it can be activated with a commandline option or become the default bahaviour. |
|
|
msg47501 - (view) |
Author: Martin v. Löwis (loewis) *  |
Date: 2005-02-09 23:21 |
Logged In: YES user_id=21627 Philipp Eby commented that he would not like to see this as a default, as the default already prints too much information. He also (indirectly) suggested that he would not like to see it as a command line option, but that he would prefer to see a different command line option instead which enters pdb on uncaught exceptions. |
|
|
msg82128 - (view) |
Author: Daniel Diniz (ajaksu2) *  |
Date: 2009-02-14 22:24 |
Maybe this could be enabled only on the interactive interpreter? Skip's patch includes tests and docs, needs updating. |
|
|
msg82129 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2009-02-14 22:29 |
It would certainly be horrible to enable this by default. Think about intermediate functions having 10 local variables each, most of which are totally irrelevant. |
|
|
msg82219 - (view) |
Author: Skip Montanaro (skip.montanaro) *  |
Date: 2009-02-16 06:07 |
How about reformulating it as a function appropriate as sys.excepthook? |
|
|
msg82224 - (view) |
Author: Martin v. Löwis (loewis) *  |
Date: 2009-02-16 08:20 |
If it becomes a sys.excepthook, it doesn't need to be part of Python anymore; any user could install it as a recipe if they desire. Hence I'm rejecting the patch. |
|
|
msg82264 - (view) |
Author: Skip Montanaro (skip.montanaro) *  |
Date: 2009-02-16 19:29 |
Martin> If it becomes a sys.excepthook, it doesn't need to be part of Martin> Python anymore; any user could install it as a recipe if they Martin> desire. Martin> Hence I'm rejecting the patch. It could still be a nice addition to the traceback module: sys.excepthook = traceback.verbose_traceback In fact, a minimalist traceback style, similar to the one asyncore, might also be useful: sys.excepthook = traceback.minimal_traceback Skip |
|
|
msg82276 - (view) |
Author: Martin v. Löwis (loewis) *  |
Date: 2009-02-16 21:50 |
> It could still be a nice addition to the traceback module: If it doesn't have to be in the standard library, I'd rather see it as a cookbook recipe, or PyPI package. I think the debate is still open whether, as an addition to traceback module, it would be "nice". |
|
|
msg82301 - (view) |
Author: Skip Montanaro (skip.montanaro) *  |
Date: 2009-02-17 06:40 |
PyPI: http://pypi.python.org/pypi/tb |
|
|