]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40334: Fix shifting of nested f-strings in the new parser (GH-19771)
authorLysandros Nikolaou <lisandrosnik@gmail.com>
Wed, 29 Apr 2020 00:43:50 +0000 (03:43 +0300)
committerGitHub <noreply@github.com>
Wed, 29 Apr 2020 00:43:50 +0000 (01:43 +0100)
`JoinedStr`s and `FormattedValue also needs to be shifted, in order to correctly compute the location information of nested f-strings.

Lib/test/test_fstring.py
Parser/pegen/parse_string.c

index 8cafbe863c288a6a3e3b4166595fe3c6644637ad..4c240f34a3543043a36bc816ba9696955eb8ec6b 100644 (file)
@@ -207,8 +207,7 @@ f'{a * f"-{x()}-"}'"""
         call = binop.right.values[1].value
         self.assertEqual(type(call), ast.Call)
         self.assertEqual(call.lineno, 3)
-        if support.use_old_parser():
-            self.assertEqual(call.col_offset, 11)
+        self.assertEqual(call.col_offset, 11)
 
     def test_ast_line_numbers_duplicate_expression(self):
         """Duplicate expression
index 9a78a28d241965b3e72e8c5eeb7a7f345101c53c..834239e23fa879299438859934ea3043f0331e1f 100644 (file)
@@ -449,6 +449,15 @@ static void fstring_shift_children_locations(expr_ty n, int lineno, int col_offs
         case Tuple_kind:
             fstring_shift_seq_locations(n, n->v.Tuple.elts, lineno, col_offset);
             break;
+        case JoinedStr_kind:
+            fstring_shift_seq_locations(n, n->v.JoinedStr.values, lineno, col_offset);
+            break;
+        case FormattedValue_kind:
+            shift_expr(n, n->v.FormattedValue.value, lineno, col_offset);
+            if (n->v.FormattedValue.format_spec) {
+                shift_expr(n, n->v.FormattedValue.format_spec, lineno, col_offset);
+            }
+            break;
         default:
             return;
     }