Patchwork [11,of,11,py3] extensions: fix extension module hunting on Python 3

login
register
mail settings
Submitter Augie Fackler
Date Oct. 9, 2016, 2:16 p.m.
Message ID <e6a69e778cb02cada233.1476022613@augie-macbookair2.roam.corp.google.com>
Download mbox | patch
Permalink /patch/17007/
State Accepted
Headers show

Comments

Augie Fackler - Oct. 9, 2016, 2:16 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1476019688 14400
#      Sun Oct 09 09:28:08 2016 -0400
# Node ID e6a69e778cb02cada23352df512b8580026640d9
# Parent  e89f400277d564c4a576ad8e4a6003a201993639
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?
Martijn Pieters - Oct. 9, 2016, 2:32 p.m.
On 9 October 2016 at 16:16, Augie Fackler <raf@durin42.com> wrote:

> # HG changeset patch
> # User Augie Fackler <augie@google.com>
> # Date 1476019688 14400
> #      Sun Oct 09 09:28:08 2016 -0400
> # Node ID e6a69e778cb02cada23352df512b8580026640d9
> # Parent  e89f400277d564c4a576ad8e4a6003a201993639
> 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.
>

os.fsdecode exists only in Python 3, I'm afraid.


> 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)
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>

Patch

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)