# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1432843886 14400 # Node ID 2e7804110b14f5695893cb8634223632a5f598bf # Parent 238e5cd94bbcf1678e91060e64fb2819c7694e92 demandimport: define a `deactivated` context manager This can be useful for use in "with" blocks for temporarily disabling demandimport. diff -r 238e5cd94bbc -r 2e7804110b14 mercurial/demandimport.py --- a/mercurial/demandimport.py Thu May 28 14:14:11 2015 -0400 +++ b/mercurial/demandimport.py Thu May 28 16:11:26 2015 -0400 @@ -25,6 +25,8 @@ ''' import __builtin__, os, sys +from contextlib import contextmanager + _origimport = __import__ nothing = object() @@ -179,3 +181,16 @@ def disable(): "disable global demand-loading of modules" __builtin__.__import__ = _origimport + +@contextmanager +def deactivated(): + "context manager for disabling demandimport in 'with' blocks" + demandenabled = isenabled() + if demandenabled: + disable() + + try: + yield + finally: + if demandenabled: + enable()