Skip to content

Commit 79ff0d1

Browse files
authored
bpo-45494: Fix error location in EOF tokenizer errors (GH-29108)
1 parent 48744db commit 79ff0d1

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Parser/pegen.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,12 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...)
382382
Py_ssize_t col_offset;
383383
Py_ssize_t end_col_offset = -1;
384384
if (t->col_offset == -1) {
385-
col_offset = Py_SAFE_DOWNCAST(p->tok->cur - p->tok->buf,
386-
intptr_t, int);
385+
if (p->tok->cur == p->tok->buf) {
386+
col_offset = 0;
387+
} else {
388+
const char* start = p->tok->buf ? p->tok->line_start : p->tok->buf;
389+
col_offset = Py_SAFE_DOWNCAST(p->tok->cur - start, intptr_t, int);
390+
}
387391
} else {
388392
col_offset = t->col_offset + 1;
389393
}
@@ -410,6 +414,7 @@ get_error_line(Parser *p, Py_ssize_t lineno)
410414
assert(p->tok->fp == NULL || p->tok->fp == stdin);
411415

412416
char *cur_line = p->tok->fp_interactive ? p->tok->interactive_src_start : p->tok->str;
417+
assert(cur_line != NULL);
413418

414419
for (int i = 0; i < lineno - 1; i++) {
415420
cur_line = strchr(cur_line, '\n') + 1;

0 commit comments

Comments
 (0)