Patchwork [06,of,10] py3: add support to pass bool type variable into pycompat.sysbytes()

login
register
mail settings
Submitter Pulkit Goyal
Date May 31, 2017, 9:47 p.m.
Message ID <471f42c23f0c8793ee8b.1496267222@workspace>
Download mbox | patch
Permalink /patch/21101/
State Accepted
Headers show

Comments

Pulkit Goyal - May 31, 2017, 9:47 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1496262135 -19800
#      Thu Jun 01 01:52:15 2017 +0530
# Node ID 471f42c23f0c8793ee8bcd589940eb28a6d32106
# Parent  8a1b753a1eac83bd2b07ad1b3aa454e383794329
py3: add support to pass bool type variable into pycompat.sysbytes()

On Python 3, the way to convert a bool type variable to bytes type is to first
convert the bool type to str and then encode the str to bytes.

>>> ab = "abc"
>>> bv = bool(ab)
>>> type(bv)
<class 'bool'>
>>> "%b" % bv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unsupported format character 'b' (0x62) at index 1
>>> "%s" % bv
'True'
>>> ("%s" % bv).encode('ascii')
b'True'
Yuya Nishihara - June 1, 2017, 3:07 p.m.
On Thu, 01 Jun 2017 03:17:02 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1496262135 -19800
> #      Thu Jun 01 01:52:15 2017 +0530
> # Node ID 471f42c23f0c8793ee8bcd589940eb28a6d32106
> # Parent  8a1b753a1eac83bd2b07ad1b3aa454e383794329
> py3: add support to pass bool type variable into pycompat.sysbytes()
> 
> On Python 3, the way to convert a bool type variable to bytes type is to first
> convert the bool type to str and then encode the str to bytes.
> 
> >>> ab = "abc"
> >>> bv = bool(ab)
> >>> type(bv)
> <class 'bool'>
> >>> "%b" % bv
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ValueError: unsupported format character 'b' (0x62) at index 1
> >>> "%s" % bv
> 'True'
> >>> ("%s" % bv).encode('ascii')
> b'True'
> 
> diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
> --- a/mercurial/pycompat.py
> +++ b/mercurial/pycompat.py
> @@ -159,7 +159,12 @@
>          This never raises UnicodeEncodeError, but only ASCII characters
>          can be round-trip by sysstr(sysbytes(s)).
>          """
> -        return s.encode(u'utf-8')
> +        try:
> +            return s.encode(u'utf-8')
> +        except AttributeError:
> +            if isinstance(s, bool):
> +                return (r'%s' % s).encode(u'ascii')
> +            raise

Maybe you want bytestr() instead?
Pulkit Goyal - June 2, 2017, 2:05 a.m.
> Maybe you want bytestr() instead?

I didn't thought about that, sorry. I will send a V2 with bytestr()
and other changes. Can you please drop this and the next one before
they get public.
Yuya Nishihara - June 2, 2017, 12:14 p.m.
On Fri, 2 Jun 2017 07:35:25 +0530, Pulkit Goyal wrote:
> > Maybe you want bytestr() instead?
> 
> I didn't thought about that, sorry. I will send a V2 with bytestr()
> and other changes. Can you please drop this and the next one before
> they get public.

Okay, pruned a0d174b5ba0b and ad84062b826c. And now I'm reviewing the
changesets to be rebased.

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -159,7 +159,12 @@ 
         This never raises UnicodeEncodeError, but only ASCII characters
         can be round-trip by sysstr(sysbytes(s)).
         """
-        return s.encode(u'utf-8')
+        try:
+            return s.encode(u'utf-8')
+        except AttributeError:
+            if isinstance(s, bool):
+                return (r'%s' % s).encode(u'ascii')
+            raise
 
     def sysstr(s):
         """Return a keyword str to be passed to Python functions such as