[PATCH] dispatch: also pass level argument to __import__ for ignored modules

Dan Villiom Podlaski Christiansen danchr at gmail.com
Wed Jul 29 13:54:17 CDT 2009

# HG changeset patch
# User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
# Date 1248798288 -7200
# Node ID c0c0327fc0224c29b39d89a6b490a32f3d9f80a4
# Parent  b3e6607ad3baf41b047518f3e5a28bffed84210a
dispatch: also pass level argument to __import__ for ignored modules.

I wanted to check if mercurial.demandimport could speed up the loading
of PyObjC, and ran into this: the level argument for __import__,
available in Python 2.5 and later, is silently dropped when doing an
'import *'. I have no idea what these arguments mean, but this minor
change made demandimport work with PyObjC.

(Oh, and because of that 'from ... import *', PyObjC still took about
2 seconds to load...)

diff --git a/mercurial/demandimport.py b/mercurial/demandimport.py
--- a/mercurial/demandimport.py
+++ b/mercurial/demandimport.py
@@ -28,6 +28,8 @@ import __builtin__
 _origimport = __import__
 class _demandmod(object):
+    __slots__ = ('_data', '_module')
     """module demand-loader and proxy"""
     def __init__(self, name, globals, locals):
         if '.' in name:
@@ -81,7 +83,10 @@ class _demandmod(object):
 def _demandimport(name, globals=None, locals=None, fromlist=None, level=None):
     if not locals or name in ignore or fromlist == ('*',):
         # these cases we can't really delay
-        return _origimport(name, globals, locals, fromlist)
+        if level is None:
+            return _origimport(name, globals, locals, fromlist)
+        else:
+            return _origimport(name, globals, locals, fromlist, level)
     elif not fromlist:
         # import a [as b]
         if '.' in name: # a.b

More information about the Mercurial-devel mailing list