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

login
register
mail settings
Submitter Pulkit Goyal
Date Oct. 5, 2016, 2:35 p.m.
Message ID <51e49c041614b463953b.1475678117@pulkit-goyal>
Download mbox | patch
Permalink /patch/16866/
State Accepted
Headers show

Comments

Pulkit Goyal - Oct. 5, 2016, 2:35 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1475594763 -19800
#      Tue Oct 04 20:56:03 2016 +0530
# Node ID 51e49c041614b463953b3973d5b58d8bbdcbbab3
# Parent  d24e03da24b52618f94005928970dde901f44e56
py3: convert to unicode to pass into encode()

encoding.encoding is bytes, we need to pass it to encode(), so converted
it into unicodes where we want to pass it because we don't want to change
the actual type. Also this can't be transformed using transformer as
that only transforms the string values not variables.
timeless - Oct. 5, 2016, 9:01 p.m.
> @@ -85,6 +85,9 @@
>              # means u.encode(sys.getdefaultencoding()).decode(enc). Since
>              # the Python encoding defaults to 'ascii', this fails if the
>              # translated string use non-ASCII characters.
> +            if (isinstance(encoding.encoding, bytes) and
> +                 sys.version_info[0] >= 3):
> +                 encoding.encoding = encoding.encoding.decode("ascii")

This feels like the kind of check that should be done once (possibly
swapping in the proper function) instead of per call. But I'm not the
owner...
Yuya Nishihara - Oct. 7, 2016, 4:43 a.m.
On Wed, 5 Oct 2016 17:01:03 -0400, timeless wrote:
> > @@ -85,6 +85,9 @@
> >              # means u.encode(sys.getdefaultencoding()).decode(enc). Since
> >              # the Python encoding defaults to 'ascii', this fails if the
> >              # translated string use non-ASCII characters.
> > +            if (isinstance(encoding.encoding, bytes) and
> > +                 sys.version_info[0] >= 3):
> > +                 encoding.encoding = encoding.encoding.decode("ascii")
> 
> This feels like the kind of check that should be done once (possibly
> swapping in the proper function) instead of per call. But I'm not the
> owner...

That's why I added pycompat.sysstr().

Patch

diff -r d24e03da24b5 -r 51e49c041614 mercurial/i18n.py
--- a/mercurial/i18n.py	Mon Sep 12 13:37:14 2016 +0200
+++ b/mercurial/i18n.py	Tue Oct 04 20:56:03 2016 +0530
@@ -85,6 +85,9 @@ 
             # means u.encode(sys.getdefaultencoding()).decode(enc). Since
             # the Python encoding defaults to 'ascii', this fails if the
             # translated string use non-ASCII characters.
+            if (isinstance(encoding.encoding, bytes) and
+                 sys.version_info[0] >= 3):
+                 encoding.encoding = encoding.encoding.decode("ascii")
             _msgcache[message] = u.encode(encoding.encoding, "replace")
         except LookupError:
             # An unknown encoding results in a LookupError.
diff -r d24e03da24b5 -r 51e49c041614 tests/test-check-py3-compat.t
--- a/tests/test-check-py3-compat.t	Mon Sep 12 13:37:14 2016 +0200
+++ b/tests/test-check-py3-compat.t	Tue Oct 04 20:56:03 2016 +0530
@@ -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