Skip to content

Commit e5fe509

Browse files
bpo-42827: Fix crash on SyntaxError in multiline expressions (GH-24140)
When trying to extract the error line for the error message there are two distinct cases: 1. The input comes from a file, which means that we can extract the error line by using `PyErr_ProgramTextObject` and which we already do. 2. The input does not come from a file, at which point we need to get the source code from the tokenizer: * If the tokenizer's current line number is the same with the line of the error, we get the line from `tok->buf` and we're ready. * Else, we can extract the error line from the source code in the following two ways: * If the input comes from a string we have all the input in `tok->str` and we can extract the error line from it. * If the input comes from stdin, i.e. the interactive prompt, we do not have access to the previous line. That's why a new field `tok->stdin_content` is added which holds the whole input for the current (multiline) statement or expression. We can then extract the error line from `tok->stdin_content` like we do in the string case above. Co-authored-by: Pablo Galindo <[email protected]>
1 parent 9712358 commit e5fe509

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

Lib/test/test_exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ def testSyntaxErrorOffset(self):
209209
check('x = "a', 1, 7)
210210
check('lambda x: x = 2', 1, 1)
211211
check('f{a + b + c}', 1, 2)
212+
check('[file for str(file) in []\n])', 1, 11)
213+
check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5)
214+
check('[file for\n str(file) in []]', 2, 2)
212215

213216
# Errors thrown by compile.c
214217
check('class foo:return 1', 1, 11)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash when working out the error line of a :exc:`SyntaxError` in some
2+
multi-line expressions.

Parser/pegen.c

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,27 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...)
380380
return NULL;
381381
}
382382

383+
static PyObject *
384+
get_error_line(Parser *p, Py_ssize_t lineno)
385+
{
386+
/* If p->tok->fp == NULL, then we're parsing from a string, which means that
387+
the whole source is stored in p->tok->str. If not, then we're parsing
388+
from the REPL, so the source lines of the current (multi-line) statement
389+
are stored in p->tok->stdin_content */
390+
assert(p->tok->fp == NULL || p->tok->fp == stdin);
391+
392+
char *cur_line = p->tok->fp == NULL ? p->tok->str : p->tok->stdin_content;
393+
for (int i = 0; i < lineno - 1; i++) {
394+
cur_line = strchr(cur_line, '\n') + 1;
395+
}
396+
397+
char *next_newline;
398+
if ((next_newline = strchr(cur_line, '\n')) == NULL) { // This is the last line
399+
next_newline = cur_line + strlen(cur_line);
400+
}
401+
return PyUnicode_DecodeUTF8(cur_line, next_newline - cur_line, "replace");
402+
}
403+
383404
void *
384405
_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
385406
Py_ssize_t lineno, Py_ssize_t col_offset,
@@ -416,8 +437,22 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
416437
}
417438

418439
if (!error_line) {
419-
Py_ssize_t size = p->tok->inp - p->tok->buf;
420-
error_line = PyUnicode_DecodeUTF8(p->tok->buf, size, "replace");
440+
/* PyErr_ProgramTextObject was not called or returned NULL. If it was not called,
441+
then we need to find the error line from some other source, because
442+
p->start_rule != Py_file_input. If it returned NULL, then it either unexpectedly
443+
failed or we're parsing from a string or the REPL. There's a third edge case where
444+
we're actually parsing from a file, which has an E_EOF SyntaxError and in that case
445+
`PyErr_ProgramTextObject` fails because lineno points to last_file_line + 1, which
446+
does not physically exist */
447+
assert(p->tok->fp == NULL || p->tok->fp == stdin || p->tok->done == E_EOF);
448+
449+
if (p->tok->lineno == lineno) {
450+
Py_ssize_t size = p->tok->inp - p->tok->buf;
451+
error_line = PyUnicode_DecodeUTF8(p->tok->buf, size, "replace");
452+
}
453+
else {
454+
error_line = get_error_line(p, lineno);
455+
}
421456
if (!error_line) {
422457
goto error;
423458
}

Parser/tokenizer.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ tok_new(void)
8181
tok->decoding_readline = NULL;
8282
tok->decoding_buffer = NULL;
8383
tok->type_comments = 0;
84+
tok->stdin_content = NULL;
8485

8586
tok->async_hacks = 0;
8687
tok->async_def = 0;
@@ -816,6 +817,8 @@ PyTokenizer_Free(struct tok_state *tok)
816817
PyMem_Free(tok->buf);
817818
if (tok->input)
818819
PyMem_Free(tok->input);
820+
if (tok->stdin_content)
821+
PyMem_Free(tok->stdin_content);
819822
PyMem_Free(tok);
820823
}
821824

@@ -856,6 +859,24 @@ tok_nextc(struct tok_state *tok)
856859
if (translated == NULL)
857860
return EOF;
858861
newtok = translated;
862+
if (tok->stdin_content == NULL) {
863+
tok->stdin_content = PyMem_Malloc(strlen(translated) + 1);
864+
if (tok->stdin_content == NULL) {
865+
tok->done = E_NOMEM;
866+
return EOF;
867+
}
868+
sprintf(tok->stdin_content, "%s", translated);
869+
}
870+
else {
871+
char *new_str = PyMem_Malloc(strlen(tok->stdin_content) + strlen(translated) + 1);
872+
if (new_str == NULL) {
873+
tok->done = E_NOMEM;
874+
return EOF;
875+
}
876+
sprintf(new_str, "%s%s", tok->stdin_content, translated);
877+
PyMem_Free(tok->stdin_content);
878+
tok->stdin_content = new_str;
879+
}
859880
}
860881
if (tok->encoding && newtok && *newtok) {
861882
/* Recode to UTF-8 */

Parser/tokenizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct tok_state {
3737
int atbol; /* Nonzero if at begin of new line */
3838
int pendin; /* Pending indents (if > 0) or dedents (if < 0) */
3939
const char *prompt, *nextprompt; /* For interactive prompting */
40+
char *stdin_content;
4041
int lineno; /* Current line number */
4142
int first_lineno; /* First line of a single line or multi line string
4243
expression (cf. issue 16806) */

0 commit comments

Comments
 (0)