]> granicus.if.org Git - python/commitdiff
#1326077: fix traceback formatting of SyntaxErrors. This fixes two differences with...
authorGeorg Brandl <georg@python.org>
Sun, 5 Apr 2009 14:24:52 +0000 (14:24 +0000)
committerGeorg Brandl <georg@python.org>
Sun, 5 Apr 2009 14:24:52 +0000 (14:24 +0000)
Lib/test/test_traceback.py
Lib/traceback.py

index 0708f8163ef23e312c18c813c64f8ae1348a9e10..5cd08eeff3ded5fe0f9c2224f0fb59eda2e77767 100644 (file)
@@ -8,16 +8,6 @@ from test.test_support import run_unittest, is_jython, Error
 
 import traceback
 
-try:
-    raise KeyError
-except KeyError:
-    type_, value, tb = sys.exc_info()
-    file_ = StringIO()
-    traceback_print(tb, file_)
-    example_traceback = file_.getvalue()
-else:
-    raise Error("unable to create test traceback string")
-
 
 class TracebackCases(unittest.TestCase):
     # For now, a very minimal set of tests.  I want to be sure that
@@ -162,9 +152,24 @@ def test():
 
 class TracebackFormatTests(unittest.TestCase):
 
-    def test_traceback_indentation(self):
+    def test_traceback_format(self):
+        try:
+            raise KeyError('blah')
+        except KeyError:
+            type_, value, tb = sys.exc_info()
+            traceback_fmt = 'Traceback (most recent call last):\n' + \
+                            ''.join(traceback.format_tb(tb))
+            file_ = StringIO()
+            traceback_print(tb, file_)
+            python_fmt  = file_.getvalue()
+        else:
+            raise Error("unable to create test traceback string")
+
+        # Make sure that Python and the traceback module format the same thing
+        self.assertEquals(traceback_fmt, python_fmt)
+
         # Make sure that the traceback is properly indented.
-        tb_lines = example_traceback.splitlines()
+        tb_lines = python_fmt.splitlines()
         self.assertEquals(len(tb_lines), 3)
         banner, location, source_line = tb_lines
         self.assert_(banner.startswith('Traceback'))
index b06fcea79618529e17bd8a8e83a8ed54b3e6d26d..86a2c06b3c50730b393ea10e8433435c3d1875a1 100644 (file)
@@ -64,7 +64,7 @@ def print_tb(tb, limit=None, file=None):
         filename = co.co_filename
         name = co.co_name
         _print(file,
-               '  File "%s", line %d, in %s' % (filename,lineno,name))
+               '  File "%s", line %d, in %s' % (filename, lineno, name))
         linecache.checkcache(filename)
         line = linecache.getline(filename, lineno, f.f_globals)
         if line: _print(file, '    ' + line.strip())
@@ -124,9 +124,8 @@ def print_exception(etype, value, tb, limit=None, file=None):
         _print(file, 'Traceback (most recent call last):')
         print_tb(tb, limit, file)
     lines = format_exception_only(etype, value)
-    for line in lines[:-1]:
-        _print(file, line, ' ')
-    _print(file, lines[-1], '')
+    for line in lines:
+        _print(file, line, '')
 
 def format_exception(etype, value, tb, limit = None):
     """Format a stack trace and the exception information.
@@ -195,7 +194,7 @@ def format_exception_only(etype, value):
                 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))
-            value = msg
+        value = msg
 
     lines.append(_format_final_exc_line(stype, value))
     return lines