[PATCH 4 of 5] demandimport: suppport rejecting modules

timeless timeless at fmr.im
Wed Sep 21 03:59:34 UTC 2016


# HG changeset patch
# User timeless <timeless at mozdev.org>
# Date 1474429655 0
#      Wed Sep 21 03:47:35 2016 +0000
# Node ID 894cc47eb82a85d167f5717c9fe0a31392e5bb97
# Parent  d750b7a37b860dedeea32f803c03f81ff005fed4
# Available At https://bitbucket.org/timeless/mercurial-crew
#              hg pull https://bitbucket.org/timeless/mercurial-crew -r 894cc47eb82a
demandimport: suppport rejecting modules

diff -r d750b7a37b86 -r 894cc47eb82a mercurial/demandimport.py
--- a/mercurial/demandimport.py	Wed Sep 21 03:40:51 2016 +0000
+++ b/mercurial/demandimport.py	Wed Sep 21 03:47:35 2016 +0000
@@ -221,9 +221,17 @@
             return mod
 
         def processfromlist(mod, name, fromlist):
-            if True:
+            if not name in rejects:
                 for x in fromlist:
                     processfromitem(mod, x)
+            else:
+                reject = rejects[name]
+                for x in fromlist:
+                    if x in reject:
+                        cls, msg = reject[x]
+                        raise cls(msg)
+                    processfromitem(mod, x)
+
         if level >= 0:
             if name:
                 # "from a import b" or "from .a import b" style
@@ -288,6 +296,16 @@
     'distutils.msvc9compiler',
     ]
 
+rejects = {}
+
+def reject(mod, prop, cls, msg):
+    """inform demandimport that a module does not have a property
+
+    arguments are the class and message to raise when code tries to
+    import it."""
+    if not mod in rejects:
+        rejects[mod] = {}
+    rejects[mod][prop] = [cls, msg]
 def isenabled():
     return builtins.__import__ == _demandimport
 
diff -r d750b7a37b86 -r 894cc47eb82a tests/test-demandimport.py
--- a/tests/test-demandimport.py	Wed Sep 21 03:40:51 2016 +0000
+++ b/tests/test-demandimport.py	Wed Sep 21 03:47:35 2016 +0000
@@ -63,6 +63,14 @@
 print("re.stderr =", f(re.stderr))
 print("re =", f(re))
 
+demandimport.reject('chunk', 'Chunk', ImportError, 'boo')
+try:
+    from chunk import Chunk
+    print('reject should prevent chunk from loading for Chunk')
+    Chunk.getname
+except ImportError:
+    pass
+
 demandimport.disable()
 os.environ['HGDEMANDIMPORT'] = 'disable'
 # this enable call should not actually enable demandimport!



More information about the Mercurial-devel mailing list