From 8540465eed15b1d9be83d6f683fb9f8b390d4c50 Mon Sep 17 00:00:00 2001 From: yangyang Date: Tue, 7 Apr 2020 02:17:35 +0800 Subject: [PATCH 1/3] bpo-35212: fix col_offset in f-strings --- Lib/test/test_ast.py | 15 +++++++++++++++ Python/ast.c | 10 ++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 6f86eb954ec55ae..e23641afea40104 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -645,6 +645,21 @@ def test_issue39579_dotted_name_end_col_offset(self): attr_b = tree.body[0].decorator_list[0].value self.assertEqual(attr_b.end_col_offset, 4) + def test_issue35212_fstring_col_offset(self): + tree = ast.parse(dedent(''' + ( + f"""Some multi-line text. + {a:b} + It goes on...""" + ) + ''').strip()) + fstr = tree.body[0].value + value = fstr.values[1].value + self.assertEqual(value.col_offset, 9) + self.assertEqual(value.lineno, 3) + self.assertEqual(value.end_col_offset, 10) + self.assertEqual(value.end_lineno, 3) + def test_ast_asdl_signature(self): self.assertEqual(ast.withitem.__doc__, "withitem(expr context_expr, expr? optional_vars)") self.assertEqual(ast.GtE.__doc__, "GtE") diff --git a/Python/ast.c b/Python/ast.c index 550ee03b1ac384d..4467485fefd72ad 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -4695,8 +4695,12 @@ fstring_fix_node_location(const node *parent, node *n, char *expr_str) if (substr) { start = substr; while (start > parent->n_str) { - if (start[0] == '\n') + if (start[0] == '\n') { + /* when the f-string expression is on a new line, clear + old offset and also don't count the `\n` */ + cols = -1; break; + } start--; } cols += (int)(substr - start); @@ -4770,7 +4774,9 @@ fstring_compile_expr(const char *expr_start, const char *expr_end, } /* Reuse str to find the correct column offset. */ str[0] = '{'; - str[len+1] = '}'; + /* bpo-35212: if the fstring has a format_spec, it doesn't match a close + brace. */ + str[len+1] = 0; fstring_fix_node_location(n, mod_n, str); mod = PyAST_FromNode(mod_n, &cf, "", c->c_arena); PyMem_RawFree(str); From 0b1cba050685a9dab9a22cc16437697a3554eea0 Mon Sep 17 00:00:00 2001 From: yangyang Date: Tue, 7 Apr 2020 13:59:26 +0800 Subject: [PATCH 2/3] make test_fstring accord with fixed col_offsets --- Lib/test/test_fstring.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 49663923e7f5aa2..20d345fc4b9a795 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -324,8 +324,8 @@ def test_ast_line_numbers_multiline_fstring(self): 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) def test_docstring(self): From 6c3dce2467faff1a26213352b4e4266dd7ee1381 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 7 Apr 2020 06:00:21 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core and Builtins/2020-04-07-06-00-20.bpo-35212.86avLy.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-04-07-06-00-20.bpo-35212.86avLy.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-04-07-06-00-20.bpo-35212.86avLy.rst b/Misc/NEWS.d/next/Core and Builtins/2020-04-07-06-00-20.bpo-35212.86avLy.rst new file mode 100644 index 000000000000000..4ae8a38ace5e4f8 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-04-07-06-00-20.bpo-35212.86avLy.rst @@ -0,0 +1 @@ +fix col_offset in f-strings \ No newline at end of file