Issue 6962: traceback.format_exception_only does not return SyntaxError carot correctly (original) (raw)
On Python 2.6.2, and possibly other versions, in the traceback module, format_exception_only does not behave correctly when printout out a SyntaxError. An extra newline is inserted before the carot. E.g.
38 exceptionType, exceptionValue, exceptionTraceback =
sys.exc_info()
39 sys.stderr.write("Exception:\n")
40 ex = traceback.format_exception_only(exceptionType,
exceptionValue)
41 sys.stderr.write(repr(ex))
42 for line in ex:
43 sys.stderr.write(line)
yields, e.g. Exception: [' File "/home/matt/apps/posac/source/posac/posac_main.py", line 12\n', ' def run()\n', ' \n^\n', 'SyntaxError: invalid syntax\n'] File "/home/matt/apps/posac/source/posac/posac_main.py", line 12 def run()
^ SyntaxError: invalid syntax
When it should be: Exception: [' File "/home/matt/apps/posac/source/posac/posac_main.py", line 12\n', ' def run()\n', ' ^\n', 'SyntaxError: invalid syntax\n'] File "/home/matt/apps/posac/source/posac/posac_main.py", line 12 def run() ^ SyntaxError: invalid syntax
Attached is a patch. This patch has been tested on gentoo linux.
Sorry about the lack of the attached file. I will try again and include it inline.
That other patch does not fix the bug I am seeing, which is experienced while using traceback.format_exception_only directly.
Thanks.
--- /usr/lib/python2.6/traceback.py 2009-09-23 19:49:17.000000000 -0500 +++ /home/matt/tmp/traceback.py 2009-09-21 17:09:49.590440613 -0500 @@ -190,11 +190,10 @@ if badline is not None: lines.append(' %s\n' % badline.strip()) if offset is not None:
caretspace = badline[:offset].lstrip()
caretspace = badline[:offset-1].lstrip() # non-space whitespace (likes tabs) must be kept for
alignment caretspace = ((c.isspace() and c or ' ') for c in caretspace)
# only three spaces to account for offset1 == pos 0
lines.append(' %s^\n' % ''.join(caretspace))
lines.append(_format_final_exc_line(stype, value))lines.append(' %s^\n' % ''.join(caretspace)) value = msg
This appears to be out of date. The output matches that of the interpreter (no extra newline) for 2.6.8, 2.7.3 and 3.2.1. The carrot in this particular example points to the ) and not the newline; I'm not sure if that is ideal, but it is consistent and, as Amaury said, currently tested for.
Closing this as out of date; if someone wants to analyze the ^/newline position issue and open a new bug if it seems appropriate, please do.