Skip to content

Commit 015b97d

Browse files
authored
gh-115823: Calculate correctly error locations when dealing with implicit encodings (#115824)
1 parent b7383b8 commit 015b97d

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
@@ -369,20 +369,18 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
369369
Py_ssize_t col_number = col_offset;
370370
Py_ssize_t end_col_number = end_col_offset;
371371

372-
if (p->tok->encoding != NULL) {
373-
col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
374-
if (col_number < 0) {
372+
col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
373+
if (col_number < 0) {
374+
goto error;
375+
}
376+
377+
if (end_col_offset > 0) {
378+
end_col_number = _PyPegen_byte_offset_to_character_offset(error_line, end_col_offset);
379+
if (end_col_number < 0) {
375380
goto error;
376381
}
377-
if (end_col_number > 0) {
378-
Py_ssize_t end_col_offset = _PyPegen_byte_offset_to_character_offset(error_line, end_col_number);
379-
if (end_col_offset < 0) {
380-
goto error;
381-
} else {
382-
end_col_number = end_col_offset;
383-
}
384-
}
385382
}
383+
386384
tmp = Py_BuildValue("(OnnNnn)", p->tok->filename, lineno, col_number, error_line, end_lineno, end_col_number);
387385
if (!tmp) {
388386
goto error;

0 commit comments

Comments
 (0)