[Python-checkins] bpo-45494: Fix error location in EOF tokenizer errors (GH-29108)

miss-islington webhook-mailer at python.org
Sat Nov 20 12:59:42 EST 2021


https://github.com/python/cpython/commit/a427eb862f11888fa69fee520eb8a20bd396fcdb
commit: a427eb862f11888fa69fee520eb8a20bd396fcdb
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-11-20T09:59:34-08:00
summary:

bpo-45494: Fix error location in EOF tokenizer errors (GH-29108)

(cherry picked from commit 79ff0d1687e3f823fb121a19f0297ad052871b1b)

Co-authored-by: Pablo Galindo Salgado <Pablogsal at gmail.com>

files:
M Parser/pegen.c

diff --git a/Parser/pegen.c b/Parser/pegen.c
index 98f07a13c92cb..464a902173dfb 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -403,8 +403,12 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...)
     Py_ssize_t col_offset;
     Py_ssize_t end_col_offset = -1;
     if (t->col_offset == -1) {
-        col_offset = Py_SAFE_DOWNCAST(p->tok->cur - p->tok->buf,
-                                      intptr_t, int);
+        if (p->tok->cur == p->tok->buf) {
+            col_offset = 0;
+        } else {
+            const char* start = p->tok->buf  ? p->tok->line_start : p->tok->buf;
+            col_offset = Py_SAFE_DOWNCAST(p->tok->cur - start, intptr_t, int);
+        }
     } else {
         col_offset = t->col_offset + 1;
     }
@@ -431,6 +435,7 @@ get_error_line(Parser *p, Py_ssize_t lineno)
     assert(p->tok->fp == NULL || p->tok->fp == stdin);
 
     char *cur_line = p->tok->fp_interactive ? p->tok->interactive_src_start : p->tok->str;
+    assert(cur_line != NULL);
 
     for (int i = 0; i < lineno - 1; i++) {
         cur_line = strchr(cur_line, '\n') + 1;



More information about the Python-checkins mailing list