Skip to content
Open
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
17 changes: 17 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@ def test_error_offset_continuation_characters(self):
check = self.check
check('"\\\n"(1 for c in I,\\\n\\', 2, 2)

def testSyntaxErrorRange(self):
# gh-156894: the position was reported in bytes, not in characters,
# for the errors which cover a range
for source, offset, end_offset in [
('abcd = 00010', 8, 11),
('\u03b1\u03b2\u03b3\u03b4 = 00010', 8, 11),
('a\u0301b\u0308c\u20d7d\u1ab0 = 00010', 12, 15),
("abcd = ub'a'", 8, 10),
("\u03b1\u03b2\u03b3\u03b4 = ub'a'", 8, 10),
("a\u0301b\u0308c\u20d7d\u1ab0 = ub'a'", 12, 14),
]:
with self.subTest(source=source):
with self.assertRaises(SyntaxError) as cm:
compile(source, '<testcase>', 'exec')
self.assertEqual(cm.exception.offset, offset)
self.assertEqual(cm.exception.end_offset, end_offset)

def testSyntaxErrorOffset(self):
check = self.check
check('def fact(x):\n\treturn x!\n', 2, 10)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the position of syntax errors which cover a range if the line contains
non-ASCII characters before the error.
20 changes: 20 additions & 0 deletions Parser/tokenizer/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@

/* ############## ERRORS ############## */

/* Convert a 1-based column in bytes into a 1-based column in characters.
The line is UTF-8 encoded, so it is enough to skip continuation bytes. */
static int
byte_col_to_char_col(const char *line, int byte_col)
{
int char_col = 1;
for (int i = 0; i < byte_col - 1; i++) {
if ((line[i] & 0xC0) != 0x80) {
char_col++;
}
}
return char_col;
}

static int
_syntaxerror_range(struct tok_state *tok, const char *format,
int col_offset, int end_col_offset,
Expand All @@ -35,9 +49,15 @@ _syntaxerror_range(struct tok_state *tok, const char *format,
if (col_offset == -1) {
col_offset = (int)PyUnicode_GET_LENGTH(errtext);
}
else if (col_offset > 0) {
col_offset = byte_col_to_char_col(tok->line_start, col_offset);
}
if (end_col_offset == -1) {
end_col_offset = col_offset;
}
else if (end_col_offset > 0) {
end_col_offset = byte_col_to_char_col(tok->line_start, end_col_offset);
}

Py_ssize_t line_len = strcspn(tok->line_start, "\n");
if (line_len != tok->cur - tok->line_start) {
Expand Down
Loading