Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f1b993

Browse files
committed
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.
1 parent e860f9b commit 9f1b993

2 files changed

Lines changed: 144 additions & 67 deletions

File tree

Python/compile.c

Lines changed: 96 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -381,60 +381,88 @@ int is_free(int v)
381381
return 0;
382382
}
383383

384-
/* Error message including line number */
384+
/* com_fetch_program_text will attempt to load the line of text that
385+
the exception refers to. If it fails, it will return NULL but will
386+
not set an exception.
385387
386-
static void
387-
set_error_location(char *filename, int lineno)
388+
XXX The functionality of this function is quite similar to the
389+
functionality in tb_displayline() in traceback.c.
390+
*/
391+
392+
static PyObject *
393+
fetch_program_text(char *filename, int lineno)
388394
{
389-
PyObject *exc, *v, *tb, *tmp;
395+
FILE *fp;
396+
int i;
397+
char linebuf[1000];
390398

391-
/* add attributes for the line number and filename for the error */
392-
PyErr_Fetch(&exc, &v, &tb);
393-
PyErr_NormalizeException(&exc, &v, &tb);
394-
tmp = PyInt_FromLong(lineno);
395-
if (tmp == NULL)
396-
PyErr_Clear();
397-
else {
398-
if (PyObject_SetAttrString(v, "lineno", tmp))
399-
PyErr_Clear();
400-
Py_DECREF(tmp);
401-
}
402-
if (filename != NULL) {
403-
tmp = PyString_FromString(filename);
404-
if (tmp == NULL)
405-
PyErr_Clear();
406-
else {
407-
if (PyObject_SetAttrString(v, "filename", tmp))
408-
PyErr_Clear();
409-
Py_DECREF(tmp);
410-
}
399+
if (filename == NULL || lineno <= 0)
400+
return NULL;
401+
fp = fopen(filename, "r");
402+
if (fp == NULL)
403+
return NULL;
404+
for (i = 0; i < lineno; i++) {
405+
char *pLastChar = &linebuf[sizeof(linebuf) - 2];
406+
do {
407+
*pLastChar = '\0';
408+
if (fgets(linebuf, sizeof linebuf, fp) == NULL)
409+
break;
410+
/* fgets read *something*; if it didn't get as
411+
far as pLastChar, it must have found a newline
412+
or hit the end of the file; if pLastChar is \n,
413+
it obviously found a newline; else we haven't
414+
yet seen a newline, so must continue */
415+
} while (*pLastChar != '\0' && *pLastChar != '\n');
416+
}
417+
fclose(fp);
418+
if (i == lineno) {
419+
char *p = linebuf;
420+
while (*p == ' ' || *p == '\t' || *p == '\014')
421+
p++;
422+
return PyString_FromString(p);
411423
}
412-
PyErr_Restore(exc, v, tb);
424+
return NULL;
413425
}
414426

415427
static void
416428
com_error(struct compiling *c, PyObject *exc, char *msg)
417429
{
418-
PyObject *v;
430+
PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
431+
419432
if (c == NULL) {
420433
/* Error occurred via symtable call to
421434
is_constant_false */
422435
PyErr_SetString(exc, msg);
423436
return;
424437
}
425438
c->c_errors++;
426-
if (c->c_lineno <= 1) {
427-
/* Unknown line number or single interactive command */
439+
if (c->c_lineno < 1 || c->c_interactive) {
440+
/* Unknown line number or interactive input */
428441
PyErr_SetString(exc, msg);
429442
return;
430443
}
431444
v = PyString_FromString(msg);
432445
if (v == NULL)
433446
return; /* MemoryError, too bad */
434-
PyErr_SetObject(exc, v);
435-
Py_DECREF(v);
436447

437-
set_error_location(c->c_filename, c->c_lineno);
448+
line = fetch_program_text(c->c_filename, c->c_lineno);
449+
if (line == NULL) {
450+
Py_INCREF(Py_None);
451+
line = Py_None;
452+
}
453+
t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
454+
Py_None, line);
455+
if (t == NULL)
456+
goto exit;
457+
w = Py_BuildValue("(OO)", v, t);
458+
if (w == NULL)
459+
goto exit;
460+
PyErr_SetObject(exc, w);
461+
exit:
462+
Py_XDECREF(t);
463+
Py_XDECREF(v);
464+
Py_XDECREF(w);
465+
Py_XDECREF(line);
438466
}
439467

440468
/* Interface to the block stack */
@@ -3998,6 +4026,43 @@ get_ref_type(struct compiling *c, char *name)
39984026
return -1; /* can't get here */
39994027
}
40004028

