Patchwork D3708: py3: replace `unicode` with `type(u'')` in isinstance() calls

login
register
mail settings
Submitter phabricator
Date June 10, 2018, 10:33 a.m.
Message ID <differential-rev-PHID-DREV-jraurc7bptpzyret6beh-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/32046/
State Superseded
Headers show

Comments

phabricator - June 10, 2018, 10:33 a.m.
pulkit created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  unicode() is not available on Python 3 and throws a NameError because unicodes
  are now default str() on py3.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3708

AFFECTED FILES
  hgext/convert/common.py
  hgext/convert/convcmd.py
  hgext/convert/darcs.py
  hgext/win32mbcs.py
  mercurial/i18n.py
  mercurial/scmutil.py

CHANGE DETAILS




To: pulkit, #hg-reviewers
Cc: mercurial-devel
Yuya Nishihara - June 10, 2018, 3:01 p.m.
>   unicode() is not available on Python 3 and throws a NameError because unicodes
>   are now default str() on py3.

Appears that we need many `type(u'')`s, so I'd rather make `pycompat.unicode`
available on both Py2 and Py3. IMHO, `type(u'')` is just a temporary
workaround.
phabricator - June 10, 2018, 3:01 p.m.
yuja added a comment.


  >   unicode() is not available on Python 3 and throws a NameError because unicodes
  >   are now default str() on py3.
  
  Appears that we need many `type(u'')`s, so I'd rather make `pycompat.unicode`
  available on both Py2 and Py3. IMHO, `type(u'')` is just a temporary
  workaround.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3708

To: pulkit, #hg-reviewers
Cc: yuja, mercurial-devel

Patch

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -233,7 +233,7 @@ 
             except (AttributeError, IndexError):
                 # it might be anything, for example a string
                 reason = inst.reason
-            if isinstance(reason, unicode):
+            if isinstance(reason, type(u'')):
                 # SSLError of Python 2.7.9 contains a unicode
                 reason = encoding.unitolocal(reason)
             ui.warn(_("abort: error: %s\n") % reason)
diff --git a/mercurial/i18n.py b/mercurial/i18n.py
--- a/mercurial/i18n.py
+++ b/mercurial/i18n.py
@@ -23,11 +23,6 @@ 
 else:
     module = pycompat.fsencode(__file__)
 
-try:
-    unicode
-except NameError:
-    unicode = str
-
 _languages = None
 if (pycompat.iswindows
     and 'LANGUAGE' not in encoding.environ
@@ -76,7 +71,7 @@ 
 
     cache = _msgcache.setdefault(encoding.encoding, {})
     if message not in cache:
-        if type(message) is unicode:
+        if type(message) is type(u''):
             # goofy unicode docstrings in test
             paragraphs = message.split(u'\n\n')
         else:
diff --git a/hgext/win32mbcs.py b/hgext/win32mbcs.py
--- a/hgext/win32mbcs.py
+++ b/hgext/win32mbcs.py
@@ -90,7 +90,7 @@ 
     return arg
 
 def encode(arg):
-    if isinstance(arg, unicode):
+    if isinstance(arg, type(u'')):
         return arg.encode(_encoding)
     elif isinstance(arg, tuple):
         return tuple(map(encode, arg))
diff --git a/hgext/convert/darcs.py b/hgext/convert/darcs.py
--- a/hgext/convert/darcs.py
+++ b/hgext/convert/darcs.py
@@ -104,7 +104,7 @@ 
         shutil.rmtree(self.tmppath, ignore_errors=True)
 
     def recode(self, s, encoding=None):
-        if isinstance(s, unicode):
+        if isinstance(s, type(u'')):
             # XMLParser returns unicode objects for anything it can't
             # encode into ASCII. We convert them back to str to get
             # recode's normal conversion behavior.
diff --git a/hgext/convert/convcmd.py b/hgext/convert/convcmd.py
--- a/hgext/convert/convcmd.py
+++ b/hgext/convert/convcmd.py
@@ -55,7 +55,7 @@ 
 orig_encoding = 'ascii'
 
 def recode(s):
-    if isinstance(s, unicode):
+    if isinstance(s, type(u'')):
         return s.encode(pycompat.sysstr(orig_encoding), 'replace')
     else:
         return s.decode('utf-8').encode(
diff --git a/hgext/convert/common.py b/hgext/convert/common.py
--- a/hgext/convert/common.py
+++ b/hgext/convert/common.py
@@ -214,7 +214,7 @@ 
         if not encoding:
             encoding = self.encoding or 'utf-8'
 
-        if isinstance(s, unicode):
+        if isinstance(s, type(u'')):
             return s.encode("utf-8")
         try:
             return s.decode(pycompat.sysstr(encoding)).encode("utf-8")