[MERGE] Lazy Importer
John Arbash Meinel
john at arbash-meinel.com
Tue Sep 12 20:25:21 BST 2006
This is a stepping stone towards getting demandload() like support for
bzrlib. I decided to rewrite what demandload did, because I didn't
really like the way they did it, and more importantly, I *really* didn't
like their syntax. Also, I don't think they had written it in a
test-driven method. Which I have done here.
So attached is a patch which provides the 'bzrlib.lazy_import' module,
which has 1 important function 'lazy_import'. It tries quite hard to be
compatible with standard python import syntax, so we can just wrap
around the area in question, without having to change all the lines.
For example:
from bzrlib.lazy_import import lazy_import
lazy_import(globals(), """
import os
import subprocess
import sys
import time
from bzrlib import (
errors,
transport,
foo as bar,
)
""")
Should make all of those modules lazy modules.
It can load classes or functions as well as modules, but having used it
a bit, it is safest to only import modules. (You always have to access a
sub-object with a module. With a class/function/variable it may be
useful by itself, which means we don't get an alert that we need to
mutate ourself.)
Also doing this is bad:
lazy_import(globals, 'import foo')
bar = foo
At this point bar points to the lazy object foo, and not the real foo
object. With the biggest problem being a future
foo.something
will cause 'foo' to be replaced with its real object, but 'bar' will
still be a lazy import object. It turns out that I can't detect this
until later, but if 'bar.something' is accessed, I'll raise a
IllegalUseOfScopeReplacer error, to help developers realize that they
did something wrong.
I also try pretty hard to forget about the scope and factory after an
action has occurred, to help avoid circular dependencies.
If we wanted, it would be pretty easy to save the real object, and just
keep returning attributes (demandload() does this). But since the point
of this code is so that on first access the variable gets replaced, and
you suffer no further overhead, it seemed wrong to silently allow more
overhead to continue.
I'd really like to at least get this into 0.11. I'll be starting another
branch which starts using it, to cut down on the bzr load time. But
probably we need a big change like that to sit in bzr.dev for longer
than 1 week before we freeze and then release it.
John
=:->
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lazy_import.diff
Type: text/x-patch
Size: 55678 bytes
Desc: not available
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20060912/c7562918/attachment.bin
-------------- 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/20060912/c7562918/attachment.pgp
More information about the bazaar
mailing list