def setUp(self): """ Configure L{twisted.python.components.registerAdapter} to mutate an alternate registry to improve test isolation. """ # Create a brand new, empty registry and put it onto the components # module where registerAdapter will use it. Also ensure that it goes # away at the end of the test. scratchRegistry = AdapterRegistry() self.patch(components, 'globalRegistry', scratchRegistry) # Hook the new registry up to the adapter lookup system and ensure that # association is also discarded after the test. hook = _addHook(scratchRegistry) self.addCleanup(_removeHook, hook)
def _registryActive(registry): """ A context manager that activates and deactivates a zope adapter registry for the duration of the call. For example, if you wanted to have a function that could adapt C{IFoo} to C{IBar}, but doesn't expose that adapter outside of itself:: def convertToBar(maybeFoo): with _registryActive(_registryAdapting((IFoo, IBar, fooToBar))): return IBar(maybeFoo) @note: This isn't thread safe, so other threads will be affected as well. @param registry: The registry to activate. @type registry: L{AdapterRegistry} @rtype: """ hook = _addHook(registry) yield _removeHook(hook)