Comments
Patch
@@ -222,9 +222,18 @@
def processfromlist(mod, name, fromlist):
# call processfromitem for each item in fromlist
- if True:
+ # if a module property is registered for rejection,
+ # raise the specified exception
+ 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:
@@ -290,6 +299,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
@@ -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!