Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 236f797

Browse files
committed
Merged revisions 71237-71238 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r71237 | georg.brandl | 2009-04-05 16:24:52 +0200 (So, 05 Apr 2009) | 1 line #1326077: fix traceback formatting of SyntaxErrors. This fixes two differences with formatting coming from Python: a) the reproduction of location details in the error message if no line text is given, b) the prefixing of the last line by one space. ........ r71238 | georg.brandl | 2009-04-05 16:25:41 +0200 (So, 05 Apr 2009) | 1 line Add NEWS entry for r71237. ........
1 parent dfd7344 commit 236f797

3 files changed

Lines changed: 22 additions & 16 deletions

File tree

Lib/test/test_traceback.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@
1010

1111
import traceback
1212

13-
try:
14-
raise KeyError
15-
except KeyError:
16-
type_, value, tb = sys.exc_info()
17-
file_ = StringIO()
18-
traceback_print(tb, file_)
19-
example_traceback = file_.getvalue()
20-
else:
21-
raise Error("unable to create test traceback string")
22-
2313

2414
class SyntaxTracebackCases(unittest.TestCase):
2515
# For now, a very minimal set of tests. I want to be sure that
@@ -158,9 +148,24 @@ def do_test(firstlines, message, charset, lineno):
158148

159149
class TracebackFormatTests(unittest.TestCase):
160150

161-
def test_traceback_indentation(self):
151+
def test_traceback_format(self):
152+
try:
153+
raise KeyError('blah')
154+
except KeyError:
155+
type_, value, tb = sys.exc_info()
156+
traceback_fmt = 'Traceback (most recent call last):\n' + \
157+
''.join(traceback.format_tb(tb))
158+
file_ = StringIO()
159+
traceback_print(tb, file_)
160+
python_fmt = file_.getvalue()
161+
else:
162+
raise Error("unable to create test traceback string")
163+
164+
# Make sure that Python and the traceback module format the same thing
165+
self.assertEquals(traceback_fmt, python_fmt)
166+
162167
# Make sure that the traceback is properly indented.
163-
tb_lines = example_traceback.splitlines()
168+
tb_lines = python_fmt.splitlines()
164169
self.assertEquals(len(tb_lines), 3)
165170
banner, location, source_line = tb_lines
166171
self.assert_(banner.startswith('Traceback'))

Lib/traceback.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def print_tb(tb, limit=None, file=None):
6363
filename = co.co_filename
6464
name = co.co_name
6565
_print(file,
66-
' File "%s", line %d, in %s' % (filename,lineno,name))
66+
' File "%s", line %d, in %s' % (filename, lineno, name))
6767
linecache.checkcache(filename)
6868
line = linecache.getline(filename, lineno, f.f_globals)
6969
if line: _print(file, ' ' + line.strip())
@@ -159,9 +159,8 @@ def print_exception(etype, value, tb, limit=None, file=None, chain=True):
159159
_print(file, 'Traceback (most recent call last):')
160160
print_tb(tb, limit, file)
161161
lines = format_exception_only(type(value), value)
162-
for line in lines[:-1]:
163-
_print(file, line, ' ')
164-
_print(file, lines[-1], '')
162+
for line in lines:
163+
_print(file, line, '')
165164

166165
def format_exception(etype, value, tb, limit=None, chain=True):
167166
"""Format a stack trace and the exception information.

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ Core and Builtins
327327
Library
328328
-------
329329

330+
- Issue 1326077: fix the formatting of SyntaxErrors by the traceback module.
331+
330332
- Issue #1665206 (partially): Move imports in cgitb to the top of the module
331333
instead of performing them in functions. Helps prevent import deadlocking in
332334
threads.

0 commit comments

Comments
 (0)