Skip to content

Commit 78a0f9c

Browse files
committed
Address feedback
1 parent 75e714c commit 78a0f9c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

Parser/pegen.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,18 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...)
383383
static PyObject *
384384
get_error_line(Parser *p, Py_ssize_t lineno)
385385
{
386+
// If p->tok->fp == NULL, the 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
386390
char *cur_line = p->tok->fp == NULL ? p->tok->str : p->tok->stdin_content;
387391
for (int i = 0; i < lineno - 1; i++) {
388392
cur_line = strchr(cur_line, '\n') + 1;
389393
}
390394

391395
char *next_newline;
392396
if ((next_newline = strchr(cur_line, '\n')) == NULL) { // This is the last line
393-
return PyUnicode_DecodeUTF8(cur_line, strlen(cur_line), "replace");
397+
next_newline = cur_line + strlen(cur_line);
394398
}
395399
return PyUnicode_DecodeUTF8(cur_line, next_newline - cur_line, "replace");
396400
}
@@ -431,6 +435,8 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
431435
}
432436

433437
if (!error_line) {
438+
// PyErr_ProgramTextObject returned NULL, so we're not parsing from a file
439+
assert(p->tok->fp == NULL || p->tok->fp == stdin);
434440
if (p->tok->lineno == lineno) {
435441
Py_ssize_t size = p->tok->inp - p->tok->buf;
436442
error_line = PyUnicode_DecodeUTF8(p->tok->buf, size, "replace");

Parser/tokenizer.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,7 @@ tok_nextc(struct tok_state *tok)
865865
tok->done = E_NOMEM;
866866
return EOF;
867867
}
868-
strcpy(tok->stdin_content, translated);
869-
tok->stdin_content[strlen(translated)] = 0;
868+
sprintf(tok->stdin_content, "%s", translated);
870869
}
871870
else {
872871
char *new_str = PyMem_Malloc(strlen(tok->stdin_content) + strlen(translated) + 1);

0 commit comments

Comments
 (0)