Skip to content

Commit c4d2b6d

Browse files
pablogsalmiss-islington
authored andcommitted
pythongh-115823: Calculate correctly error locations when dealing with implicit encodings (pythonGH-115824)
(cherry picked from commit 015b97d) Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 0f7f5a4 commit c4d2b6d

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

Lib/test/test_exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ def baz():
301301
{
302302
6
303303
0="""''', 5, 13)
304+
check('b"fooжжж"'.encode(), 1, 1, 1, 10)
304305

305306
# Errors thrown by symtable.c
306307
check('x = [(yield i) for i in range(3)]', 1, 7)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Properly calculate error ranges in the parser when raising
2+
:exc:`SyntaxError` exceptions caused by invalid byte sequences. Patch by
3+
Pablo Galindo

Parser/pegen_errors.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -377,20 +377,18 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
377377
Py_ssize_t col_number = col_offset;
378378
Py_ssize_t end_col_number = end_col_offset;
379379

380-
if (p->tok->encoding != NULL) {
381-
col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
382-
if (col_number < 0) {
380+
col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
381+
if (col_number < 0) {
382+
goto error;
383+
}
384+
385+
if (end_col_offset > 0) {
386+
end_col_number = _PyPegen_byte_offset_to_character_offset(error_line, end_col_offset);
387+
if (end_col_number < 0) {
383388
goto error;
384389
}
385-
if (end_col_number > 0) {
386-
Py_ssize_t end_col_offset = _PyPegen_byte_offset_to_character_offset(error_line, end_col_number);
387-
if (end_col_offset < 0) {
388-
goto error;
389-
} else {
390-
end_col_number = end_col_offset;
391-
}
392-
}
393390
}
391+
394392
tmp = Py_BuildValue("(OnnNnn)", p->tok->filename, lineno, col_number, error_line, end_lineno, end_col_number);
395393
if (!tmp) {
396394
goto error;

0 commit comments

Comments
 (0)