]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#3342: In tracebacks, printed source lines were not indented since r62555.
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 11 Jul 2008 21:45:06 +0000 (21:45 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 11 Jul 2008 21:45:06 +0000 (21:45 +0000)
#3343: Py_DisplaySourceLine should be a private function. Rename it to _Py_DisplaySourceLine.

Include/traceback.h
Lib/test/test_traceback.py
Python/_warnings.c
Python/traceback.c

index b77cc9202b4a6346d90376d297200dfaa20afd3d..e7943dae9698016009c45f38c3cb510afefd093c 100644 (file)
@@ -19,7 +19,7 @@ typedef struct _traceback {
 
 PyAPI_FUNC(int) PyTraceBack_Here(struct _frame *);
 PyAPI_FUNC(int) PyTraceBack_Print(PyObject *, PyObject *);
-PyAPI_FUNC(int) Py_DisplaySourceLine(PyObject *, const char *, int);
+PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, const char *, int, int);
 
 /* Reveal traceback type so we can typecheck traceback objects */
 PyAPI_DATA(PyTypeObject) PyTraceBack_Type;
index 96b8938d3650ea712bc4b9859427557d3f3b6072..8034839499a3114177aaac100270d9815fafba0e 100644 (file)
@@ -177,7 +177,7 @@ class TracebackFormatTests(unittest.TestCase):
         banner, location, source_line = tb_lines
         self.assert_(banner.startswith('Traceback'))
         self.assert_(location.startswith('  File'))
-        self.assert_(source_line.startswith('raise'))
+        self.assert_(source_line.startswith('    raise'))
 
 
 def test_main():
index 07b98efb2bebdff88be2cd132e2978bfad095b53..5ed8b55b9f71d36584247525fd809823cd4da5df 100644 (file)
@@ -256,7 +256,6 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
     Py_XDECREF(name);
 
     /* Print "  source_line\n" */
-    PyFile_WriteString("  ", f_stderr);
     if (sourceline) {
         char *source_line_str = PyString_AS_STRING(sourceline);
         while (*source_line_str == ' ' || *source_line_str == '\t' ||
@@ -267,7 +266,8 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
         PyFile_WriteString("\n", f_stderr);
     }
     else
-        Py_DisplaySourceLine(f_stderr, PyString_AS_STRING(filename), lineno);
+        _Py_DisplaySourceLine(f_stderr, PyString_AS_STRING(filename), 
+                              lineno, 2);
     PyErr_Clear();
 }
 
index a4819634d83642a67e7b44319f5c32973c779f75..5df7694e66a43ae6d1e745cd366f1446aa7434d8 100644 (file)
@@ -123,7 +123,7 @@ PyTraceBack_Here(PyFrameObject *frame)
 }
 
 int
-Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno)
+_Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
 {
        int err = 0;
        FILE *xfp = NULL;
@@ -197,12 +197,27 @@ Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno)
                } while (*pLastChar != '\0' && *pLastChar != '\n');
        }
        if (i == lineno) {
+               char buf[11];
                char *p = linebuf;
                while (*p == ' ' || *p == '\t' || *p == '\014')
                        p++;
-                    err = PyFile_WriteString(p, f);
-                    if (err == 0 && strchr(p, '\n') == NULL)
-                            err = PyFile_WriteString("\n", f);
+
+               /* Write some spaces before the line */
+               strcpy(buf, "          ");
+               assert (strlen(buf) == 10);
+               while (indent > 0) {
+                       if(indent < 10)
+                               buf[indent] = '\0';
+                       err = PyFile_WriteString(buf, f);
+                       if (err != 0)
+                               break;
+                       indent -= 10;
+               }
+
+               if (err == 0)
+                       err = PyFile_WriteString(p, f);
+               if (err == 0 && strchr(p, '\n') == NULL)
+                       err = PyFile_WriteString("\n", f);
        }
        fclose(xfp);
        return err;
@@ -222,7 +237,7 @@ tb_displayline(PyObject *f, const char *filename, int lineno, const char *name)
        err = PyFile_WriteString(linebuf, f);
        if (err != 0)
                return err;
-        return Py_DisplaySourceLine(f, filename, lineno);
+        return _Py_DisplaySourceLine(f, filename, lineno, 4);
 }
 
 static int