diff Python/compile.c @ 16831:27f04d714ecb legacy-trunk

Print the offending line of code in the traceback for SyntaxErrors raised by the compiler. XXX For now, text entered into the interactive intepreter is not printed in the traceback. Inspired by a patch from Roman Sulzhyk compile.c: Add helper fetch_program_text() that opens a file and reads until it finds the specified line number. The code is a near duplicate of similar code in traceback.c. Modify com_error() to pass two arguments to SyntaxError constructor, where the second argument contains the offending text when possible. Modify set_error_location(), now used only by the symtable pass, to set the text attribute on existing exceptions. pythonrun.c: Change parse_syntax_error() to continue of the offset attribute of a SyntaxError is None. In this case, it sets offset to -1. Move code from PyErr_PrintEx() into helper function print_error_text(). In the helper, only print the caret for a SyntaxError if offset > 0.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Wed, 28 Feb 2001 07:07:43 +0000
parents acbe74f3a786
children 09f6c1372056
line wrap: on
line diff
--- a/Python/compile.c	Wed Feb 28 05:57:51 2001 +0000
+++ b/Python/compile.c	Wed Feb 28 07:07:43 2001 +0000
@@ -381,41 +381,54 @@
 	return 0;
 }
 
-/* Error message including line number */
-
-static void
-set_error_location(char *filename, int lineno)
+/* com_fetch_program_text will attempt to load the line of text that
+   the exception refers to.  If it fails, it will return NULL but will
+   not set an exception. 
+
+   XXX The functionality of this function is quite similar to the
+   functionality in tb_displayline() in traceback.c.
+*/
+
+static PyObject *
+fetch_program_text(char *filename, int lineno)
 {
-	PyObject *exc, *v, *tb, *tmp;
-
-	/* add attributes for the line number and filename for the error */
-	PyErr_Fetch(&exc, &v, &tb);
-	PyErr_NormalizeException(&exc, &v, &tb);
-	tmp = PyInt_FromLong(lineno);
-	if (tmp == NULL)
-		PyErr_Clear();
-	else {
-		if (PyObject_SetAttrString(v, "lineno", tmp))
-			PyErr_Clear();
-		Py_DECREF(tmp);
+	FILE *fp;
+	int i;
+	char linebuf[1000];
+
+	if (filename == NULL || lineno <= 0)
+		return NULL;
+	fp = fopen(filename, "r");
+	if (fp == NULL)
+		return NULL;
+	for (i = 0; i < lineno; i++) {
+		char *pLastChar = &linebuf[sizeof(linebuf) - 2];
+		do {
+			*pLastChar = '\0';
+			if (fgets(linebuf, sizeof linebuf, fp) == NULL)
+				break;
+			/* fgets read *something*; if it didn't get as
+			   far as pLastChar, it must have found a newline
+			   or hit the end of the file;	if pLastChar is \n,
+			   it obviously found a newline; else we haven't
+			   yet seen a newline, so must continue */
+		} while (*pLastChar != '\0' && *pLastChar != '\n');
 	}
-	if (filename != NULL) {
-		tmp = PyString_FromString(filename);
-		if (tmp == NULL)
-			PyErr_Clear();
-		else {
-			if (PyObject_SetAttrString(v, "filename", tmp))
-				PyErr_Clear();
-			Py_DECREF(tmp);
-		}
+	fclose(fp);
+	if (i == lineno) {
+		char *p = linebuf;
+		while (*p == ' ' || *p == '\t' || *p == '\014')
+			p++;
+		return PyString_FromString(p);
 	}
-	PyErr_Restore(exc, v, tb);
+	return NULL;
 }
 
 static void
 com_error(struct compiling *c, PyObject *exc, char *msg)
 {
-	PyObject *v;
+	PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
+
 	if (c == NULL) {
 		/* Error occurred via symtable call to
 		   is_constant_false */
@@ -423,18 +436,33 @@
 		return;
 	}
 	c->c_errors++;
-	if (c->c_lineno <= 1) {
-		/* Unknown line number or single interactive command */
+	if (c->c_lineno < 1 || c->c_interactive) { 
+		/* Unknown line number or interactive input */
 		PyErr_SetString(exc, msg);
 		return;
 	}
 	v = PyString_FromString(msg);
 	if (v == NULL)
 		return; /* MemoryError, too bad */
-	PyErr_SetObject(exc, v);
-	Py_DECREF(v);
-
-	set_error_location(c->c_filename, c->c_lineno);
+
+	line = fetch_program_text(c->c_filename, c->c_lineno);
+	if (line == NULL) {
+		Py_INCREF(Py_None);
+		line = Py_None;
+	}
+	t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
+			  Py_None, line);
+	if (t == NULL)
+		goto exit;
+	w = Py_BuildValue("(OO)", v, t);
+	if (w == NULL)
+		goto exit;
+	PyErr_SetObject(exc, w);
+ exit:
+	Py_XDECREF(t);
+	Py_XDECREF(v);
+	Py_XDECREF(w);
+	Py_XDECREF(line);
 }
 
 /* Interface to the block stack */
@@ -3998,6 +4026,43 @@
 	return -1; /* can't get here */
 }
 
+/* Helper function for setting lineno and filename */
+
+static void
+set_error_location(char *filename, int lineno)
+{
+	PyObject *exc, *v, *tb, *tmp;
+
+	/* add attributes for the line number and filename for the error */
+	PyErr_Fetch(&exc, &v, &tb);
+	PyErr_NormalizeException(&exc, &v, &tb);
+	tmp = PyInt_FromLong(lineno);
+	if (tmp == NULL)
+		PyErr_Clear();
+	else {
+		if (PyObject_SetAttrString(v, "lineno", tmp))
+			PyErr_Clear();
+		Py_DECREF(tmp);
+	}
+	if (filename != NULL) {
+		tmp = PyString_FromString(filename);
+		if (tmp == NULL)
+			PyErr_Clear();
+		else {
+			if (PyObject_SetAttrString(v, "filename", tmp))
+				PyErr_Clear();
+			Py_DECREF(tmp);
+		}
+
+		tmp = fetch_program_text(filename, lineno);
+		if (tmp) {
+			PyObject_SetAttrString(v, "text", tmp);
+			Py_DECREF(tmp);
+		}
+	}
+	PyErr_Restore(exc, v, tb);
+}
+
 static int
 symtable_build(struct compiling *c, node *n)
 {