Skip to content

Commit e821e23

Browse files
committed
bpo-40619: Correctly handle error text and offset for <string> input
1 parent 7443d42 commit e821e23

File tree

2 files changed

+3
-25
lines changed

2 files changed

+3
-25
lines changed

Lib/test/test_exceptions.py

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

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

Parser/pegen/pegen.c

Lines changed: 2 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,8 @@ _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+
error_line = PyUnicode_DecodeUTF8(p->tok->buf, size ? size - 1: size, "replace");
426403
if (!error_line) {
427404
goto error;
428405
}

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