[Python-checkins] bpo-40334: Fix shifting of nested f-strings in the new parser (GH-19771)

Lysandros Nikolaou webhook-mailer at python.org
Tue Apr 28 20:43:55 EDT 2020


https://github.com/python/cpython/commit/37af21b667a9f41437b5b8e451497d7725016df5
commit: 37af21b667a9f41437b5b8e451497d7725016df5
branch: master
author: Lysandros Nikolaou <lisandrosnik at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-04-29T01:43:50+01:00
summary:

bpo-40334: Fix shifting of nested f-strings in the new parser (GH-19771)

`JoinedStr`s and `FormattedValue also needs to be shifted, in order to correctly compute the location information of nested f-strings.

files:
M Lib/test/test_fstring.py
M Parser/pegen/parse_string.c

diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index 8cafbe863c288..4c240f34a3543 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -207,8 +207,7 @@ def test_ast_line_numbers_nested(self):
         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
diff --git a/Parser/pegen/parse_string.c b/Parser/pegen/parse_string.c
index 9a78a28d24196..834239e23fa87 100644
--- a/Parser/pegen/parse_string.c
+++ b/Parser/pegen/parse_string.c
@@ -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;
     }



More information about the Python-checkins mailing list