~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.

This is used to replace various ad hoc implementations of the same logic,
notably the version used in registry's _LazyObjectGetter which had a bug when
getting a module without also getting a member.  And of course, this new
function has unit tests, unlike the replaced code.

This also adds a KnownHooksRegistry subclass to provide a more natural home for
some other logic.

I'm not thrilled about the name of the new module or the new functions, but it's
hard to think of good names for such generic functionality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
    lockdir,
40
40
    lru_cache,
41
41
    osutils,
 
42
    pyutils,
42
43
    revision as _mod_revision,
43
44
    static_tuple,
44
45
    symbol_versioning,
52
53
from bzrlib.testament import Testament
53
54
""")
54
55
 
 
56
import sys
55
57
from bzrlib import (
56
58
    errors,
57
59
    registry,
2836
2838
            % (name, from_module),
2837
2839
            DeprecationWarning,
2838
2840
            stacklevel=2)
2839
 
        m = __import__(from_module, globals(), locals(), [name])
2840
2841
        try:
2841
 
            return getattr(m, name)
 
2842
            return pyutils.get_named_object(from_module, name)
2842
2843
        except AttributeError:
2843
2844
            raise AttributeError('module %s has no name %s'
2844
 
                    % (m, name))
 
2845
                    % (sys.modules[from_module], name))
2845
2846
    globals()[name] = _deprecated_repository_forwarder
2846
2847
 
2847
2848
for _name in [