Patchwork [5,of,7] py3: workaround to prevent switching kwargs keys between str and bytes

login
register
mail settings
Submitter Pulkit Goyal
Date June 17, 2017, 12:45 p.m.
Message ID <48ef02400b59a6d9d250.1497703523@workspace>
Download mbox | patch
Permalink /patch/21458/
State Accepted
Headers show

Comments

Pulkit Goyal - June 17, 2017, 12:45 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1497692808 -19800
#      Sat Jun 17 15:16:48 2017 +0530
# Node ID 48ef02400b59a6d9d250627d0e5f60e85c90cb06
# Parent  e95901baf03f983af8163a1d848233b29629d97b
py3: workaround to prevent switching kwargs keys between str and bytes

We are getting kwargs variable as keyword argument here, hence it keys must be
str. Also we need to pass that as keyword argument again, so its better to add
a r'' at few places instead of converting first to bytes and then to str.
Yuya Nishihara - June 18, 2017, 6:21 a.m.
On Sat, 17 Jun 2017 18:15:23 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1497692808 -19800
> #      Sat Jun 17 15:16:48 2017 +0530
> # Node ID 48ef02400b59a6d9d250627d0e5f60e85c90cb06
> # Parent  e95901baf03f983af8163a1d848233b29629d97b
> py3: workaround to prevent switching kwargs keys between str and bytes
> 
> We are getting kwargs variable as keyword argument here, hence it keys must be
> str. Also we need to pass that as keyword argument again, so its better to add
> a r'' at few places instead of converting first to bytes and then to str.
> 
> diff --git a/mercurial/exchange.py b/mercurial/exchange.py
> --- a/mercurial/exchange.py
> +++ b/mercurial/exchange.py
> @@ -1585,12 +1585,12 @@
>      usebundle2 = bundle2requested(bundlecaps)
>      # bundle10 case
>      if not usebundle2:
> -        if bundlecaps and not kwargs.get('cg', True):
> +        if bundlecaps and not kwargs.get(r'cg', True):
>              raise ValueError(_('request for bundle10 must include changegroup'))
>  
>          if kwargs:
> -            raise ValueError(_('unsupported getbundle arguments: %s')
> -                             % ', '.join(sorted(kwargs.keys())))
> +            raise ValueError(_(r'unsupported getbundle arguments: %s')
> +                             % r', '.join(sorted(kwargs.keys())))

I'm not sure if this ValueError is intended to be caught somewhere. If this
is just for sanity check, _() should be removed. If not, we'll have to use
more specific error class and put bytes in it.

Anybody know the detail?

Patch

diff --git a/mercurial/exchange.py b/mercurial/exchange.py
--- a/mercurial/exchange.py
+++ b/mercurial/exchange.py
@@ -1585,12 +1585,12 @@ 
     usebundle2 = bundle2requested(bundlecaps)
     # bundle10 case
     if not usebundle2:
-        if bundlecaps and not kwargs.get('cg', True):
+        if bundlecaps and not kwargs.get(r'cg', True):
             raise ValueError(_('request for bundle10 must include changegroup'))
 
         if kwargs:
-            raise ValueError(_('unsupported getbundle arguments: %s')
-                             % ', '.join(sorted(kwargs.keys())))
+            raise ValueError(_(r'unsupported getbundle arguments: %s')
+                             % r', '.join(sorted(kwargs.keys())))
         outgoing = _computeoutgoing(repo, heads, common)
         bundler = changegroup.getbundler('01', repo, bundlecaps)
         return changegroup.getsubsetraw(repo, outgoing, bundler, source)
@@ -1603,8 +1603,8 @@ 
             b2caps.update(bundle2.decodecaps(blob))
     bundler = bundle2.bundle20(repo.ui, b2caps)
 
-    kwargs['heads'] = heads
-    kwargs['common'] = common
+    kwargs[r'heads'] = heads
+    kwargs[r'common'] = common
 
     for name in getbundle2partsorder:
         func = getbundle2partsmapping[name]