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

Dan Villiom Podlaski Christiansen danchr at gmail.com
Wed Jul 29 14:34:57 CDT 2009


On 29/07/2009, at 21.17, Matt Mackall wrote:

> On Wed, 2009-07-29 at 20:54 +0200, Dan Villiom Podlaski Christiansen
> wrote:
>> # 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')
>> +
>
> This bit looks unrelated?

Oh, yes. It should be a separate patch; I figured quite a lot of  
_demandmod instances are created, so adding __slots__ just might save  
a bit of memory.

>
>>     """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
>
> I assume this still works with 2.4?


Yes, on 2.4 the level argument isn't passed, so it's always None. Of  
course, *should* someone pass a level argument, it'll still break ;)

(Or to put it another way, I also ran Mercurial on 2.4 and didn't  
notice any breakage.)

--

Dan Villiom Podlaski Christiansen
danchr at gmail.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1943 bytes
Desc: not available
Url : http://selenic.com/pipermail/mercurial-devel/attachments/20090729/20ccbb3d/attachment.bin 


More information about the Mercurial-devel mailing list