Patchwork [4,of,6,v4] debuginstall: expose modulepolicy

login
register
mail settings
Submitter Yuya Nishihara
Date May 18, 2016, 11:49 a.m.
Message ID <20160518204948.6c2d1902@mimosa>
Download mbox | patch
Permalink /patch/15162/
State Superseded
Commit b3a677c82a35e499e84f69245f65e682c4be698f
Headers show

Comments

Yuya Nishihara - May 18, 2016, 11:49 a.m.
On Wed, 11 May 2016 17:18:14 +0000, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1457553345 0
> #      Wed Mar 09 19:55:45 2016 +0000
> # Node ID 65825180761f1e2bacbe8def2e0d502bf17b5e07
> # Parent  f08636bb41265409e5fbdc7376cd4032d9a680a1
> # EXP-Topic runtests
> # Available At bb://timeless/mercurial-crew
> #              hg pull bb://timeless/mercurial-crew -r 65825180761f
> debuginstall: expose modulepolicy
> 
> With this, you can check for pure easily:
> $ HGMODULEPOLICY=py ./hg debuginstall -T "{hgmodulepolicy}"
> py
> 
> diff -r f08636bb4126 -r 65825180761f mercurial/__init__.py
> --- a/mercurial/__init__.py	Wed May 11 15:20:25 2016 +0000
> +++ b/mercurial/__init__.py	Wed Mar 09 19:55:45 2016 +0000
> @@ -12,36 +12,13 @@
>  import sys
>  import zipimport
>  
> +from . import (
> +    policy
> +)
> +
>  __all__ = []
>  
> -# Rules for how modules can be loaded. Values are:
> -#
> -#    c - require C extensions
> -#    allow - allow pure Python implementation when C loading fails
> -#    py - only load pure Python modules
> -#
> -# By default, require the C extensions for performance reasons.
> -modulepolicy = 'c'
> -try:
> -    from . import __modulepolicy__
> -    modulepolicy = __modulepolicy__.modulepolicy
> -except ImportError:
> -    pass
> -
> -# PyPy doesn't load C extensions.
> -#
> -# The canonical way to do this is to test platform.python_implementation().
> -# But we don't import platform and don't bloat for it here.
> -if '__pypy__' in sys.builtin_module_names:
> -    modulepolicy = 'py'
> -
> -# Our C extensions aren't yet compatible with Python 3. So use pure Python
> -# on Python 3 for now.
> -if sys.version_info[0] >= 3:
> -    modulepolicy = 'py'
> -
> -# Environment variable can always force settings.
> -modulepolicy = os.environ.get('HGMODULEPOLICY', modulepolicy)
> +modulepolicy = policy.policy
>  
>  # Modules that have both Python and C implementations. See also the
>  # set of .py files under mercurial/pure/.
> diff -r f08636bb4126 -r 65825180761f mercurial/commands.py
> --- a/mercurial/commands.py	Wed May 11 15:20:25 2016 +0000
> +++ b/mercurial/commands.py	Wed Mar 09 19:55:45 2016 +0000
> @@ -59,6 +59,7 @@
>      obsolete,
>      patch,
>      phases,
> +    policy,
>      pvec,
>      repair,
>      revlog,
> @@ -2741,6 +2742,8 @@
>               os.path.dirname(os.__file__))
>  
>      # compiled modules
> +    fm.write('hgmodulepolicy', _("checking module policy (%s)\n"),
> +             policy.policy)
>      fm.write('hgmodules', _("checking installed modules (%s)...\n"),
>               os.path.dirname(__file__))

I don't know if it is better idea than making policy module, but we can add
a utility function to avoid "from . import modulepolicy" in commands.py.
timeless - May 18, 2016, 6 p.m.
Yuya Nishihara wrote:
> I don't know if it is better idea than making policy module, but we can add
> a utility function to avoid "from . import modulepolicy" in commands.py.
>
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -357,6 +357,10 @@ def popen4(cmd, env=None, newlines=False
>                           env=env)
>      return p.stdin, p.stdout, p.stderr, p
>
> +def modulepolicy():
> +    from . import modulepolicy
> +    return modulepolicy

Personally, I'd like this. I did it the hard way because iirc augie
objected to that style saying it's poor form.

I don't really care which way we do it. I just need the ability to use
hg debuginstall so that ./run-tests.py --with-hg works properly. The
sooner the better.
Augie Fackler - May 18, 2016, 6:13 p.m.
> On May 18, 2016, at 14:00, timeless <timeless@gmail.com> wrote:
> 
>> +def modulepolicy():
>> +    from . import modulepolicy
>> +    return modulepolicy
> 
> Personally, I'd like this. I did it the hard way because iirc augie
> objected to that style saying it's poor form.

I'm generally opposed to imports hidden inside functions because it's a great way to hide circular dependencies.
Yuya Nishihara - May 19, 2016, 2:49 p.m.
On Wed, 18 May 2016 14:13:07 -0400, Augie Fackler wrote:
> > On May 18, 2016, at 14:00, timeless <timeless@gmail.com> wrote:
> >   
> >> +def modulepolicy():
> >> +    from . import modulepolicy
> >> +    return modulepolicy  
> > 
> > Personally, I'd like this. I did it the hard way because iirc augie
> > objected to that style saying it's poor form.  
> 
> I'm generally opposed to imports hidden inside functions because it's a great
> way to hide circular dependencies.

Since there isn't circular dependencies, we could move the import outside of
the function. But we'll need a way to silence the import checker.

Patch

--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -357,6 +357,10 @@  def popen4(cmd, env=None, newlines=False
                          env=env)
     return p.stdin, p.stdout, p.stderr, p
 
+def modulepolicy():
+    from . import modulepolicy
+    return modulepolicy
+
 def version():
     """Return version information if available."""
     try: