Patchwork [5,of,5] demandimport: reject contextlib._GeneratorContextManager on Py < 3.2

login
register
mail settings
Submitter timeless
Date Sept. 21, 2016, 3:59 a.m.
Message ID <e07c3d398573f74a2e38.1474430375@gcc2-power8.osuosl.org>
Download mbox | patch
Permalink /patch/16727/
State Accepted
Headers show

Comments

timeless - Sept. 21, 2016, 3:59 a.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1474429683 0
#      Wed Sep 21 03:48:03 2016 +0000
# Node ID e07c3d398573f74a2e382deb06462bdc15ed437f
# Parent  894cc47eb82a85d167f5717c9fe0a31392e5bb97
# Available At https://bitbucket.org/timeless/mercurial-crew
#              hg pull https://bitbucket.org/timeless/mercurial-crew -r e07c3d398573
demandimport: reject contextlib._GeneratorContextManager on Py < 3.2

issue5373
Yuya Nishihara - Sept. 21, 2016, 3 p.m.
On Wed, 21 Sep 2016 03:59:35 +0000, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1474429683 0
> #      Wed Sep 21 03:48:03 2016 +0000
> # Node ID e07c3d398573f74a2e382deb06462bdc15ed437f
> # Parent  894cc47eb82a85d167f5717c9fe0a31392e5bb97
> # Available At https://bitbucket.org/timeless/mercurial-crew
> #              hg pull https://bitbucket.org/timeless/mercurial-crew -r e07c3d398573
> demandimport: reject contextlib._GeneratorContextManager on Py < 3.2
> 
> issue5373
> 
> diff -r 894cc47eb82a -r e07c3d398573 mercurial/demandimport.py
> --- a/mercurial/demandimport.py	Wed Sep 21 03:47:35 2016 +0000
> +++ b/mercurial/demandimport.py	Wed Sep 21 03:48:03 2016 +0000
> @@ -306,6 +306,13 @@
>      if not mod in rejects:
>          rejects[mod] = {}
>      rejects[mod][prop] = [cls, msg]
> +
> +# decorator imported by ipython from pygments does an import which isn't
> +# friendly to demandimport.
> +if sys.version_info[0] < 3 or sys.version_info[1] < 2:
> +    reject('contextlib', '_GeneratorContextManager',
> +           ImportError, 'cannot import name _GeneratorContextManager')

Can't we simply ignore 'contextlib' ?

Since contextlib is imported by demandimport.py, there's no benefit to delay
importing contextlib.
timeless - Sept. 21, 2016, 6:47 p.m.
Yuya Nishihara wrote:
> Can't we simply ignore 'contextlib' ?

I had a patch that did that. It felt gross (it clearly wasn't very elegant)

> Since contextlib is imported by demandimport.py, there's no benefit to delay
> importing contextlib.

So, demandimport could just return a given module if it's already
loaded w/o wrapping things,
but it's possible for a module to be loaded w/o its submodules being loaded.

We don't want to automatically load all submodules (that would more or
less bypass demandimport, and thus defeat its purpose).

I think that other modules have similarly unfriendly code, and that it
thus makes sense to offer this feature.

After looking through our list of ignores, I think that it should be
possible to make distutils.msvc9compiler work with my general approach
(although the specific implementation I have in v1 and v2 doesn't
work, so I'll need a v3).

Patch

diff -r 894cc47eb82a -r e07c3d398573 mercurial/demandimport.py
--- a/mercurial/demandimport.py	Wed Sep 21 03:47:35 2016 +0000
+++ b/mercurial/demandimport.py	Wed Sep 21 03:48:03 2016 +0000
@@ -306,6 +306,13 @@ 
     if not mod in rejects:
         rejects[mod] = {}
     rejects[mod][prop] = [cls, msg]
+
+# decorator imported by ipython from pygments does an import which isn't
+# friendly to demandimport.
+if sys.version_info[0] < 3 or sys.version_info[1] < 2:
+    reject('contextlib', '_GeneratorContextManager',
+           ImportError, 'cannot import name _GeneratorContextManager')
+
 def isenabled():
     return builtins.__import__ == _demandimport
 
diff -r 894cc47eb82a -r e07c3d398573 tests/test-demandimport.py
--- a/tests/test-demandimport.py	Wed Sep 21 03:47:35 2016 +0000
+++ b/tests/test-demandimport.py	Wed Sep 21 03:48:03 2016 +0000
@@ -71,6 +71,15 @@ 
 except ImportError:
     pass
 
+if sys.version_info[0] < 3 or sys.version_info[1] < 2:
+    try:
+        from contextlib import _GeneratorContextManager
+        print('contextlib._GeneratorContextManager needs to be an '
+              'immediate importerror on python <3.2')
+        _GeneratorContextManager.__doc__
+    except ImportError:
+        pass
+
 demandimport.disable()
 os.environ['HGDEMANDIMPORT'] = 'disable'
 # this enable call should not actually enable demandimport!