[PATCH 1 of 3 V2] demandimport: define a `deactivated` context manager

Jordi GutiƩrrez Hermoso jordigh at octave.org
Thu May 28 15:44:31 CDT 2015


# HG changeset patch
# User Jordi GutiƩrrez Hermoso <jordigh at octave.org>
# Date 1432843886 14400
#      Thu May 28 16:11:26 2015 -0400
# Node ID 2727a8bbdce7d482bb3877f9d36df09483a4895b
# Parent  bcb17d7dbec25088eaec5e4d34dedbd7057c5d68
demandimport: define a `deactivated` 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,16 @@ def enable():
 def disable():
     "disable global demand-loading of modules"
     __builtin__.__import__ = _origimport
+
+ at contextmanager
+def deactivated():
+    "context manager for disabling demandimport in 'with' blocks"
+    demandenabled = isenabled()
+    if demandenabled:
+        disable()
+
+    try:
+        yield
+    finally:
+        if demandenabled:
+            enable()


More information about the Mercurial-devel mailing list