[PATCH 5 of 5 py3] extensions: fix extension module hunting on Python 3

Augie Fackler raf at durin42.com
Thu Nov 10 17:10:43 EST 2016


# HG changeset patch
# User Augie Fackler <augie at google.com>
# Date 1476019688 14400
#      Sun Oct 09 09:28:08 2016 -0400
# Node ID bfc0c8c65e9cfec70e0e68e50fb4d1e7c6398a57
# Parent  61d6c1706d3246ea08a4176a5b3d286f498b8054
extensions: fix extension module hunting on Python 3

importlib is a firm believer that paths are really strings, not
bytes. Fortunately we have os.fsdecode to help us with that.

This also makes a few strings r-strings to make them bytes on Py2 and
str on py3. That might be too clever?

diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -18,6 +18,7 @@ from .i18n import (
 from . import (
     cmdutil,
     error,
+    pycompat,
     util,
 )
 
@@ -57,8 +58,9 @@ def find(name):
     return mod
 
 def loadpath(path, module_name):
-    module_name = module_name.replace('.', '_')
+    module_name = module_name.replace(r'.', r'_')
     path = util.normpath(util.expandpath(path))
+    path = os.fsdecode(path)
     if os.path.isdir(path):
         # module/__init__.py style
         d, f = os.path.split(path)
@@ -85,7 +87,7 @@ def _importext(name, path=None, reportfu
         # the module will be loaded in sys.modules
         # choose an unique name so that it doesn't
         # conflicts with other modules
-        mod = loadpath(path, 'hgext.%s' % name)
+        mod = loadpath(path, pycompat.sysstr('hgext.%s' % name))
     else:
         try:
             mod = _importh("hgext.%s" % name)


More information about the Mercurial-devel mailing list