[Python-checkins] r64881 - in python/trunk: Include/traceback.h Lib/test/test_traceback.py Python/_warnings.c Python/traceback.c

amaury.forgeotdarc python-checkins at python.org
Fri Jul 11 23:45:07 CEST 2008


Author: amaury.forgeotdarc
Date: Fri Jul 11 23:45:06 2008
New Revision: 64881

Log:
#3342: In tracebacks, printed source lines were not indented since r62555.
#3343: Py_DisplaySourceLine should be a private function. Rename it to _Py_DisplaySourceLine.


Modified:
   python/trunk/Include/traceback.h
   python/trunk/Lib/test/test_traceback.py
   python/trunk/Python/_warnings.c
   python/trunk/Python/traceback.c

Modified: python/trunk/Include/traceback.h
==============================================================================
--- python/trunk/Include/traceback.h	(original)
+++ python/trunk/Include/traceback.h	Fri Jul 11 23:45:06 2008
@@ -19,7 +19,7 @@
 
 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;

Modified: python/trunk/Lib/test/test_traceback.py
==============================================================================
--- python/trunk/Lib/test/test_traceback.py	(original)
+++ python/trunk/Lib/test/test_traceback.py	Fri Jul 11 23:45:06 2008
@@ -177,7 +177,7 @@
         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():

Modified: python/trunk/Python/_warnings.c
==============================================================================
--- python/trunk/Python/_warnings.c	(original)
+++ python/trunk/Python/_warnings.c	Fri Jul 11 23:45:06 2008
@@ -256,7 +256,6 @@
     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 @@
         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();
 }
 

Modified: python/trunk/Python/traceback.c
==============================================================================
--- python/trunk/Python/traceback.c	(original)
+++ python/trunk/Python/traceback.c	Fri Jul 11 23:45:06 2008
@@ -123,7 +123,7 @@
 }
 
 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 @@
 		} 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 @@
 	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


More information about the Python-checkins mailing list