[issue12458] Tracebacks should contain the first line of continuation lines

Ezio Melotti report at bugs.python.org
Tue Apr 30 09:12:22 CEST 2013


Ezio Melotti added the comment:

I think all the lines involved in a multi-line expression should be reported.

Take for example this simple code:
  x = [int(c, 16)
       for c in '0123456789ABCDEFG'
       if c.isdigit() or c.isupper()]

With Python 2 the traceback is:
Traceback (most recent call last):
  File "deleteme.py", line 3, in <module>
    if c.isdigit() or c.isupper()]
ValueError: invalid literal for int() with base 16: 'G'

Which is not very helpful, because you don't see the int() call that is causing the error in the first place and you don't see where the G comes from.


Incidentally, for this specific case, the traceback is a bit better on Python3:
Traceback (most recent call last):
  File "deleteme.py", line 2, in <module>
    for c in '0123456789ABGDEF'
  File "deleteme.py", line 3, in <listcomp>
    if c.isdigit() or c.isupper()]
ValueError: invalid literal for int() with base 16: 'G'

But this still misses the int() call.

IMHO it would be better to have something like:
  Traceback (most recent call last):
  File "deleteme.py", lines 1-3, in <module>
    x = [int(c, 16)
         for c in '0123456789ABCDEFG'
         if c.isdigit() or c.isupper()]
ValueError: invalid literal for int() with base 16: 'G'


If this is rejected for some reason, I think printing the first line -- rather than the last -- would still be an improvement, because in most of the cases the first line contains more information (e.g. the name of the function with a multi-line list of args, a raise/assert with the name of the exception and part of the message, the operation done for each element of a genexp/listcomp).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12458>
_______________________________________


More information about the Python-bugs-list mailing list