Patchwork [1,of,2] py3: convert to unicode to pass into encode()

login
register
mail settings
Submitter Pulkit Goyal
Date Oct. 7, 2016, 12:05 p.m.
Message ID <480cfc1623c2ad732300.1475841941@pulkit-goyal>
Download mbox | patch
Permalink /patch/16872/
State Accepted
Headers show

Comments

Pulkit Goyal - Oct. 7, 2016, 12:05 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1475835208 -7200
#      Fri Oct 07 12:13:28 2016 +0200
# Node ID 480cfc1623c2ad732300d942bbfab41343f7b8c5
# Parent  91a3c58ecf938ed675f5364b88f0d663f12b0047
py3: convert to unicode to pass into encode()

encoding.encoding is bytes, we need to pass it to encode() which accepts
unicodes in py3, so used pycomapt.sysstr() Also this can't be done using
transformer as that only transforms the string values not variables.
Yuya Nishihara - Oct. 7, 2016, 1:42 p.m.
On Fri, 07 Oct 2016 14:05:41 +0200, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1475835208 -7200
> #      Fri Oct 07 12:13:28 2016 +0200
> # Node ID 480cfc1623c2ad732300d942bbfab41343f7b8c5
> # Parent  91a3c58ecf938ed675f5364b88f0d663f12b0047
> py3: convert to unicode to pass into encode()
> 
> encoding.encoding is bytes, we need to pass it to encode() which accepts
> unicodes in py3, so used pycomapt.sysstr() Also this can't be done using
> transformer as that only transforms the string values not variables.
> 
> diff -r 91a3c58ecf93 -r 480cfc1623c2 mercurial/i18n.py
> --- a/mercurial/i18n.py	Mon Oct 03 13:24:56 2016 +0200
> +++ b/mercurial/i18n.py	Fri Oct 07 12:13:28 2016 +0200
> @@ -12,7 +12,10 @@
>  import os
>  import sys
>  
> -from . import encoding
> +from . import (
> +    encoding,
> +    pycompat,
> +)
>  
>  # modelled after templater.templatepath:
>  if getattr(sys, 'frozen', None) is not None:
> @@ -85,6 +88,7 @@
>              # means u.encode(sys.getdefaultencoding()).decode(enc). Since
>              # the Python encoding defaults to 'ascii', this fails if the
>              # translated string use non-ASCII characters.
> +            encoding.encoding = pycompat.sysstr(encoding.encoding)

We shouldn't convert encoding.encoding to unicode because it would break
_('checking encoding (%s)...\n') % encoding.encoding, bytes % unicode, for
 example.

>              _msgcache[message] = u.encode(encoding.encoding, "replace")
Pulkit Goyal - Oct. 7, 2016, 2:04 p.m.
On Fri, Oct 7, 2016 at 3:42 PM, Yuya Nishihara <yuya@tcha.org> wrote:
> On Fri, 07 Oct 2016 14:05:41 +0200, Pulkit Goyal wrote:
>> # HG changeset patch
>> # User Pulkit Goyal <7895pulkit@gmail.com>
>> # Date 1475835208 -7200
>> #      Fri Oct 07 12:13:28 2016 +0200
>> # Node ID 480cfc1623c2ad732300d942bbfab41343f7b8c5
>> # Parent  91a3c58ecf938ed675f5364b88f0d663f12b0047
>> py3: convert to unicode to pass into encode()
>>
>> encoding.encoding is bytes, we need to pass it to encode() which accepts
>> unicodes in py3, so used pycomapt.sysstr() Also this can't be done using
>> transformer as that only transforms the string values not variables.
>>
>> diff -r 91a3c58ecf93 -r 480cfc1623c2 mercurial/i18n.py
>> --- a/mercurial/i18n.py       Mon Oct 03 13:24:56 2016 +0200
>> +++ b/mercurial/i18n.py       Fri Oct 07 12:13:28 2016 +0200
>> @@ -12,7 +12,10 @@
>>  import os
>>  import sys
>>
>> -from . import encoding
>> +from . import (
>> +    encoding,
>> +    pycompat,
>> +)
>>
>>  # modelled after templater.templatepath:
>>  if getattr(sys, 'frozen', None) is not None:
>> @@ -85,6 +88,7 @@
>>              # means u.encode(sys.getdefaultencoding()).decode(enc). Since
>>              # the Python encoding defaults to 'ascii', this fails if the
>>              # translated string use non-ASCII characters.
>> +            encoding.encoding = pycompat.sysstr(encoding.encoding)
>
> We shouldn't convert encoding.encoding to unicode because it would break
> _('checking encoding (%s)...\n') % encoding.encoding, bytes % unicode, for
>  example.
I am gonna change it to encodingstr =
pycompat.sysstr(encoding.encoding) and use encodingstr below. Drop the
patch as that was pushed.
>
>>              _msgcache[message] = u.encode(encoding.encoding, "replace")

