Skip to content

Commit a7e53a5

Browse files
author
amaury.forgeotdarc
committed
#3342: In tracebacks, printed source lines were not indented since r62555.
#3343: Py_DisplaySourceLine should be a private function. Rename it to _Py_DisplaySourceLine. git-svn-id: http://svn.python.org/projects/python/trunk@64881 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 691203e commit a7e53a5

4 files changed

Lines changed: 24 additions & 9 deletions

File tree

Include/traceback.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ typedef struct _traceback {
1919

2020
PyAPI_FUNC(int) PyTraceBack_Here(struct _frame *);
2121
PyAPI_FUNC(int) PyTraceBack_Print(PyObject *, PyObject *);
22-
PyAPI_FUNC(int) Py_DisplaySourceLine(PyObject *, const char *, int);
22+
PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, const char *, int, int);
2323

2424
/* Reveal traceback type so we can typecheck traceback objects */
2525
PyAPI_DATA(PyTypeObject) PyTraceBack_Type;

Lib/test/test_traceback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def test_traceback_indentation(self):
177177
banner, location, source_line = tb_lines
178178
self.assert_(banner.startswith('Traceback'))
179179
self.assert_(location.startswith(' File'))
180-
self.assert_(source_line.startswith('raise'))
180+
self.assert_(source_line.startswith(' raise'))
181181

182182

183183
def test_main():

Python/_warnings.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
256256
Py_XDECREF(name);
257257

258258
/* Print " source_line\n" */
259-
PyFile_WriteString(" ", f_stderr);
260259
if (sourceline) {
261260
char *source_line_str = PyString_AS_STRING(sourceline);
262261
while (*source_line_str == ' ' || *source_line_str == '\t' ||
@@ -267,7 +266,8 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
267266
PyFile_WriteString("\n", f_stderr);
268267
}
269268
else
270-
Py_DisplaySourceLine(f_stderr, PyString_AS_STRING(filename), lineno);
269+
_Py_DisplaySourceLine(f_stderr, PyString_AS_STRING(filename),
270+
lineno, 2);
271271
PyErr_Clear();
272272
}
273273

Python/traceback.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ PyTraceBack_Here(PyFrameObject *frame)
123123
}
124124

125125
int
126-
Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno)
126+
_Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
127127
{
128128
int err = 0;
129129
FILE *xfp = NULL;
@@ -197,12 +197,27 @@ Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno)
197197
} while (*pLastChar != '\0' && *pLastChar != '\n');
198198
}
199199
if (i == lineno) {
200+
char buf[11];
200201
char *p = linebuf;
201202
while (*p == ' ' || *p == '\t' || *p == '\014')
202203
p++;
203-
err = PyFile_WriteString(p, f);
204-
if (err == 0 && strchr(p, '\n') == NULL)
205-
err = PyFile_WriteString("\n", f);
204+
205+
/* Write some spaces before the line */
206+
strcpy(buf, " ");
207+
assert (strlen(buf) == 10);
208+
while (indent > 0) {
209+
if(indent < 10)
210+
buf[indent] = '\0';
211+
err = PyFile_WriteString(buf, f);
212+
if (err != 0)
213+
break;
214+
indent -= 10;
215+
}
216+
217+
if (err == 0)
218+
err = PyFile_WriteString(p, f);
219+
if (err == 0 && strchr(p, '\n') == NULL)
220+
err = PyFile_WriteString("\n", f);
206221
}
207222
fclose(xfp);
208223
return err;
@@ -222,7 +237,7 @@ tb_displayline(PyObject *f, const char *filename, int lineno, const char *name)
222237
err = PyFile_WriteString(linebuf, f);
223238
if (err != 0)
224239
return err;
225-
return Py_DisplaySourceLine(f, filename, lineno);
240+
return _Py_DisplaySourceLine(f, filename, lineno, 4);
226241
}
227242

228243
static int

0 commit comments

Comments
 (0)