Mercurial > public > mercurial-scm > hg-stable
diff mercurial/demandimport.py @ 25327:2e7804110b14
demandimport: define a `deactivated` context manager
This can be useful for use in "with" blocks for temporarily disabling
demandimport.
author | Jordi Guti?rrez Hermoso <jordigh@octave.org> |
---|---|
date | Thu, 28 May 2015 16:11:26 -0400 |
parents | 2205d00b6d2b |
children | fa1f04529775 |
line wrap: on
line diff
--- 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()