Patchwork [2,of,2,STABLE] templater: fix handling of \-escapes in raw string literals

login
register
mail settings
Submitter Yuya Nishihara
Date June 21, 2015, 7:42 a.m.
Message ID <40c1ed4b163e1668c303.1434872576@mimosa>
Download mbox | patch
Permalink /patch/9740/
State Accepted
Headers show

Comments

Yuya Nishihara - June 21, 2015, 7:42 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1434860683 -32400
#      Sun Jun 21 13:24:43 2015 +0900
# Branch stable
# Node ID 40c1ed4b163e1668c3033125119f0c9d8cbb32d5
# Parent  2676242b76c64647b68701d927ff7d4288644a61
templater: fix handling of \-escapes in raw string literals

The backslash character should start escape sequences no matter if a string is
prefixed with 'r'. They are just not interpreted as escape sequences in raw
strings. revset.tokenize() handles them correctly, but templater didn't.

https://docs.python.org/2/reference/lexical_analysis.html#string-literals
Augie Fackler - June 22, 2015, 6:24 p.m.
On Sun, Jun 21, 2015 at 04:42:56PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1434860683 -32400
> #      Sun Jun 21 13:24:43 2015 +0900
> # Branch stable
> # Node ID 40c1ed4b163e1668c3033125119f0c9d8cbb32d5
> # Parent  2676242b76c64647b68701d927ff7d4288644a61
> templater: fix handling of \-escapes in raw string literals

Queued for stable, thanks.

>
> The backslash character should start escape sequences no matter if a string is
> prefixed with 'r'. They are just not interpreted as escape sequences in raw
> strings. revset.tokenize() handles them correctly, but templater didn't.
>
> https://docs.python.org/2/reference/lexical_analysis.html#string-literals
>
> diff --git a/mercurial/templater.py b/mercurial/templater.py
> --- a/mercurial/templater.py
> +++ b/mercurial/templater.py
> @@ -47,7 +47,7 @@ def tokenizer(data):
>              s = pos
>              while pos < end: # find closing quote
>                  d = program[pos]
> -                if decode and d == '\\': # skip over escaped characters
> +                if d == '\\': # skip over escaped characters
>                      pos += 2
>                      continue
>                  if d == c:
> diff --git a/tests/test-command-template.t b/tests/test-command-template.t
> --- a/tests/test-command-template.t
> +++ b/tests/test-command-template.t
> @@ -2280,6 +2280,17 @@ Test string escaping:
>    <>\n<]>
>    <>\n<
>
> +Test string escaping of quotes:
> +
> +  $ hg log -Ra -r0 -T '{"\""}\n'
> +  "
> +  $ hg log -Ra -r0 -T '{"\\\""}\n'
> +  \"
> +  $ hg log -Ra -r0 -T '{r"\""}\n'
> +  \"
> +  $ hg log -Ra -r0 -T '{r"\\\""}\n'
> +  \\\"
> +
>  Test leading backslashes:
>
>    $ cd latesttag
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -47,7 +47,7 @@  def tokenizer(data):
             s = pos
             while pos < end: # find closing quote
                 d = program[pos]
-                if decode and d == '\\': # skip over escaped characters
+                if d == '\\': # skip over escaped characters
                     pos += 2
                     continue
                 if d == c:
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -2280,6 +2280,17 @@  Test string escaping:
   <>\n<]>
   <>\n<
 
+Test string escaping of quotes:
+
+  $ hg log -Ra -r0 -T '{"\""}\n'
+  "
+  $ hg log -Ra -r0 -T '{"\\\""}\n'
+  \"
+  $ hg log -Ra -r0 -T '{r"\""}\n'
+  \"
+  $ hg log -Ra -r0 -T '{r"\\\""}\n'
+  \\\"
+
 Test leading backslashes:
 
   $ cd latesttag