mirror of https://github.com/python/cpython
gh-94869: Fix the location in some expressions for multi-line f-string ast nodes (#94895)
This commit is contained in:
parent
4b4439daed
commit
2e9da8e352
|
@ -346,10 +346,39 @@ non-important content
|
|||
self.assertEqual(binop.lineno, 4)
|
||||
self.assertEqual(binop.left.lineno, 4)
|
||||
self.assertEqual(binop.right.lineno, 6)
|
||||
self.assertEqual(binop.col_offset, 4)
|
||||
self.assertEqual(binop.left.col_offset, 4)
|
||||
self.assertEqual(binop.col_offset, 3)
|
||||
self.assertEqual(binop.left.col_offset, 3)
|
||||
self.assertEqual(binop.right.col_offset, 7)
|
||||
|
||||
expr = """
|
||||
a = f'''
|
||||
{blech}
|
||||
'''
|
||||
"""
|
||||
t = ast.parse(expr)
|
||||
self.assertEqual(type(t), ast.Module)
|
||||
self.assertEqual(len(t.body), 1)
|
||||
# Check f'...'
|
||||
self.assertEqual(type(t.body[0]), ast.Assign)
|
||||
self.assertEqual(type(t.body[0].value), ast.JoinedStr)
|
||||
self.assertEqual(len(t.body[0].value.values), 3)
|
||||
self.assertEqual(type(t.body[0].value.values[1]), ast.FormattedValue)
|
||||
self.assertEqual(t.body[0].lineno, 2)
|
||||
self.assertEqual(t.body[0].value.lineno, 2)
|
||||
self.assertEqual(t.body[0].value.values[0].lineno, 2)
|
||||
self.assertEqual(t.body[0].value.values[1].lineno, 2)
|
||||
self.assertEqual(t.body[0].value.values[2].lineno, 2)
|
||||
self.assertEqual(t.body[0].col_offset, 0)
|
||||
self.assertEqual(t.body[0].value.col_offset, 4)
|
||||
self.assertEqual(t.body[0].value.values[0].col_offset, 4)
|
||||
self.assertEqual(t.body[0].value.values[1].col_offset, 4)
|
||||
self.assertEqual(t.body[0].value.values[2].col_offset, 4)
|
||||
# Check {blech}
|
||||
self.assertEqual(t.body[0].value.values[1].value.lineno, 3)
|
||||
self.assertEqual(t.body[0].value.values[1].value.end_lineno, 3)
|
||||
self.assertEqual(t.body[0].value.values[1].value.col_offset, 11)
|
||||
self.assertEqual(t.body[0].value.values[1].value.end_col_offset, 16)
|
||||
|
||||
def test_ast_line_numbers_with_parentheses(self):
|
||||
expr = """
|
||||
x = (
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Fix the column offsets for some expressions in multi-line f-strings
|
||||
:mod:`ast` nodes. Patch by Pablo Galindo.
|
|
@ -326,6 +326,9 @@ fstring_find_expr_location(Token *parent, const char* expr_start, char *expr_str
|
|||
start--;
|
||||
}
|
||||
*p_cols += (int)(expr_start - start);
|
||||
if (*start == '\n') {
|
||||
*p_cols -= 1;
|
||||
}
|
||||
}
|
||||
/* adjust the start based on the number of newlines encountered
|
||||
before the f-string expression */
|
||||
|
@ -416,7 +419,7 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end,
|
|||
NULL, p->arena);
|
||||
|
||||
p2->starting_lineno = t->lineno + lines;
|
||||
p2->starting_col_offset = t->col_offset + cols;
|
||||
p2->starting_col_offset = lines != 0 ? cols : t->col_offset + cols;
|
||||
|
||||
expr = _PyPegen_run_parser(p2);
|
||||
|
||||
|
|
Loading…
Reference in New Issue