~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__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:
71
71
    lock as _mod_lock,
72
72
    memorytree,
73
73
    osutils,
 
74
    pyutils,
74
75
    ui,
75
76
    urlutils,
76
77
    registry,
861
862
 
862
863
    def _clear_hooks(self):
863
864
        # prevent hooks affecting tests
 
865
        known_hooks = hooks.known_hooks
864
866
        self._preserved_hooks = {}
865
 
        for key, factory in hooks.known_hooks.items():
866
 
            parent, name = hooks.known_hooks_key_to_parent_and_attribute(key)
867
 
            current_hooks = hooks.known_hooks_key_to_object(key)
 
867
        for key, (parent, name) in known_hooks.iter_parent_objects():
 
868
            current_hooks = getattr(parent, name)
868
869
            self._preserved_hooks[parent] = (name, current_hooks)
869
870
        self.addCleanup(self._restoreHooks)
870
 
        for key, factory in hooks.known_hooks.items():
871
 
            parent, name = hooks.known_hooks_key_to_parent_and_attribute(key)
 
871
        for key, (parent, name) in known_hooks.iter_parent_objects():
 
872
            factory = known_hooks.get(key)
872
873
            setattr(parent, name, factory())
873
874
        # this hook should always be installed
874
875
        request._install_hook()
3729
3730
        'bzrlib.tests.test_permissions',
3730
3731
        'bzrlib.tests.test_plugins',
3731
3732
        'bzrlib.tests.test_progress',
 
3733
        'bzrlib.tests.test_pyutils',
3732
3734
        'bzrlib.tests.test_read_bundle',
3733
3735
        'bzrlib.tests.test_reconcile',
3734
3736
        'bzrlib.tests.test_reconfigure',
4054
4056
        the module is available.
4055
4057
    """
4056
4058
 
4057
 
    py_module = __import__(py_module_name, {}, {}, ['NO_SUCH_ATTRIB'])
 
4059
    py_module = pyutils.get_named_object(py_module_name)
4058
4060
    scenarios = [
4059
4061
        ('python', {'module': py_module}),
4060
4062
    ]
4213
4215
            symbol_versioning.warn(depr_msg + use_msg, DeprecationWarning)
4214
4216
            # Import the new feature and use it as a replacement for the
4215
4217
            # deprecated one.
4216
 
            mod = __import__(self._replacement_module, {}, {},
4217
 
                             [self._replacement_name])
4218
 
            self._feature = getattr(mod, self._replacement_name)
 
4218
            self._feature = pyutils.get_named_object(
 
4219
                self._replacement_module, self._replacement_name)
4219
4220
 
4220
4221
    def _probe(self):
4221
4222
        self._ensure()