[PATCH 3 of 6] encoding: use unicode literals to appease Python 3

Gregory Szorc gregory.szorc at gmail.com
Wed Jun 1 01:53:00 EDT 2016


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1464759028 25200
#      Tue May 31 22:30:28 2016 -0700
# Node ID fae2a2edc2a1e9d507f1c882c58e4d30962815a2
# Parent  b062ba4aeefeb21ab1d5ea7e304fb81c866587da
encoding: use unicode literals to appease Python 3

Various places in encoding.py related to encoding names and
environment variables were expecting unicode/str types, not
bytes. This was causing various imports in Python 3 to fail
with our new module loader.

Add some u'' prefixes to prevent these literals from getting
b'' prefixed by the loader so Python 3 imports don't fail
as fast.

diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -65,30 +65,30 @@ def _getpreferredencoding():
     oldloc = locale.setlocale(locale.LC_CTYPE)
     locale.setlocale(locale.LC_CTYPE, "")
     result = locale.nl_langinfo(locale.CODESET)
     locale.setlocale(locale.LC_CTYPE, oldloc)
 
     return result
 
 _encodingfixers = {
-    '646': lambda: 'ascii',
-    'ANSI_X3.4-1968': lambda: 'ascii',
-    'mac-roman': _getpreferredencoding
+    u'646': lambda: u'ascii',
+    u'ANSI_X3.4-1968': lambda: u'ascii',
+    u'mac-roman': _getpreferredencoding
 }
 
 try:
-    encoding = os.environ.get("HGENCODING")
+    encoding = os.environ.get(u"HGENCODING")
     if not encoding:
-        encoding = locale.getpreferredencoding() or 'ascii'
+        encoding = locale.getpreferredencoding() or u'ascii'
         encoding = _encodingfixers.get(encoding, lambda: encoding)()
 except locale.Error:
-    encoding = 'ascii'
-encodingmode = os.environ.get("HGENCODINGMODE", "strict")
-fallbackencoding = 'ISO-8859-1'
+    encoding = u'ascii'
+encodingmode = os.environ.get(u"HGENCODINGMODE", u"strict")
+fallbackencoding = u'ISO-8859-1'
 
 class localstr(str):
     '''This class allows strings that are unmodified to be
     round-tripped to the local encoding and back'''
     def __new__(cls, u, l):
         s = str.__new__(cls, l)
         s._utf8 = u
         return s
@@ -175,18 +175,18 @@ def fromlocal(s):
         return s.decode(encoding, encodingmode).encode("utf-8")
     except UnicodeDecodeError as inst:
         sub = s[max(0, inst.start - 10):inst.start + 10]
         raise error.Abort("decoding near '%s': %s!" % (sub, inst))
     except LookupError as k:
         raise error.Abort(k, hint="please check your locale settings")
 
 # How to treat ambiguous-width characters. Set to 'wide' to treat as wide.
-wide = (os.environ.get("HGENCODINGAMBIGUOUS", "narrow") == "wide"
-        and "WFA" or "WF")
+wide = (os.environ.get(u"HGENCODINGAMBIGUOUS", u"narrow") == u"wide"
+        and u"WFA" or u"WF")
 
 def colwidth(s):
     "Find the column width of a string for display in the local encoding"
     return ucolwidth(s.decode(encoding, 'replace'))
 
 def ucolwidth(d):
     "Find the column width of a Unicode string for display"
     eaw = getattr(unicodedata, 'east_asian_width', None)
diff --git a/tests/test-check-py3-compat.t b/tests/test-check-py3-compat.t
--- a/tests/test-check-py3-compat.t
+++ b/tests/test-check-py3-compat.t
@@ -30,98 +30,98 @@
   tests/md5sum.py not using absolute_import
   tests/readlink.py not using absolute_import
   tests/run-tests.py not using absolute_import
   tests/test-demandimport.py not using absolute_import
 
 #if py3exe
   $ hg files 'set:(**.py)' | sed 's|\\|/|g' | xargs $PYTHON3 contrib/check-py3-compat.py
   doc/hgmanpage.py: invalid syntax: invalid syntax (<unknown>, line *) (glob)
-  hgext/acl.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/automv.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/blackbox.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
+  hgext/acl.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/automv.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/blackbox.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
   hgext/bugzilla.py: error importing module: <ImportError> No module named 'urlparse' (line 284)
-  hgext/censor.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
+  hgext/censor.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
   hgext/chgserver.py: error importing module: <ImportError> No module named 'SocketServer' (line 43)
