Patchwork [3,of,3] py3: switch to unicode/bytes in pycompat.py

login
register
mail settings
Submitter Pulkit Goyal
Date Aug. 13, 2016, 12:38 a.m.
Message ID <9a350943e42d8969e52a.1471048706@pulkit-goyal>
Download mbox | patch
Permalink /patch/16267/
State Accepted
Headers show

Comments

Pulkit Goyal - Aug. 13, 2016, 12:38 a.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1471048490 -19800
#      Sat Aug 13 06:04:50 2016 +0530
# Node ID 9a350943e42d8969e52a0001a806938aa8e815e1
# Parent  1141751d3fbffc136a0afd6103c43532220f337d
py3: switch to unicode/bytes in pycompat.py

replace() requires unicode, so switching to unicodes as the arguments will be
converted to bytes by the transformer. The _alias() expects bytes, so making
sure that bytes are passed through.
Yuya Nishihara - Aug. 14, 2016, 2:37 a.m.
On Sat, 13 Aug 2016 06:08:26 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1471048490 -19800
> #      Sat Aug 13 06:04:50 2016 +0530
> # Node ID 9a350943e42d8969e52a0001a806938aa8e815e1
> # Parent  1141751d3fbffc136a0afd6103c43532220f337d
> py3: switch to unicode/bytes in pycompat.py
> 
> replace() requires unicode, so switching to unicodes as the arguments will be
> converted to bytes by the transformer.

bytes also provides replace().

https://docs.python.org/3/library/stdtypes.html#bytes.replace

> --- a/mercurial/pycompat.py	Sat Aug 13 04:21:42 2016 +0530
> +++ b/mercurial/pycompat.py	Sat Aug 13 06:04:50 2016 +0530
> @@ -43,7 +43,8 @@
>      """
>      for item in items:
>          try:
> -            lcase = item.replace('_', '').lower()
> +            item = item.decode('latin1')
> +            lcase = item.replace(u'_', u'').lower()
>              setattr(alias, lcase, getattr(origin, item))

This change seems okay, but I guess we'll need setattr/getattr wrappers
which support both bytes and unicode.

Any thoughts?

Patch

diff -r 1141751d3fbf -r 9a350943e42d mercurial/pycompat.py
--- a/mercurial/pycompat.py	Sat Aug 13 04:21:42 2016 +0530
+++ b/mercurial/pycompat.py	Sat Aug 13 06:04:50 2016 +0530
@@ -43,7 +43,8 @@ 
     """
     for item in items:
         try:
-            lcase = item.replace('_', '').lower()
+            item = item.decode('latin1')
+            lcase = item.replace(u'_', u'').lower()
             setattr(alias, lcase, getattr(origin, item))
         except AttributeError:
             pass
diff -r 1141751d3fbf -r 9a350943e42d tests/test-check-py3-compat.t
--- a/tests/test-check-py3-compat.t	Sat Aug 13 04:21:42 2016 +0530
+++ b/tests/test-check-py3-compat.t	Sat Aug 13 06:04:50 2016 +0530
@@ -122,24 +122,22 @@ 
   mercurial/hook.py: error importing: <TypeError> str expected, not bytes (error at i18n.py:*) (glob)
   mercurial/httpconnection.py: error importing: <TypeError> str expected, not bytes (error at i18n.py:*) (glob)
   mercurial/httppeer.py: error importing: <TypeError> str expected, not bytes (error at i18n.py:*) (glob)
-  mercurial/keepalive.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/localrepo.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/lock.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/mail.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/manifest.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/match.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/mdiff.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/merge.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/minirst.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/namespaces.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/obsolete.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/patch.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/pathutil.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/peer.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/pure/mpatch.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/pure/parsers.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/pushkey.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
-  mercurial/pvec.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:*) (glob)
+  mercurial/keepalive.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/localrepo.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/lock.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/mail.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/manifest.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/match.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/mdiff.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/merge.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/minirst.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/namespaces.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/obsolete.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/patch.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/pathutil.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/peer.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/pushkey.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
+  mercurial/pvec.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
   mercurial/registrar.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
   mercurial/repair.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)
   mercurial/repoview.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)