Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def testSyntaxErrorOffset(self):
check('Python = "\u1e54\xfd\u0163\u0125\xf2\xf1" +', 1, 20)
check(b'# -*- coding: cp1251 -*-\nPython = "\xcf\xb3\xf2\xee\xed" +',
2, 19, encoding='cp1251')
check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 18)
check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 13)
check('x = "a', 1, 5)
check('lambda x: x = 2', 1, 1)
check('f{a + b + c}', 1, 2)
Expand Down Expand Up @@ -301,6 +301,7 @@ def baz():
{
6
0="""''', 5, 13)
check('b"fooжжж"'.encode(), 1, 1, 1, 10)

# Errors thrown by symtable.c
check('x = [(yield i) for i in range(3)]', 1, 7)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Properly calculate error ranges in the parser when raising
:exc:`SyntaxError` exceptions caused by invalid byte sequences. Patch by
Pablo Galindo
20 changes: 9 additions & 11 deletions Parser/pegen_errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,20 +377,18 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
Py_ssize_t col_number = col_offset;
Py_ssize_t end_col_number = end_col_offset;

if (p->tok->encoding != NULL) {
col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
if (col_number < 0) {
col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
if (col_number < 0) {
goto error;
}

if (end_col_offset > 0) {
end_col_number = _PyPegen_byte_offset_to_character_offset(error_line, end_col_offset);
if (end_col_number < 0) {
goto error;
}
if (end_col_number > 0) {
Py_ssize_t end_col_offset = _PyPegen_byte_offset_to_character_offset(error_line, end_col_number);
if (end_col_offset < 0) {
goto error;
} else {
end_col_number = end_col_offset;
}
}
}

tmp = Py_BuildValue("(OnnNnn)", p->tok->filename, lineno, col_number, error_line, end_lineno, end_col_number);
if (!tmp) {
goto error;
Expand Down