Skip to content

Commit bcc3036

Browse files
authored
bpo-40619: Correctly handle error lines in programs without file mode (GH-20090)
1 parent a482dc5 commit bcc3036

File tree

2 files changed

+7
-25
lines changed

2 files changed

+7
-25
lines changed

Lib/test/test_exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ def bar():
228228
def baz():
229229
'''quux'''
230230
""", 9, 20)
231+
check("pass\npass\npass\n(1+)\npass\npass\npass", 4, 4)
232+
check("(1+)", 1, 4)
231233

232234
# Errors thrown by symtable.c
233235
check('x = [(yield i) for i in range(3)]', 1, 5)

Parser/pegen/pegen.c

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -300,30 +300,6 @@ raise_tokenizer_init_error(PyObject *filename)
300300
Py_XDECREF(tuple);
301301
}
302302

303-
static inline PyObject *
304-
get_error_line(char *buffer, int is_file)
305-
{
306-
const char *newline;
307-
if (is_file) {
308-
newline = strrchr(buffer, '\n');
309-
} else {
310-
newline = strchr(buffer, '\n');
311-
}
312-
313-
if (is_file) {
314-
while (newline > buffer && newline[-1] == '\n') {
315-
--newline;
316-
}
317-
}
318-
319-
if (newline) {
320-
return PyUnicode_DecodeUTF8(buffer, newline - buffer, "replace");
321-
}
322-
else {
323-
return PyUnicode_DecodeUTF8(buffer, strlen(buffer), "replace");
324-
}
325-
}
326-
327303
static int
328304
tokenizer_error(Parser *p)
329305
{
@@ -422,7 +398,11 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
422398
}
423399

424400
if (!error_line) {
425-
error_line = get_error_line(p->tok->buf, p->start_rule == Py_file_input);
401+
Py_ssize_t size = p->tok->inp - p->tok->buf;
402+
if (size && p->tok->buf[size-1] == '\n') {
403+
size--;
404+
}
405+
error_line = PyUnicode_DecodeUTF8(p->tok->buf, size, "replace");
426406
if (!error_line) {
427407
goto error;
428408
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy