Kaydet (Commit) 2252d11c authored tarafından Amaury Forgeot d'Arc's avatar Amaury Forgeot d'Arc

#3342: In tracebacks, printed source lines were not indented since r62555.

#3343: Py_DisplaySourceLine should be a private function. Rename it to _Py_DisplaySourceLine.
üst ae6d2b91
...@@ -19,7 +19,7 @@ typedef struct _traceback { ...@@ -19,7 +19,7 @@ typedef struct _traceback {
PyAPI_FUNC(int) PyTraceBack_Here(struct _frame *); PyAPI_FUNC(int) PyTraceBack_Here(struct _frame *);
PyAPI_FUNC(int) PyTraceBack_Print(PyObject *, PyObject *); 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 */ /* Reveal traceback type so we can typecheck traceback objects */
PyAPI_DATA(PyTypeObject) PyTraceBack_Type; PyAPI_DATA(PyTypeObject) PyTraceBack_Type;
......
...@@ -177,7 +177,7 @@ class TracebackFormatTests(unittest.TestCase): ...@@ -177,7 +177,7 @@ class TracebackFormatTests(unittest.TestCase):
banner, location, source_line = tb_lines banner, location, source_line = tb_lines
self.assert_(banner.startswith('Traceback')) self.assert_(banner.startswith('Traceback'))
self.assert_(location.startswith(' File')) self.assert_(location.startswith(' File'))
self.assert_(source_line.startswith('raise')) self.assert_(source_line.startswith(' raise'))
def test_main(): def test_main():
......
...@@ -256,7 +256,6 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject ...@@ -256,7 +256,6 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
Py_XDECREF(name); Py_XDECREF(name);
/* Print " source_line\n" */ /* Print " source_line\n" */
PyFile_WriteString(" ", f_stderr);
if (sourceline) { if (sourceline) {
char *source_line_str = PyString_AS_STRING(sourceline); char *source_line_str = PyString_AS_STRING(sourceline);
while (*source_line_str == ' ' || *source_line_str == '\t' || while (*source_line_str == ' ' || *source_line_str == '\t' ||
...@@ -267,7 +266,8 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject ...@@ -267,7 +266,8 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
PyFile_WriteString("\n", f_stderr); PyFile_WriteString("\n", f_stderr);
} }
else else
Py_DisplaySourceLine(f_stderr, PyString_AS_STRING(filename), lineno); _Py_DisplaySourceLine(f_stderr, PyString_AS_STRING(filename),
lineno, 2);
PyErr_Clear(); PyErr_Clear();
} }
......
...@@ -123,7 +123,7 @@ PyTraceBack_Here(PyFrameObject *frame) ...@@ -123,7 +123,7 @@ PyTraceBack_Here(PyFrameObject *frame)
} }
int int
Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno) _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
{ {
int err = 0; int err = 0;
FILE *xfp = NULL; FILE *xfp = NULL;
...@@ -197,12 +197,27 @@ Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno) ...@@ -197,12 +197,27 @@ Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno)
} while (*pLastChar != '\0' && *pLastChar != '\n'); } while (*pLastChar != '\0' && *pLastChar != '\n');
} }
if (i == lineno) { if (i == lineno) {
char buf[11];
char *p = linebuf; char *p = linebuf;
while (*p == ' ' || *p == '\t' || *p == '\014') while (*p == ' ' || *p == '\t' || *p == '\014')
p++; p++;
err = PyFile_WriteString(p, f);
if (err == 0 && strchr(p, '\n') == NULL) /* Write some spaces before the line */
err = PyFile_WriteString("\n", f); 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); fclose(xfp);
return err; return err;
...@@ -222,7 +237,7 @@ tb_displayline(PyObject *f, const char *filename, int lineno, const char *name) ...@@ -222,7 +237,7 @@ tb_displayline(PyObject *f, const char *filename, int lineno, const char *name)
err = PyFile_WriteString(linebuf, f); err = PyFile_WriteString(linebuf, f);
if (err != 0) if (err != 0)
return err; return err;
return Py_DisplaySourceLine(f, filename, lineno); return _Py_DisplaySourceLine(f, filename, lineno, 4);
} }
static int static int
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment