bpo-43591: Fix error location in interactive mode for errors at the end of the line (GH-24973)
Co-authored-by: Erlend Egeberg Aasland
diff --git a/Parser/pegen.c b/Parser/pegen.c
index 24aa3af..953480d 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -147,7 +147,11 @@
if (!str) {
return 0;
}
- assert(col_offset >= 0 && (unsigned long)col_offset <= strlen(str));
+ Py_ssize_t len = strlen(str);
+ if (col_offset > len) {
+ col_offset = len;
+ }
+ assert(col_offset >= 0);
PyObject *text = PyUnicode_DecodeUTF8(str, col_offset, "replace");
if (!text) {
return 0;
@@ -392,10 +396,10 @@
static PyObject *
get_error_line(Parser *p, Py_ssize_t lineno)
{
- /* If p->tok->fp == NULL, then we're parsing from a string, which means that
- the whole source is stored in p->tok->str. If not, then we're parsing
- from the REPL, so the source lines of the current (multi-line) statement
- are stored in p->tok->stdin_content */
+ /* If the file descriptor is interactive, the source lines of the current
+ * (multi-line) statement are stored in p->tok->interactive_src_start.
+ * If not, we're parsing from a string, which means that the whole source
+ * is stored in p->tok->str. */
assert(p->tok->fp == NULL || p->tok->fp == stdin);
char *cur_line = p->tok->fp_interactive ? p->tok->interactive_src_start : p->tok->str;