From 9f1b9932b87a6da14f2c010dfcf2ed08cb50b768 Mon Sep 17 00:00:00 2001 From: Jeremy Hylton Date: Wed, 28 Feb 2001 07:07:43 +0000 Subject: 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. --- Python/pythonrun.c | 84 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 36 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 48b875e149..40611b6c5e 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -693,12 +693,18 @@ parse_syntax_error(PyObject *err, PyObject **message, char **filename, if (!(v = PyObject_GetAttrString(err, "offset"))) goto finally; - hold = PyInt_AsLong(v); - Py_DECREF(v); - v = NULL; - if (hold < 0 && PyErr_Occurred()) - goto finally; - *offset = (int)hold; + if (v == Py_None) { + *offset = -1; + Py_DECREF(v); + v = NULL; + } else { + hold = PyInt_AsLong(v); + Py_DECREF(v); + v = NULL; + if (hold < 0 && PyErr_Occurred()) + goto finally; + *offset = (int)hold; + } if (!(v = PyObject_GetAttrString(err, "text"))) goto finally; @@ -720,6 +726,40 @@ PyErr_Print(void) PyErr_PrintEx(1); } +static void +print_error_text(PyObject *f, int offset, char *text) +{ + char *nl; + if (offset >= 0) { + if (offset > 0 && offset == (int)strlen(text)) + offset--; + for (;;) { + nl = strchr(text, '\n'); + if (nl == NULL || nl-text >= offset) + break; + offset -= (nl+1-text); + text = nl+1; + } + while (*text == ' ' || *text == '\t') { + text++; + offset--; + } + } + PyFile_WriteString(" ", f); + PyFile_WriteString(text, f); + if (*text == '\0' || text[strlen(text)-1] != '\n') + PyFile_WriteString("\n", f); + if (offset == -1) + return; + PyFile_WriteString(" ", f); + offset--; + while (offset > 0) { + PyFile_WriteString(" ", f); + offset--; + } + PyFile_WriteString("^\n", f); +} + void PyErr_PrintEx(int set_sys_last_vars) { @@ -795,36 +835,8 @@ PyErr_PrintEx(int set_sys_last_vars) sprintf(buf, "%d", lineno); PyFile_WriteString(buf, f); PyFile_WriteString("\n", f); - if (text != NULL) { - char *nl; - if (offset > 0 && - offset == (int)strlen(text)) - offset--; - for (;;) { - nl = strchr(text, '\n'); - if (nl == NULL || - nl-text >= offset) - break; - offset -= (nl+1-text); - text = nl+1; - } - while (*text == ' ' || *text == '\t') { - text++; - offset--; - } - PyFile_WriteString(" ", f); - PyFile_WriteString(text, f); - if (*text == '\0' || - text[strlen(text)-1] != '\n') - PyFile_WriteString("\n", f); - PyFile_WriteString(" ", f); - offset--; - while (offset > 0) { - PyFile_WriteString(" ", f); - offset--; - } - PyFile_WriteString("^\n", f); - } + if (text != NULL) + print_error_text(f, offset, text); Py_INCREF(message); Py_DECREF(v); v = message; -- cgit v1.2.1