Patchwork [2,of,2] py3: make check-py3-compat.py use correct module name at loading pure modules

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Aug. 8, 2016, 5:31 p.m.
Message ID <2eb2bb13fcafc4d105ef.1470677494@juju>
Download mbox | patch
Permalink /patch/16209/
State Accepted
Headers show

Comments

Katsunori FUJIWARA - Aug. 8, 2016, 5:31 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1470677314 -32400
#      Tue Aug 09 02:28:34 2016 +0900
# Node ID 2eb2bb13fcafc4d105ef22a5cebff3871c8068dc
# Parent  28e13e7cac1c695702e160865cbc566b5a1acb70
py3: make check-py3-compat.py use correct module name at loading pure modules

Before this patch, check-py3-compat.py implies unintentional ".pure"
sub-package name at loading pure modules, because module name is
composed by just replacing "/" in the path to actual ".py" file by
".".

This makes pure modules belong to "mercurial.pure" package, and
prevents them from importing a module belonging to "mercurial" package
relatively by "from . import foo" or so.

This is reason why pure modules fail to import another module
relatively only at examination by check-py3-compat.py.
Yuya Nishihara - Aug. 9, 2016, 12:46 p.m.
On Tue, 09 Aug 2016 02:31:34 +0900, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
> # Date 1470677314 -32400
> #      Tue Aug 09 02:28:34 2016 +0900
> # Node ID 2eb2bb13fcafc4d105ef22a5cebff3871c8068dc
> # Parent  28e13e7cac1c695702e160865cbc566b5a1acb70
> py3: make check-py3-compat.py use correct module name at loading pure modules
> 
> Before this patch, check-py3-compat.py implies unintentional ".pure"
> sub-package name at loading pure modules, because module name is
> composed by just replacing "/" in the path to actual ".py" file by
> ".".
> 
> This makes pure modules belong to "mercurial.pure" package, and
> prevents them from importing a module belonging to "mercurial" package
> relatively by "from . import foo" or so.
> 
> This is reason why pure modules fail to import another module
> relatively only at examination by check-py3-compat.py.
> 
> diff --git a/contrib/check-py3-compat.py b/contrib/check-py3-compat.py
> --- a/contrib/check-py3-compat.py
> +++ b/contrib/check-py3-compat.py
> @@ -55,7 +55,7 @@ def check_compat_py3(f):
>      # out module paths for things not in a package can be confusing.
>      if f.startswith(('hgext/', 'mercurial/')) and not f.endswith('__init__.py'):
>          assert f.endswith('.py')
> -        name = f.replace('/', '.')[:-3]
> +        name = f.replace('/', '.')[:-3].replace('.pure.', '.')
>          with open(f, 'r') as fh:
>              try:
>                  imp.load_module(name, fh, '', ('py', 'r', imp.PY_SOURCE))
> 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
> @@ -136,9 +136,8 @@
>    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 module: <ImportError> cannot import name 'policy' (line *) (glob)
> -  mercurial/pure/osutil.py: error importing module: <ImportError> cannot import name 'policy' (line *) (glob)
> -  mercurial/pure/parsers.py: error importing module: <ImportError> No module named 'mercurial.pure.node' (line *) (glob)
> +  mercurial/pure/mpatch.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:48)
> +  mercurial/pure/parsers.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:48)

It appears we should glob out line numbers. Fixed in flight.

Patch

diff --git a/contrib/check-py3-compat.py b/contrib/check-py3-compat.py
--- a/contrib/check-py3-compat.py
+++ b/contrib/check-py3-compat.py
@@ -55,7 +55,7 @@  def check_compat_py3(f):
     # out module paths for things not in a package can be confusing.
     if f.startswith(('hgext/', 'mercurial/')) and not f.endswith('__init__.py'):
         assert f.endswith('.py')
-        name = f.replace('/', '.')[:-3]
+        name = f.replace('/', '.')[:-3].replace('.pure.', '.')
         with open(f, 'r') as fh:
             try:
                 imp.load_module(name, fh, '', ('py', 'r', imp.PY_SOURCE))
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
@@ -136,9 +136,8 @@ 
   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 module: <ImportError> cannot import name 'policy' (line *) (glob)
-  mercurial/pure/osutil.py: error importing module: <ImportError> cannot import name 'policy' (line *) (glob)
-  mercurial/pure/parsers.py: error importing module: <ImportError> No module named 'mercurial.pure.node' (line *) (glob)
+  mercurial/pure/mpatch.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:48)
+  mercurial/pure/parsers.py: error importing: <TypeError> getattr(): attribute name must be string (error at pycompat.py:48)
   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/registrar.py: error importing: <TypeError> getattr(): attribute name must be string (error at util.py:*) (glob)