Patchwork [2,of,3,V3] py3: utility functions to convert keys of kwargs to bytes/unicodes

login
register
mail settings
Submitter Pulkit Goyal
Date Dec. 8, 2016, 7:12 p.m.
Message ID <a359ab71022408a864d5.1481224358@pulkit-goyal>
Download mbox | patch
Permalink /patch/17879/
State Superseded
Headers show

Comments

Pulkit Goyal - Dec. 8, 2016, 7:12 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1481127783 -19800
#      Wed Dec 07 21:53:03 2016 +0530
# Node ID a359ab71022408a864d58baba3b365ee262560df
# Parent  e6e1c531a879c091caeaf7597744e98bcfbb41c9
py3: utility functions to convert keys of kwargs to bytes/unicodes

Keys of keyword arguments need to be str(unicodes) on Python 3. We have a lot
of function where we pass keyword arguments. Having utility functions to help
converting keys to unicodes before passing and convert back them to bytes once
passed into the function will be helpful. We now have functions named
pycompat.strkwargs(dic) and pycompat.byteskwargs(dic) to help us.
Yuya Nishihara - Dec. 9, 2016, 10:44 a.m.
On Fri, 09 Dec 2016 00:42:38 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1481127783 -19800
> #      Wed Dec 07 21:53:03 2016 +0530
> # Node ID a359ab71022408a864d58baba3b365ee262560df
> # Parent  e6e1c531a879c091caeaf7597744e98bcfbb41c9
> py3: utility functions to convert keys of kwargs to bytes/unicodes
> 
> Keys of keyword arguments need to be str(unicodes) on Python 3. We have a lot
> of function where we pass keyword arguments. Having utility functions to help
> converting keys to unicodes before passing and convert back them to bytes once
> passed into the function will be helpful. We now have functions named
> pycompat.strkwargs(dic) and pycompat.byteskwargs(dic) to help us.
> 
> diff -r e6e1c531a879 -r a359ab710224 mercurial/pycompat.py
> --- a/mercurial/pycompat.py	Tue Dec 06 06:36:36 2016 +0530
> +++ b/mercurial/pycompat.py	Wed Dec 07 21:53:03 2016 +0530
> @@ -109,6 +109,20 @@
>          args = [a.encode('latin-1') for a in args]
>          return opts, args
>  
> +    # keys of keyword arguments in Python need to be strings which are unicodes
> +    # Python 3. This function take keyword arguments, convert the keys to str
> +    # if they are in bytes.
> +    def strkwargs(dic):
> +        dic = {k.decode('latin-1'): v for k, v in dic.iteritems()}
> +        return dic
> +
> +    # keys of keyword arguments need to be unicode while passing into a
> +    # a function. This function helps us to convert those keys back to bytes
> +    # again as we need to deal with bytes.
> +    def byteskwargs(dic):
> +        dic = {k.encode('latin-1'): v for k, v in dic.iteritems()}
> +        return dic

SyntaxError on Python 2.6. Maybe you sent old version?
Pulkit Goyal - Dec. 9, 2016, 10:54 a.m.
Can I see the traceback, because this is the iteritems() version and I
don't see anythinng breaking Python 2.6 :(

On Fri, Dec 9, 2016 at 4:14 PM, Yuya Nishihara <yuya@tcha.org> wrote:
> On Fri, 09 Dec 2016 00:42:38 +0530, Pulkit Goyal wrote:
>> # HG changeset patch
>> # User Pulkit Goyal <7895pulkit@gmail.com>
>> # Date 1481127783 -19800
>> #      Wed Dec 07 21:53:03 2016 +0530
>> # Node ID a359ab71022408a864d58baba3b365ee262560df
>> # Parent  e6e1c531a879c091caeaf7597744e98bcfbb41c9
>> py3: utility functions to convert keys of kwargs to bytes/unicodes
>>
>> Keys of keyword arguments need to be str(unicodes) on Python 3. We have a lot
>> of function where we pass keyword arguments. Having utility functions to help
>> converting keys to unicodes before passing and convert back them to bytes once
>> passed into the function will be helpful. We now have functions named
>> pycompat.strkwargs(dic) and pycompat.byteskwargs(dic) to help us.
>>
>> diff -r e6e1c531a879 -r a359ab710224 mercurial/pycompat.py
>> --- a/mercurial/pycompat.py   Tue Dec 06 06:36:36 2016 +0530
>> +++ b/mercurial/pycompat.py   Wed Dec 07 21:53:03 2016 +0530
>> @@ -109,6 +109,20 @@
>>          args = [a.encode('latin-1') for a in args]
>>          return opts, args
>>
>> +    # keys of keyword arguments in Python need to be strings which are unicodes
>> +    # Python 3. This function take keyword arguments, convert the keys to str
>> +    # if they are in bytes.
>> +    def strkwargs(dic):
>> +        dic = {k.decode('latin-1'): v for k, v in dic.iteritems()}
>> +        return dic
>> +
>> +    # keys of keyword arguments need to be unicode while passing into a
>> +    # a function. This function helps us to convert those keys back to bytes
>> +    # again as we need to deal with bytes.
>> +    def byteskwargs(dic):
>> +        dic = {k.encode('latin-1'): v for k, v in dic.iteritems()}
>> +        return dic
>
> SyntaxError on Python 2.6. Maybe you sent old version?

Patch

diff -r e6e1c531a879 -r a359ab710224 mercurial/pycompat.py
--- a/mercurial/pycompat.py	Tue Dec 06 06:36:36 2016 +0530
+++ b/mercurial/pycompat.py	Wed Dec 07 21:53:03 2016 +0530
@@ -109,6 +109,20 @@ 
         args = [a.encode('latin-1') for a in args]
         return opts, args
 
+    # keys of keyword arguments in Python need to be strings which are unicodes
+    # Python 3. This function take keyword arguments, convert the keys to str
+    # if they are in bytes.
+    def strkwargs(dic):
+        dic = {k.decode('latin-1'): v for k, v in dic.iteritems()}
+        return dic
+
+    # keys of keyword arguments need to be unicode while passing into a
+    # a function. This function helps us to convert those keys back to bytes
+    # again as we need to deal with bytes.
+    def byteskwargs(dic):
+        dic = {k.encode('latin-1'): v for k, v in dic.iteritems()}
+        return dic
+
 else:
     def sysstr(s):
         return s
@@ -131,6 +145,12 @@ 
     def getoptb(args, shortlist, namelist):
         return getopt.getopt(args, shortlist, namelist)
 
+    def strkwargs(dic):
+        return dic
+
+    def byteskwargs(dic):
+        return dic
+
     osname = os.name
     ospathsep = os.pathsep
     ossep = os.sep