本文整理汇总了Python中zope.interface.registry.Components.unregisterHandler方法的典型用法代码示例。如果您正苦于以下问题:Python Components.unregisterHandler方法的具体用法?Python Components.unregisterHandler怎么用?Python Components.unregisterHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zope.interface.registry.Components
的用法示例。
在下文中一共展示了Components.unregisterHandler方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestHandler
# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import unregisterHandler [as 别名]
#.........这里部分代码省略.........
self.assertEqual(x, test_object1)
@adapter(I1, I2)
def handle12(x, y):
self.assertEqual(x, test_object1)
self.assertEqual(y, test_object2)
handle_calls = []
def handle(*objects):
handle_calls.append(objects)
self.components.registerHandler(handle1, info=u'First handler')
self.components.registerHandler(handle12)
self.components.registerHandler(
handle, required=[I1], info=u'a comment')
self.components.registerHandler(
handle, required=[U], info=u'handle a class')
handlers = list(self.components.registeredHandlers())
handlers_required = map(lambda x: getattr(x, 'required'), handlers)
handlers_handler = map(lambda x: getattr(x, 'handler'), handlers)
handlers_info = map(lambda x: getattr(x, 'info'), handlers)
self.assertEqual(len(handlers), 4)
self.assertEqual(handlers_required,
[(I1,), (I1, I2), (I1,), (implementedBy(U),)])
self.assertEqual(handlers_handler,
[handle1, handle12, handle, handle])
self.assertEqual(
handlers_info,
[u'First handler', u'', u'a comment', u'handle a class'])
def test_unregister_handler(self):
test_object1 = U1(1)
test_object2 = U12(2)
@adapter(I1)
def handle1(x):
self.assertEqual(x, test_object1)
@adapter(I1, I2)
def handle12(x, y):
self.assertEqual(x, test_object1)
self.assertEqual(y, test_object2)
handle_calls = []
def handle(*objects):
handle_calls.append(objects)
self.components.registerHandler(handle1, info=u'First handler')
self.components.registerHandler(handle12)
self.components.registerHandler(
handle, required=[I1], info=u'a comment')
self.components.registerHandler(
handle, required=[U], info=u'handle a class')
self.assertEqual(len(list(self.components.registeredHandlers())), 4)
self.assertTrue(self.components.unregisterHandler(handle12))
self.assertEqual(len(list(self.components.registeredHandlers())), 3)
self.assertFalse(self.components.unregisterHandler(handle12))
self.assertEqual(len(list(self.components.registeredHandlers())), 3)
self.assertRaises(TypeError, self.components.unregisterHandler)
self.assertEqual(len(list(self.components.registeredHandlers())), 3)
self.assertTrue(
self.components.unregisterHandler(handle, required=[I1]))
self.assertEqual(len(list(self.components.registeredHandlers())), 2)
self.assertTrue(self.components.unregisterHandler(handle, required=[U]))
self.assertEqual(len(list(self.components.registeredHandlers())), 1)
def test_multi_handler_unregistration(self):
"""
There was a bug where multiple handlers for the same required
specification would all be removed when one of them was
unregistered.
"""
from zope import interface
calls = []
class I(interface.Interface):
pass
def factory1(event):
calls.append(2)
def factory2(event):
calls.append(3)
class Event(object):
interface.implements(I)
self.components.registerHandler(factory1, [I,])
self.components.registerHandler(factory2, [I,])
self.components.handle(Event())
self.assertEqual(sum(calls), 5)
self.assertTrue(self.components.unregisterHandler(factory1, [I,]))
calls = []
self.components.handle(Event())
self.assertEqual(sum(calls), 3)