[BUG][MERGE] 'bzr upgrade' broken by lazy imports

John Arbash Meinel john at arbash-meinel.com
Wed Nov 8 15:16:21 GMT 2006


Wouter van Heyst wrote:
...

>> My initial feeling is to just change most code paths that use
>> 'bzrilb.foo' to being 'from bzrlib import foo' at the beginning, and use
>> 'foo.bar' later on.
> 
> The fully explicit form can't be used with lazy importing?
> 
> Wouter van Heyst

It can, the code handles it okay. There are other issues, though.

The biggest one is that if we use 'bzrlib.foo' then if someone else
imported 'foo' first, it will work, even if we forget to. Which is our
current source of bugs, because now 'foo' isn't being imported first. It
can be thought of as a race condition, and we are changing the timings.

So being explicit means we don't have to worry about it. (If 'bzrlib'
isn't in the local namespace, there is no temptation to use members of
it that haven't been imported yet).

There are some other possibilities, like 'bzrlib/__init__.py' could
lazily import all modules, but that actually has some bad interactions
with other functionality. IIRC if you do 'from bzrlib import foo' it
will first try to import the member of module 'bzrlib' and if there
isn't one, it will fall back to importing the module 'foo'. Which ends
up setting a member of bzrlib, so it generally works out.

The problem is that we intentionally made lazy import objects not be
proxies. So you have to lazy import the real thing into your namespace,
you can't bind it to another name (which means it wouldn't be replaced,
and thus would need to be proxied). So doing 'from bzrlib import foo'
would give you a local reference to a lazy object that was to be
replaced in another scope.

Anyway, it is mostly just a safety check. You can't try to access
something that hasn't been imported if the bzrlib namespace isn't there.
(Or at least, the test suite will fail in that situation, which it won't
if you go bzrlib.foo since the test suite imports *everything*)

John
=:->


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20061108/18ae68f5/attachment.pgp 


More information about the bazaar mailing list