Here's a patch I wrote some time ago that checks for the presence of the module
at import time. It incurs a small performance cost, but should preserve most of
the benefits of demandimport. I think it solves your particular bug, and would
also allow most if not all of the blacklist to be removed.
# HG changeset patch
# User Brendan Cully <brendan@kublai.com>
# Date 1183049664 25200
# Node ID 1a0a31d44b87c4ce2a36305b1c53af87238f51d7
# Parent 2ececafa58596042edcaeb93f0a35b9d3daa36ab
demandimport: raise ImportError immediately for missing modules
diff --git a/mercurial/demandimport.py b/mercurial/demandimport.py
--- a/mercurial/demandimport.py
+++ b/mercurial/demandimport.py
@@ -25,6 +25,9 @@ These imports will not be delayed:
'''
_origimport = __import__
+import pdb
+import imp
+import os.path
class _demandmod(object):
"""module demand-loader and proxy"""
@@ -35,6 +38,12 @@ class _demandmod(object):
else:
head = name
after = []
+ try:
+ f = globals.get('__file__')
+ path = f and os.path.dirname(f)
+ imp.find_module(head, [path])
+ except ImportError:
+ imp.find_module(head)
object.__setattr__(self, "_data", (head, globals, locals, after))
object.__setattr__(self, "_module", None)
def _extend(self, name):
@@ -78,7 +87,7 @@ class _demandmod(object):
setattr(self._module, attr, val)
def _demandimport(name, globals=None, locals=None, fromlist=None):
- if not locals or name in ignore or fromlist == ('*',):
+ if not locals or name in ignore or (fromlist and '*' in fromlist):
# these cases we can't really delay
return _origimport(name, globals, locals, fromlist)
elif not fromlist: |