classification
Title: traceback.format_exception_only does not return SyntaxError carot correctly
Type: behavior Stage: resolved
Components: Versions: Python 2.6
process
Status: closed Resolution: out of date
Dependencies: Superseder: traceback.py formats SyntaxError differently
View: 1326077
Assigned To: Nosy List: amaury.forgeotdarc, ezio.melotti, r.david.murray, thewtex
Priority: normal Keywords: patch

Created on 2009-09-21 22:26 by thewtex, last changed 2013-02-19 01:17 by r.david.murray. This issue is now closed.

Files
File name Uploaded Description Edit
traceback.patch thewtex, 2009-10-07 20:16 traceback patch
Messages (5)
msg92966 - (view) Author: Matthew McCormick (thewtex) * Date: 2009-09-21 22:26
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.
msg93691 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-10-07 12:02
You did not attach any patch. But anyway this has already been fixed by
issue1326077.
msg93719 - (view) Author: Matthew McCormick (thewtex) * Date: 2009-10-07 20:16
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('    %s^\n' % ''.join(caretspace))
             value = msg
 
     lines.append(_format_final_exc_line(stype, value))
msg93729 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-10-07 23:04
OK, but your patch certainly interferes with issue1326077.
If format_exception_only changes, print_exception will display things 
differently.  And the precise output is now part of the test suite (in 
test_traceback.py)
msg182356 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-02-19 01:16
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.
History
Date User Action Args
2013-02-19 01:17:00r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg182356

resolution: out of date
stage: resolved
2009-10-08 14:52:17ezio.melottisetresolution: duplicate -> (no value)
2009-10-08 06:26:13ezio.melottisetnosy: + ezio.melotti
resolution: duplicate
2009-10-07 23:04:32amaury.forgeotdarcsetresolution: duplicate -> (no value)
messages: + msg93729
2009-10-07 20:16:51thewtexsetstatus: closed -> open
files: + traceback.patch
messages: + msg93719

keywords: + patch
2009-10-07 12:02:57amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg93691

superseder: traceback.py formats SyntaxError differently
resolution: duplicate
2009-09-21 22:26:51thewtexcreate