]> granicus.if.org Git - python/commitdiff
Fix a bug introduced by the warnings rewrite where tracebacks were being
authorBrett Cannon <bcannon@gmail.com>
Mon, 28 Apr 2008 03:23:50 +0000 (03:23 +0000)
committerBrett Cannon <bcannon@gmail.com>
Mon, 28 Apr 2008 03:23:50 +0000 (03:23 +0000)
improperly indented.

Closes issue #2699.

Lib/test/test_traceback.py
Modules/_testcapimodule.c
Python/traceback.c

index b42dbc4df4bb93f5214a9c19447bd6f67e6bd73e..a3a5e394a51bd4aad737e8274beaf11a701466d1 100644 (file)
@@ -1,10 +1,24 @@
 """Test cases for traceback module"""
 
+from _testcapi import test_traceback_print
+from StringIO import StringIO
+import sys
 import unittest
-from test.test_support import run_unittest, is_jython
+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()
+    test_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
     # formatting of SyntaxErrors works based on changes for 2.1.
@@ -154,8 +168,20 @@ def test():
         self.assertEqual(err, ['None\n'])
 
 
+class TracebackFormatTests(unittest.TestCase):
+
+    def test_traceback_indentation(self):
+        # Make sure that the traceback is properly indented.
+        tb_lines = example_traceback.splitlines()
+        self.assertEquals(len(tb_lines), 3)
+        banner, location, source_line = tb_lines
+        self.assert_(banner.startswith('Traceback'))
+        self.assert_(location.startswith('  File'))
+        self.assert_(source_line.startswith('raise'))
+
+
 def test_main():
-    run_unittest(TracebackCases)
+    run_unittest(TracebackCases, TracebackFormatTests)
 
 
 if __name__ == "__main__":
index 40eaaad63e76915c7330012940dfd8aff68b7f98..8b4dbce60aec4e27773e62c2f60ffaa28d1fbe30 100644 (file)
@@ -734,6 +734,24 @@ test_with_docstring(PyObject *self)
        Py_RETURN_NONE;
 }
 
+/* To test the format of tracebacks as printed out. */
+static PyObject *
+test_traceback_print(PyObject *self, PyObject *args)
+{
+       PyObject *file;
+       PyObject *traceback;
+       int result;
+       
+       if (!PyArg_ParseTuple(args, "OO:test_traceback_print",
+                               &traceback, &file))
+               return NULL;
+               
+       result = PyTraceBack_Print(traceback, file);
+       if (result < 0)
+               return NULL;
+       Py_RETURN_NONE;
+}
+
 static PyMethodDef TestMethods[] = {
        {"raise_exception",     raise_exception,                 METH_VARARGS},
        {"test_config",         (PyCFunction)test_config,        METH_NOARGS},
@@ -774,6 +792,7 @@ static PyMethodDef TestMethods[] = {
 #ifdef WITH_THREAD
        {"_test_thread_state",  test_thread_state,               METH_VARARGS},
 #endif
+       {"test_traceback_print", test_traceback_print,           METH_VARARGS},
        {NULL, NULL} /* sentinel */
 };
 
index 743399755c06dc518c426218879b243fa1f76bcc..a4819634d83642a67e7b44319f5c32973c779f75 100644 (file)
@@ -222,8 +222,6 @@ tb_displayline(PyObject *f, const char *filename, int lineno, const char *name)
        err = PyFile_WriteString(linebuf, f);
        if (err != 0)
                return err;
-
-        err = PyFile_WriteString("    ", f);
         return Py_DisplaySourceLine(f, filename, lineno);
 }