Skip to content

Commit a427eb8

Browse files
bpo-45494: Fix error location in EOF tokenizer errors (GH-29108)
(cherry picked from commit 79ff0d1) Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 511ee1c commit a427eb8

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
@@ -403,8 +403,12 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...)
403403
Py_ssize_t col_offset;
404404
Py_ssize_t end_col_offset = -1;
405405
if (t->col_offset == -1) {
406-
col_offset = Py_SAFE_DOWNCAST(p->tok->cur - p->tok->buf,
407-
intptr_t, int);
406+
if (p->tok->cur == p->tok->buf) {
407+
col_offset = 0;
408+
} else {
409+
const char* start = p->tok->buf ? p->tok->line_start : p->tok->buf;
410+
col_offset = Py_SAFE_DOWNCAST(p->tok->cur - start, intptr_t, int);
411+
}
408412
} else {
409413
col_offset = t->col_offset + 1;
410414
}
@@ -431,6 +435,7 @@ get_error_line(Parser *p, Py_ssize_t lineno)
431435
assert(p->tok->fp == NULL || p->tok->fp == stdin);
432436

433437
char *cur_line = p->tok->fp_interactive ? p->tok->interactive_src_start : p->tok->str;
438+
assert(cur_line != NULL);
434439

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

0 commit comments

Comments
 (0)