Skip to content

Commit

Permalink
bpo-40619: Correctly handle error text and offset for <string> input
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal committed May 14, 2020
1 parent 7443d42 commit e821e23
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 25 deletions.
1 change: 1 addition & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def bar():
def baz():
'''quux'''
""", 9, 20)
check("pass\npass\npass\n(1+)\npass\npass\npass", 4, 4)

# Errors thrown by symtable.c
check('x = [(yield i) for i in range(3)]', 1, 5)
Expand Down
27 changes: 2 additions & 25 deletions Parser/pegen/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,30 +300,6 @@ raise_tokenizer_init_error(PyObject *filename)
Py_XDECREF(tuple);
}

static inline PyObject *
get_error_line(char *buffer, int is_file)
{
const char *newline;
if (is_file) {
newline = strrchr(buffer, '\n');
} else {
newline = strchr(buffer, '\n');
}

if (is_file) {
while (newline > buffer && newline[-1] == '\n') {
--newline;
}
}

if (newline) {
return PyUnicode_DecodeUTF8(buffer, newline - buffer, "replace");
}
else {
return PyUnicode_DecodeUTF8(buffer, strlen(buffer), "replace");
}
}

static int
tokenizer_error(Parser *p)
{
Expand Down Expand Up @@ -422,7 +398,8 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
}

if (!error_line) {
error_line = get_error_line(p->tok->buf, p->start_rule == Py_file_input);
Py_ssize_t size = p->tok->inp - p->tok->buf;
error_line = PyUnicode_DecodeUTF8(p->tok->buf, size ? size - 1: size, "replace");
if (!error_line) {
goto error;
}
Expand Down

0 comments on commit e821e23

Please sign in to comment.