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

login
register
mail settings
Submitter timeless
Date Sept. 21, 2016, 7:09 p.m.
Message ID <0070696439eab002f6dd.1474484974@gcc2-power8.osuosl.org>
Download mbox | patch
Permalink /patch/16741/
State Superseded
Headers show

Comments

timeless - Sept. 21, 2016, 7:09 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1474429683 0
#      Wed Sep 21 03:48:03 2016 +0000
# Node ID 0070696439eab002f6dd32bcb40eb671daff800a
# Parent  adb54fd7d90f0ca607432ed7ae884da55ec427de
# Available At https://bitbucket.org/timeless/mercurial-crew
#              hg pull https://bitbucket.org/timeless/mercurial-crew -r 0070696439ea
demandimport: reject contextlib._GeneratorContextManager on Py < 3.2

decorator expects:
 from contextlib import _GeneratorContextManager
to throw an exception on python < 3.2 (issue5373).

We tell demandimport to throw it.
Siddharth Agarwal - Sept. 21, 2016, 7:23 p.m.
On 9/21/16 12:09, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1474429683 0
> #      Wed Sep 21 03:48:03 2016 +0000
> # Node ID 0070696439eab002f6dd32bcb40eb671daff800a
> # Parent  adb54fd7d90f0ca607432ed7ae884da55ec427de
> # Available At https://bitbucket.org/timeless/mercurial-crew
> #              hg pull https://bitbucket.org/timeless/mercurial-crew -r 0070696439ea
> demandimport: reject contextlib._GeneratorContextManager on Py < 3.2

As a general thing I don't think we should be spending time trying to 
fix this implementation of demandimport on Python 3 at all. I would much 
rather we use an implementation based on the new importlib stuff, like 
the one I sent to the list a while back. (Hopefully I can get it in 
during the sprint.)

- Siddharth

>
> decorator expects:
>   from contextlib import _GeneratorContextManager
> to throw an exception on python < 3.2 (issue5373).
>
> We tell demandimport to throw it.
>
> diff -r adb54fd7d90f -r 0070696439ea mercurial/demandimport.py
> --- a/mercurial/demandimport.py	Wed Sep 21 18:58:54 2016 +0000
> +++ b/mercurial/demandimport.py	Wed Sep 21 03:48:03 2016 +0000
> @@ -313,6 +313,12 @@
>   if os.name != 'nt':
>       reject('distutils', 'msvc9compiler', ImportError, 'No module named _winreg')
>   
> +# 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 adb54fd7d90f -r 0070696439ea tests/test-demandimport.py
> --- a/tests/test-demandimport.py	Wed Sep 21 18:58:54 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!
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
timeless - Sept. 21, 2016, 7:47 p.m.
Siddharth Agarwal wrote:
> As a general thing I don't think we should be spending time trying to fix
> this implementation of demandimport on Python 3 at all.

This isn't really about python3, it's just about foreign code that
expects a certain object to look a certain way, but because of
demandimport it gets a demandimport object stub instead of an
exception -- in python3 it would have at least gotten a stub for a
real object. -- I don't know if the code works in py3 but that's a
different adventure, and I'm happy to let a python3 demandimport
solution worry about it.
Yuya Nishihara - Sept. 26, 2016, 2:09 p.m.
On Wed, 21 Sep 2016 19:09:34 +0000, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1474429683 0
> #      Wed Sep 21 03:48:03 2016 +0000
> # Node ID 0070696439eab002f6dd32bcb40eb671daff800a
> # Parent  adb54fd7d90f0ca607432ed7ae884da55ec427de
> # Available At https://bitbucket.org/timeless/mercurial-crew
> #              hg pull https://bitbucket.org/timeless/mercurial-crew -r 0070696439ea
> demandimport: reject contextlib._GeneratorContextManager on Py < 3.2
> 
> decorator expects:
>  from contextlib import _GeneratorContextManager
> to throw an exception on python < 3.2 (issue5373).
> 
> We tell demandimport to throw it.
> 
> diff -r adb54fd7d90f -r 0070696439ea mercurial/demandimport.py
> --- a/mercurial/demandimport.py	Wed Sep 21 18:58:54 2016 +0000
> +++ b/mercurial/demandimport.py	Wed Sep 21 03:48:03 2016 +0000
> @@ -313,6 +313,12 @@
>  if os.name != 'nt':
>      reject('distutils', 'msvc9compiler', ImportError, 'No module named _winreg')
>  
> +# 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')

I'm not a fan of duplicating knowledge about module internals unless there's
a measurable win. We could break the current "ignore" list down to fine-tuned
"rejects" rules, but that would end with bloated set of rules we wouldn't
want to maintain.

Patch

diff -r adb54fd7d90f -r 0070696439ea mercurial/demandimport.py
--- a/mercurial/demandimport.py	Wed Sep 21 18:58:54 2016 +0000
+++ b/mercurial/demandimport.py	Wed Sep 21 03:48:03 2016 +0000
@@ -313,6 +313,12 @@ 
 if os.name != 'nt':
     reject('distutils', 'msvc9compiler', ImportError, 'No module named _winreg')
 
+# 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 adb54fd7d90f -r 0070696439ea tests/test-demandimport.py
--- a/tests/test-demandimport.py	Wed Sep 21 18:58:54 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!