Skip to content

Commit e055106

Browse files
committed
bpo-45494: Fix error location in EOF tokenizer errors
1 parent 81520fe commit e055106

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
@@ -377,8 +377,12 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...)
377377
Py_ssize_t col_offset;
378378
Py_ssize_t end_col_offset = -1;
379379
if (t->col_offset == -1) {
380-
col_offset = Py_SAFE_DOWNCAST(p->tok->cur - p->tok->buf,
381-
intptr_t, int);
380+
if (p->tok->cur == p->tok->buf) {
381+
col_offset = 0;
382+
} else {
383+
const char* start = p->tok->buf ? p->tok->line_start : p->tok->buf;
384+
col_offset = Py_SAFE_DOWNCAST(p->tok->cur - start, intptr_t, int);
385+
}
382386
} else {
383387
col_offset = t->col_offset + 1;
384388
}
@@ -405,6 +409,7 @@ get_error_line(Parser *p, Py_ssize_t lineno)
405409
assert(p->tok->fp == NULL || p->tok->fp == stdin);
406410

407411
char *cur_line = p->tok->fp_interactive ? p->tok->interactive_src_start : p->tok->str;
412+
assert(cur_line != NULL);
408413

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

0 commit comments

Comments
 (0)