[PATCH 1 of 3] demandimport: define an importnow context manager

Pierre-Yves David pierre-yves.david at ens-lyon.org
Wed May 27 13:42:05 CDT 2015



On 05/27/2015 11:39 AM, Jordi Gutiérrez Hermoso wrote:
> On Tue, 2015-05-26 at 13:45 -0700, Pierre-Yves David wrote:
>>
>> On 05/26/2015 01:35 PM, Jordi Gutiérrez Hermoso wrote:
>>> # HG changeset patch
>>> # User Jordi Gutiérrez Hermoso <jordigh at octave.org>
>>> # Date 1432671771 14400
>>> #      Tue May 26 16:22:51 2015 -0400
>>> # Node ID ac165f67eab4ab8157f73ab229e80883d49fabe0
>>> # Parent  6ac860f700b5cfeda232d5305963047696b869ca
>>> demandimport: define an importnow context manager
>>>
>>> This can be useful for use in "with" blocks for temporarily disabling
>>> demandimport.
>>>
>>> diff --git a/mercurial/demandimport.py b/mercurial/demandimport.py
>>> --- a/mercurial/demandimport.py
>>> +++ b/mercurial/demandimport.py
>>> @@ -25,6 +25,8 @@ These imports will not be delayed:
>>>    '''
>>>
>>>    import __builtin__, os, sys
>>> +from contextlib import contextmanager
>>> +
>>>    _origimport = __import__
>>>
>>>    nothing = object()
>>> @@ -179,3 +181,14 @@ def enable():
>>>    def disable():
>>>        "disable global demand-loading of modules"
>>>        __builtin__.__import__ = _origimport
>>> +
>>> + at contextmanager
>>> +def importnow():
>>
>> what about: nodemandimport?
>
> My only concern with that is that then it would read
>
>      with demandimport.nodemandimport():
>
> which akward to pash in English. If I'm going to be doing a negative,
> I really wish I could write,
>
>      without demandimport():
>
> but of course this isn't valid Python.
>
> I think I prefer Augie's recommendation to do
>
>     with demandimport.immediate():

Some black magic idea:

with demandimport.disable():

disable() both disable debug and return a context manager object able to 
restore previous value on __exit__


-- 
Pierre-Yves David


More information about the Mercurial-devel mailing list