Skip to content

Commit b4b4f14

Browse files
author
brett.cannon
committed
Fix a bug introduced by the warnings rewrite where tracebacks were being
improperly indented. Closes issue #2699. git-svn-id: http://svn.python.org/projects/python/trunk@62555 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 4af9bb3 commit b4b4f14

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

Lib/test/test_traceback.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
"""Test cases for traceback module"""
22

3+
from _testcapi import test_traceback_print
4+
from StringIO import StringIO
5+
import sys
36
import unittest
4-
from test.test_support import run_unittest, is_jython
7+
from test.test_support import run_unittest, is_jython, Error
58

69
import traceback
710

11+
try:
12+
raise KeyError
13+
except KeyError:
14+
type_, value, tb = sys.exc_info()
15+
file_ = StringIO()
16+
test_traceback_print(tb, file_)
17+
example_traceback = file_.getvalue()
18+
else:
19+
raise Error("unable to create test traceback string")
20+
21+
822
class TracebackCases(unittest.TestCase):
923
# For now, a very minimal set of tests. I want to be sure that
1024
# formatting of SyntaxErrors works based on changes for 2.1.
@@ -154,8 +168,20 @@ def test_without_exception(self):
154168
self.assertEqual(err, ['None\n'])
155169

156170

171+
class TracebackFormatTests(unittest.TestCase):
172+
173+
def test_traceback_indentation(self):
174+
# Make sure that the traceback is properly indented.
175+
tb_lines = example_traceback.splitlines()
176+
self.assertEquals(len(tb_lines), 3)
177+
banner, location, source_line = tb_lines
178+
self.assert_(banner.startswith('Traceback'))
179+
self.assert_(location.startswith(' File'))
180+
self.assert_(source_line.startswith('raise'))
181+
182+
157183
def test_main():
158-
run_unittest(TracebackCases)
184+
run_unittest(TracebackCases, TracebackFormatTests)
159185

160186

161187
if __name__ == "__main__":

Modules/_testcapimodule.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,24 @@ test_with_docstring(PyObject *self)
734734
Py_RETURN_NONE;
735735
}
736736

737+
/* To test the format of tracebacks as printed out. */
738+
static PyObject *
739+
test_traceback_print(PyObject *self, PyObject *args)
740+
{
741+
PyObject *file;
742+
PyObject *traceback;
743+
int result;
744+
745+
if (!PyArg_ParseTuple(args, "OO:test_traceback_print",
746+
&traceback, &file))
747+
return NULL;
748+
749+
result = PyTraceBack_Print(traceback, file);
750+
if (result < 0)
751+
return NULL;
752+
Py_RETURN_NONE;
753+
}
754+
737755
static PyMethodDef TestMethods[] = {
738756
{"raise_exception", raise_exception, METH_VARARGS},
739757
{"test_config", (PyCFunction)test_config, METH_NOARGS},
@@ -774,6 +792,7 @@ static PyMethodDef TestMethods[] = {
774792
#ifdef WITH_THREAD
775793
{"_test_thread_state", test_thread_state, METH_VARARGS},
776794
#endif
795+
{"test_traceback_print", test_traceback_print, METH_VARARGS},
777796
{NULL, NULL} /* sentinel */
778797
};
779798

Python/traceback.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,6 @@ tb_displayline(PyObject *f, const char *filename, int lineno, const char *name)
222222
err = PyFile_WriteString(linebuf, f);
223223
if (err != 0)
224224
return err;
225-
226-
err = PyFile_WriteString(" ", f);
227225
return Py_DisplaySourceLine(f, filename, lineno);
228226
}
229227

0 commit comments

Comments
 (0)