summaryrefslogtreecommitdiffstats
path: root/Python/pythonrun.c
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2001-02-28 07:07:43 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2001-02-28 07:07:43 (GMT)
commit9f1b9932b87a6da14f2c010dfcf2ed08cb50b768 (patch)
tree09e9969a45c9171d9bf85c2819c2d7656753d999 /Python/pythonrun.c
parente860f9b983568efad41ab8cc828a107cf2096cb4 (diff)
downloadcpython-9f1b9932b87a6da14f2c010dfcf2ed08cb50b768.zip
cpython-9f1b9932b87a6da14f2c010dfcf2ed08cb50b768.tar.gz
cpython-9f1b9932b87a6da14f2c010dfcf2ed08cb50b768.tar.bz2
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.
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r--Python/pythonrun.c84
1 files changed, 48 insertions, 36 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 48b875e..40611b6 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;