Patchwork [5,of,6,py3,v2] revsetlang: perform quoting using ui.escapestr instead of repr()

login
register
mail settings
Submitter Augie Fackler
Date March 23, 2017, 3:11 p.m.
Message ID <2190115dab97a551cd5a.1490281898@arthedain.pit.corp.google.com>
Download mbox | patch
Permalink /patch/19609/
State Accepted
Headers show

Comments

Augie Fackler - March 23, 2017, 3:11 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1490280410 14400
#      Thu Mar 23 10:46:50 2017 -0400
# Node ID 2190115dab97a551cd5aac44a8a55e2df9fd6307
# Parent  6158a45eb9f22ee651f2677b420665788fcc72e2
revsetlang: perform quoting using ui.escapestr instead of repr()

This changes one of the doctest results, but I'm pretty sure on
inspection that it's an equivalent result.
Yuya Nishihara - March 24, 2017, 2:59 p.m.
On Thu, 23 Mar 2017 11:11:38 -0400, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <augie@google.com>
> # Date 1490280410 14400
> #      Thu Mar 23 10:46:50 2017 -0400
> # Node ID 2190115dab97a551cd5aac44a8a55e2df9fd6307
> # Parent  6158a45eb9f22ee651f2677b420665788fcc72e2
> revsetlang: perform quoting using ui.escapestr instead of repr()

> @@ -581,11 +582,11 @@ def _quote(s):
>      >>> _quote("asdf'\"")
>      '\'asdf\\\'"\''
>      >>> _quote('asdf\'')
> -    '"asdf\'"'
> +    "'asdf\\''"
>      >>> _quote(1)
>      "'1'"
>      """
> -    return repr(str(s))
> +    return "'%s'" % util.escapestr('%s' % s)
                                      ^^^^^^^^

Ah, found nit. I don't know if _quote(int) is necessary, but b'%s' can't be
used to convert int to byte string on Python 3. pycompat.bytestr() should
do a job.
Augie Fackler - March 24, 2017, 3:02 p.m.
On Fri, Mar 24, 2017 at 10:59 AM, Yuya Nishihara <yuya@tcha.org> wrote:
> On Thu, 23 Mar 2017 11:11:38 -0400, Augie Fackler wrote:
>> # HG changeset patch
>> # User Augie Fackler <augie@google.com>
>> # Date 1490280410 14400
>> #      Thu Mar 23 10:46:50 2017 -0400
>> # Node ID 2190115dab97a551cd5aac44a8a55e2df9fd6307
>> # Parent  6158a45eb9f22ee651f2677b420665788fcc72e2
>> revsetlang: perform quoting using ui.escapestr instead of repr()
>
>> @@ -581,11 +582,11 @@ def _quote(s):
>>      >>> _quote("asdf'\"")
>>      '\'asdf\\\'"\''
>>      >>> _quote('asdf\'')
>> -    '"asdf\'"'
>> +    "'asdf\\''"
>>      >>> _quote(1)
>>      "'1'"
>>      """
>> -    return repr(str(s))
>> +    return "'%s'" % util.escapestr('%s' % s)
>                                       ^^^^^^^^
>
> Ah, found nit. I don't know if _quote(int) is necessary, but b'%s' can't be
> used to convert int to byte string on Python 3. pycompat.bytestr() should
> do a job.

Should I send a followup?
Yuya Nishihara - March 24, 2017, 3:39 p.m.
On Fri, 24 Mar 2017 11:02:28 -0400, Augie Fackler wrote:
> On Fri, Mar 24, 2017 at 10:59 AM, Yuya Nishihara <yuya@tcha.org> wrote:
> > On Thu, 23 Mar 2017 11:11:38 -0400, Augie Fackler wrote:
> >> # HG changeset patch
> >> # User Augie Fackler <augie@google.com>
> >> # Date 1490280410 14400
> >> #      Thu Mar 23 10:46:50 2017 -0400
> >> # Node ID 2190115dab97a551cd5aac44a8a55e2df9fd6307
> >> # Parent  6158a45eb9f22ee651f2677b420665788fcc72e2
> >> revsetlang: perform quoting using ui.escapestr instead of repr()
> >
> >> @@ -581,11 +582,11 @@ def _quote(s):
> >>      >>> _quote("asdf'\"")
> >>      '\'asdf\\\'"\''
> >>      >>> _quote('asdf\'')
> >> -    '"asdf\'"'
> >> +    "'asdf\\''"
> >>      >>> _quote(1)
> >>      "'1'"
> >>      """
> >> -    return repr(str(s))
> >> +    return "'%s'" % util.escapestr('%s' % s)
> >                                       ^^^^^^^^
> >
> > Ah, found nit. I don't know if _quote(int) is necessary, but b'%s' can't be
> > used to convert int to byte string on Python 3. pycompat.bytestr() should
> > do a job.
> 
> Should I send a followup?

Yes, please. I've already pushed this with no inflight fix.

Patch

diff --git a/mercurial/revsetlang.py b/mercurial/revsetlang.py
--- a/mercurial/revsetlang.py
+++ b/mercurial/revsetlang.py
@@ -15,6 +15,7 @@  from . import (
     node,
     parser,
     pycompat,
+    util,
 )
 
 elements = {
@@ -581,11 +582,11 @@  def _quote(s):
     >>> _quote("asdf'\"")
     '\'asdf\\\'"\''
     >>> _quote('asdf\'')
-    '"asdf\'"'
+    "'asdf\\''"
     >>> _quote(1)
     "'1'"
     """
-    return repr(str(s))
+    return "'%s'" % util.escapestr('%s' % s)
 
 def formatspec(expr, *args):
     '''