~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bundle/serializer/__init__.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:
21
21
from StringIO import StringIO
22
22
import re
23
23
 
24
 
import bzrlib.errors as errors
 
24
from bzrlib import (
 
25
    errors,
 
26
    pyutils,
 
27
    )
25
28
from bzrlib.diff import internal_diff
26
29
from bzrlib.revision import NULL_REVISION
27
30
# For backwards-compatibility
191
194
    :param overwrite: Should this version override a default
192
195
    """
193
196
    def _loader(version):
194
 
        mod = __import__(module, globals(), locals(), [classname])
195
 
        klass = getattr(mod, classname)
 
197
        klass = pyutils.get_named_object(module, classname)
196
198
        return klass(version)
197
199
    register(version, _loader, overwrite=overwrite)
198
200