Skip to content

Commit 35a43d4

Browse files
[3.11] gh-115823: Calculate correctly error locations when dealing with implicit encodings (GH-115824) (#115950)
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 0f7f5a4 commit 35a43d4

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

Lib/test/test_exceptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def testSyntaxErrorOffset(self):
234234
check('Python = "\u1e54\xfd\u0163\u0125\xf2\xf1" +', 1, 20)
235235
check(b'# -*- coding: cp1251 -*-\nPython = "\xcf\xb3\xf2\xee\xed" +',
236236
2, 19, encoding='cp1251')
237-
check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 18)
237+
check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 13)
238238
check('x = "a', 1, 5)
239239
check('lambda x: x = 2', 1, 1)
240240
check('f{a + b + c}', 1, 2)
@@ -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)