4029+
/* Helper function for setting lineno and filename */
4030+
4031+
static void
4032+
set_error_location(char *filename, int lineno)
4033+
{
4034+
PyObject *exc, *v, *tb, *tmp;
4035+
4036+
/* add attributes for the line number and filename for the error */
4037+
PyErr_Fetch(&exc, &v, &tb);
4038+
PyErr_NormalizeException(&exc, &v, &tb);
4039+
tmp = PyInt_FromLong(lineno);
4040+
if (tmp == NULL)
4041+
PyErr_Clear();
4042+
else {
4043+
if (PyObject_SetAttrString(v, "lineno", tmp))
4044+
PyErr_Clear();
4045+
Py_DECREF(tmp);
4046+
}
4047+
if (filename != NULL) {
4048+
tmp = PyString_FromString(filename);
4049+
if (tmp == NULL)
4050+
PyErr_Clear();
4051+
else {
4052+
if (PyObject_SetAttrString(v, "filename", tmp))
4053+
PyErr_Clear();
4054+
Py_DECREF(tmp);
4055+
}
4056+
4057+
tmp = fetch_program_text(filename, lineno);
4058+
if (tmp) {
4059+
PyObject_SetAttrString(v, "text", tmp);
4060+
Py_DECREF(tmp);
4061+
}
4062+
}
4063+
PyErr_Restore(exc, v, tb);
4064+
}
4065+
40014066
static int
40024067
symtable_build(struct compiling *c, node *n)
40034068
{

Python/pythonrun.c

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -693,12 +693,18 @@ parse_syntax_error(PyObject *err, PyObject **message, char **filename,
693693

694694
if (!(v = PyObject_GetAttrString(err, "offset")))
695695
goto finally;
696-
hold = PyInt_AsLong(v);
697-
Py_DECREF(v);
698-
v = NULL;
699-
if (hold < 0 && PyErr_Occurred())
700-
goto finally;
701-
*offset = (int)hold;
696+
if (v == Py_None) {
697+
*offset = -1;
698+
Py_DECREF(v);
699+
v = NULL;
700+
} else {
701+
hold = PyInt_AsLong(v);
702+
Py_DECREF(v);
703+
v = NULL;
704+
if (hold < 0 && PyErr_Occurred())
705+
goto finally;
706+
*offset = (int)hold;
707+
}
702708

703709
if (!(v = PyObject_GetAttrString(err, "text")))
704710
goto finally;
@@ -720,6 +726,40 @@ PyErr_Print(void)
720726
PyErr_PrintEx(1);
721727
}
722728

729+
static void
730+
print_error_text(PyObject *f, int offset, char *text)
731+
{
732+
char *nl;
733+
if (offset >= 0) {
734+
if (offset > 0 && offset == (int)strlen(text))
735+
offset--;
736+
for (;;) {
737+
nl = strchr(text, '\n');
738+
if (nl == NULL || nl-text >= offset)
739+
break;
740+
offset -= (nl+1-text);
741+
text = nl+1;
742+
}
743+
while (*text == ' ' || *text == '\t') {
744+
text++;
745+
offset--;
746+
}
747+
}
748+
PyFile_WriteString(" ", f);
749+
PyFile_WriteString(text, f);
750+
if (*text == '\0' || text[strlen(text)-1] != '\n')
751+
PyFile_WriteString("\n", f);
752+
if (offset == -1)
753+
return;
754+
PyFile_WriteString(" ", f);
755+
offset--;
756+
while (offset > 0) {
757+
PyFile_WriteString(" ", f);
758+
offset--;
759+
}
760+
PyFile_WriteString("^\n", f);
761+
}
762+
723763
void
724764
PyErr_PrintEx(int set_sys_last_vars)
725765
{
@@ -795,36 +835,8 @@ PyErr_PrintEx(int set_sys_last_vars)
795835
sprintf(buf, "%d", lineno);
796836
PyFile_WriteString(buf, f);
797837
PyFile_WriteString("\n", f);
798-
if (text != NULL) {
799-
char *nl;
800-
if (offset > 0 &&
801-
offset == (int)strlen(text))
802-
offset--;
803-
for (;;) {
804-
nl = strchr(text, '\n');
805-
if (nl == NULL ||
806-
nl-text >= offset)
807-
break;
808-
offset -= (nl+1-text);
809-
text = nl+1;
810-
}
811-
while (*text == ' ' || *text == '\t') {
812-
text++;
813-
offset--;
814-
}
815-
PyFile_WriteString(" ", f);
816-
PyFile_WriteString(text, f);
817-
if (*text == '\0' ||
818-
text[strlen(text)-1] != '\n')
819-
PyFile_WriteString("\n", f);
820-
PyFile_WriteString(" ", f);
821-
offset--;
822-
while (offset > 0) {
823-
PyFile_WriteString(" ", f);
824-
offset--;
825-
}
826-
PyFile_WriteString("^\n", f);
827-
}
838+
if (text != NULL)
839+
print_error_text(f, offset, text);
828840
Py_INCREF(message);
829841
Py_DECREF(v);
830842
v = message;

0 commit comments

Comments
 (0)