Skip to content

Commit 1932da0

Browse files
[3.12] gh-115823: Calculate correctly error locations when dealing with implicit encodings (GH-115824) (#115949)
gh-115823: Calculate correctly error locations when dealing with implicit encodings (GH-115824) (cherry picked from commit 015b97d) Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 8668b3c commit 1932da0

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

370-
if (p->tok->encoding != NULL) {
371-
col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
372-
if (col_number < 0) {
370+
col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
371+
if (col_number < 0) {
372+
goto error;
373+
}
374+
375+
if (end_col_offset > 0) {
376+
end_col_number = _PyPegen_byte_offset_to_character_offset(error_line, end_col_offset);
377+
if (end_col_number < 0) {
373378
goto error;
374379
}
375-
if (end_col_number > 0) {
376-
Py_ssize_t end_col_offset = _PyPegen_byte_offset_to_character_offset(error_line, end_col_number);
377-
if (end_col_offset < 0) {
378-
goto error;
379-
} else {
380-
end_col_number = end_col_offset;
381-
}
382-
}
383380
}
381+
384382
tmp = Py_BuildValue("(OnnNnn)", p->tok->filename, lineno, col_number, error_line, end_lineno, end_col_number);
385383
if (!tmp) {
386384
goto error;

0 commit comments

Comments
 (0)