Patch

diff -r 91a3c58ecf93 -r 480cfc1623c2 mercurial/i18n.py
--- a/mercurial/i18n.py	Mon Oct 03 13:24:56 2016 +0200
+++ b/mercurial/i18n.py	Fri Oct 07 12:13:28 2016 +0200
@@ -12,7 +12,10 @@ 
 import os
 import sys
 
-from . import encoding
+from . import (
+    encoding,
+    pycompat,
+)
 
 # modelled after templater.templatepath:
 if getattr(sys, 'frozen', None) is not None:
@@ -85,6 +88,7 @@ 
             # means u.encode(sys.getdefaultencoding()).decode(enc). Since
             # the Python encoding defaults to 'ascii', this fails if the
             # translated string use non-ASCII characters.
+            encoding.encoding = pycompat.sysstr(encoding.encoding)
             _msgcache[message] = u.encode(encoding.encoding, "replace")
         except LookupError:
             # An unknown encoding results in a LookupError.
diff -r 91a3c58ecf93 -r 480cfc1623c2 tests/test-check-py3-compat.t
--- a/tests/test-check-py3-compat.t	Mon Oct 03 13:24:56 2016 +0200
+++ b/tests/test-check-py3-compat.t	Fri Oct 07 12:13:28 2016 +0200
@@ -120,53 +120,53 @@ 
   mercurial/httpconnection.py: error importing: <TypeError> Can't mix strings and bytes in path components (error at i18n.py:*)
   mercurial/httppeer.py: error importing: <TypeError> Can't mix strings and bytes in path components (error at i18n.py:*)
   mercurial/i18n.py: error importing module: <TypeError> bytes expected, not str (line *)
-  mercurial/keepalive.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/localrepo.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/lock.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/mail.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/manifest.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/match.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/mdiff.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/merge.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/minirst.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/namespaces.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/obsolete.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/patch.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/pathutil.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/peer.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/profiling.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/pushkey.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/pvec.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/registrar.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/repair.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/repoview.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/revlog.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/revset.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/scmutil.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/scmwindows.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/similar.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/simplemerge.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/sshpeer.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/sshserver.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/sslutil.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/statichttprepo.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/store.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/streamclone.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/subrepo.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/tagmerge.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/tags.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/templatefilters.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/templatekw.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/templater.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/transaction.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/ui.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/unionrepo.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/url.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/util.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
-  mercurial/verify.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
+  mercurial/keepalive.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/localrepo.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/lock.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/mail.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/manifest.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/match.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/mdiff.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/merge.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/minirst.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/namespaces.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/obsolete.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/patch.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/pathutil.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/peer.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/profiling.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/pushkey.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/pvec.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/registrar.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/repair.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/repoview.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/revlog.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/revset.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/scmutil.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/scmwindows.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/similar.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/simplemerge.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/sshpeer.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/sshserver.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/sslutil.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/statichttprepo.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/store.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/streamclone.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/subrepo.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/tagmerge.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/tags.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/templatefilters.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/templatekw.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/templater.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/transaction.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/ui.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/unionrepo.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/url.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/util.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
+  mercurial/verify.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
   mercurial/win32.py: error importing module: <ImportError> No module named 'msvcrt' (line *)
   mercurial/windows.py: error importing module: <ImportError> No module named 'msvcrt' (line *)
-  mercurial/wireproto.py: error importing: <TypeError> encode() argument 1 must be str, not bytes (error at i18n.py:*)
+  mercurial/wireproto.py: error importing: <TypeError> encode() argument 2 must be str, not bytes (error at i18n.py:*)
 
 #endif