Patchwork [08,of,10] py3: implement __bytes__ for committablectx

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

Comments

Pulkit Goyal - May 31, 2017, 9:47 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1496264118 -19800
#      Thu Jun 01 02:25:18 2017 +0530
# Node ID a5498ec51c800ca99be644710b13b0e78a4997b6
# Parent  25320a995bedaaabc4e7a7f40651d07a9bfb4803
py3: implement __bytes__ for committablectx

Before this method, calling bytes on workingctx or memctx calls
basectx.__bytes__ since the magic method was not defined for this class. When it
calls the method from basectx class, it returns TypeError because None is passed
into it.

After this commit `hg update -C` works on Python 3 if eol is not enabled.
Yuya Nishihara - June 1, 2017, 3:20 p.m.
On Thu, 01 Jun 2017 03:17:04 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1496264118 -19800
> #      Thu Jun 01 02:25:18 2017 +0530
> # Node ID a5498ec51c800ca99be644710b13b0e78a4997b6
> # Parent  25320a995bedaaabc4e7a7f40651d07a9bfb4803
> py3: implement __bytes__ for committablectx
> 
> Before this method, calling bytes on workingctx or memctx calls
> basectx.__bytes__ since the magic method was not defined for this class. When it
> calls the method from basectx class, it returns TypeError because None is passed
> into it.
> 
> After this commit `hg update -C` works on Python 3 if eol is not enabled.
> 
> diff --git a/mercurial/branchmap.py b/mercurial/branchmap.py
> --- a/mercurial/branchmap.py
> +++ b/mercurial/branchmap.py
> @@ -47,7 +47,7 @@
>          last, lrev = bin(last), int(lrev)
>          filteredhash = None
>          if len(cachekey) > 2:
> -            filteredhash = bin(cachekey[2])
> +            filteredhash = bin(cachekey[2:3][0])
>          partial = branchcache(tipnode=last, tiprev=lrev,
>                                filteredhash=filteredhash)
>          if not partial.validfor(repo):
> diff --git a/mercurial/context.py b/mercurial/context.py
> --- a/mercurial/context.py
> +++ b/mercurial/context.py
> @@ -1408,6 +1408,9 @@
>      def __str__(self):
>          return str(self._parents[0]) + r"+"
>  
> +    def __bytes__(self):
> +        return bytes(self._parents[0]) + "+"

Just an idea. I'm thinking of adding (pycompat|encoding).forwardtobytes(self)
and do:

  __str__ = forwardtobytes

> diff --git a/mercurial/hook.py b/mercurial/hook.py
> --- a/mercurial/hook.py
> +++ b/mercurial/hook.py
> @@ -97,7 +97,7 @@
>                           (hname, exc.args[0]))
>          else:
>              ui.warn(_('error: %s hook raised an exception: '
> -                           '%s\n') % (hname, exc))
> +                           '%s\n') % (hname, pycompat.sysbytes(str(exc))))

encoding.strtolocal(str(exc)) should be better. exc may be IOError which
could contain a valid non-ascii unicode on Python 3.

Patch

diff --git a/mercurial/branchmap.py b/mercurial/branchmap.py
--- a/mercurial/branchmap.py
+++ b/mercurial/branchmap.py
@@ -47,7 +47,7 @@ 
         last, lrev = bin(last), int(lrev)
         filteredhash = None
         if len(cachekey) > 2:
-            filteredhash = bin(cachekey[2])
+            filteredhash = bin(cachekey[2:3][0])
         partial = branchcache(tipnode=last, tiprev=lrev,
                               filteredhash=filteredhash)
         if not partial.validfor(repo):
diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -1408,6 +1408,9 @@ 
     def __str__(self):
         return str(self._parents[0]) + r"+"
 
+    def __bytes__(self):
+        return bytes(self._parents[0]) + "+"
+
     def __nonzero__(self):
         return True
 
diff --git a/mercurial/hook.py b/mercurial/hook.py
--- a/mercurial/hook.py
+++ b/mercurial/hook.py
@@ -97,7 +97,7 @@ 
                          (hname, exc.args[0]))
         else:
             ui.warn(_('error: %s hook raised an exception: '
-                           '%s\n') % (hname, exc))
+                           '%s\n') % (hname, pycompat.sysbytes(str(exc))))
         if throw:
             raise
         if not ui.tracebackflag:
diff --git a/tests/test-py3-commands.t b/tests/test-py3-commands.t
--- a/tests/test-py3-commands.t
+++ b/tests/test-py3-commands.t
@@ -118,6 +118,7 @@ 
   $ $PYTHON3 $HGBIN add iota
   $ $PYTHON3 $HGBIN status
   A iota
+  $ $PYTHON3 $HGBIN diff --nodates
   $ $PYTHON3 $HGBIN commit --message 'commit performed in Python 3'
   $ $PYTHON3 $HGBIN status