-  hgext/children.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/churn.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/clonebundles.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
+  hgext/children.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/churn.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/clonebundles.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
   hgext/color.py: invalid syntax: invalid syntax (<unknown>, line *) (glob)
-  hgext/convert/bzr.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
+  hgext/convert/bzr.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
   hgext/convert/common.py: error importing module: <ImportError> No module named 'cPickle' (line 10)
-  hgext/convert/convcmd.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/convert/cvs.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
+  hgext/convert/convcmd.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/convert/cvs.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
   hgext/convert/cvsps.py: error importing module: <ImportError> No module named 'cPickle' (line 9)
-  hgext/convert/darcs.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/convert/filemap.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/convert/git.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/convert/gnuarch.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/convert/hg.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/convert/monotone.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/convert/p4.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
+  hgext/convert/darcs.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/convert/filemap.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/convert/git.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/convert/gnuarch.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/convert/hg.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/convert/monotone.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/convert/p4.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
   hgext/convert/subversion.py: error importing module: <ImportError> No module named 'cPickle' (line 6)
   hgext/convert/transport.py: error importing module: <ImportError> No module named 'svn.client' (line 21)
-  hgext/eol.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/extdiff.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/factotum.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/fetch.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/fsmonitor/state.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/fsmonitor/watchmanclient.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/gpg.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/graphlog.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/hgk.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/highlight/highlight.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/histedit.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/keyword.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/largefiles/basestore.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/largefiles/lfcommands.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/largefiles/lfutil.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/largefiles/localstore.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/largefiles/overrides.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
+  hgext/eol.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/extdiff.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/factotum.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/fetch.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/fsmonitor/state.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/fsmonitor/watchmanclient.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/gpg.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/graphlog.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/hgk.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/highlight/highlight.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/histedit.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/keyword.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/largefiles/basestore.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/largefiles/lfcommands.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/largefiles/lfutil.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/largefiles/localstore.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/largefiles/overrides.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
   hgext/largefiles/proto.py: error importing: <ImportError> No module named 'httplib' (error at httppeer.py:*) (glob)
-  hgext/largefiles/remotestore.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/largefiles/reposetup.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
+  hgext/largefiles/remotestore.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/largefiles/reposetup.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
   hgext/largefiles/uisetup.py: error importing module: <SyntaxError> invalid syntax (archival.py, line *) (line *) (glob)
   hgext/largefiles/wirestore.py: error importing module: <ImportError> No module named 'lfutil' (line 8)
-  hgext/mq.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/notify.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/pager.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/patchbomb.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/purge.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/rebase.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/record.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/relink.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/schemes.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/share.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/shelve.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/strip.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/transplant.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/win32mbcs.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  hgext/win32text.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
+  hgext/mq.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/notify.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/pager.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/patchbomb.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/purge.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/rebase.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/record.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/relink.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/schemes.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/share.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/shelve.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/strip.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/transplant.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/win32mbcs.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  hgext/win32text.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
   mercurial/archival.py: invalid syntax: invalid syntax (<unknown>, line *) (glob)
-  mercurial/bookmarks.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/branchmap.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
+  mercurial/bookmarks.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/branchmap.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
   mercurial/bundle*.py: invalid syntax: invalid syntax (<unknown>, line *) (glob)
-  mercurial/bundlerepo.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/byterange.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/changegroup.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/changelog.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/cmdutil.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
+  mercurial/bundlerepo.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/byterange.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/changegroup.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/changelog.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/cmdutil.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
   mercurial/commands.py: invalid syntax: invalid syntax (<unknown>, line *) (glob)
   mercurial/commandserver.py: error importing module: <ImportError> No module named 'SocketServer' (line 10)
-  mercurial/config.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/context.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/copies.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/crecord.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/dagparser.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/dagutil.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/destutil.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/dirstate.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/discovery.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
-  mercurial/dispatch.py: error importing: <TypeError> str expected, not bytes (error at encoding.py:*) (glob)
+  mercurial/config.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/context.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/copies.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/crecord.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/dagparser.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/dagutil.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/destutil.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/dirstate.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/discovery.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
+  mercurial/dispatch.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:18)
   mercurial/exchange.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:*) (glob)
   mercurial/extensions.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:*) (glob)
   mercurial/fancyopts.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:*) (glob)
   mercurial/filelog.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:*) (glob)
   mercurial/filemerge.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:*) (glob)
   mercurial/fileset.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:*) (glob)
   mercurial/formatter.py: error importing module: <ImportError> No module named 'cPickle' (line 10)
   mercurial/graphmod.py: error importing: <TypeError> getattr(): attribute name must be string (error at i18n.py:*) (glob)


More information about the Mercurial-devel mailing list