[Zope3-checkins] SVN: Zope3/trunk/src/zope/component/ Fixed XXX by doing the right thing and allow option interfaces argument

Stephan Richter srichter at cosmos.phy.tufts.edu
Sat Jul 10 10:44:16 EDT 2004


Log message for revision 26404:
Fixed XXX by doing the right thing and allow option interfaces argument 
to the Factory constructor.



-=-
Modified: Zope3/trunk/src/zope/component/factory.py
===================================================================
--- Zope3/trunk/src/zope/component/factory.py	2004-07-10 14:36:36 UTC (rev 26403)
+++ Zope3/trunk/src/zope/component/factory.py	2004-07-10 14:44:16 UTC (rev 26404)
@@ -16,6 +16,7 @@
 $Id$
 """
 from zope.interface import implements, implementedBy
+from zope.interface.declarations import Implements
 from zope.component.interfaces import IFactory
 
 class Factory(object):
@@ -26,18 +27,23 @@
     """
     implements(IFactory)
 
-    def __init__(self, callable, title='', description=''):
+    def __init__(self, callable, title='', description='', interfaces=None):
         self._callable = callable
         self.title = title
         self.description = description
+        self._interfaces = interfaces
 
     def __call__(self, *args, **kw):
         return self._callable(*args, **kw)
 
     def getInterfaces(self):
+        if self._interfaces is not None:
+            spec = Implements(*self._interfaces)
+            spec.__name__ = getattr(self._callable, '__name__', '[callable]')
+            return spec
         try:
             return implementedBy(self._callable)
         except TypeError:
-            # XXX This is a hack
-            # We really only support classes
-            return implementedBy(object())
+            spec = Implements()
+            spec.__name__ = getattr(self._callable, '__name__', '[callable]')
+            return spec

Modified: Zope3/trunk/src/zope/component/tests/test_factory.py
===================================================================
--- Zope3/trunk/src/zope/component/tests/test_factory.py	2004-07-10 14:36:36 UTC (rev 26403)
+++ Zope3/trunk/src/zope/component/tests/test_factory.py	2004-07-10 14:44:16 UTC (rev 26404)
@@ -25,6 +25,9 @@
 from zope.component.factory import Factory
 from placelesssetup import PlacelessSetup
 
+class IFunction(Interface):
+    pass
+
 class IKlass(Interface):
     pass
 
@@ -41,6 +44,7 @@
     def setUp(self):
         self._factory = Factory(Klass, 'Klass', 'Klassier')
         self._factory2 = Factory(lambda x: x, 'Func', 'Function')
+        self._factory3 = Factory(lambda x: x, 'Func', 'Function', (IFunction,))
 
     def testCall(self):
         kl = self._factory(3, foo=4)
@@ -48,19 +52,32 @@
         self.assertEqual(kl.args, (3, ))
         self.assertEqual(kl.kw, {'foo': 4})
         self.assertEqual(self._factory2(3), 3)
+        self.assertEqual(self._factory3(3), 3)
 
     def testTitleDescription(self):
         self.assertEqual(self._factory.title, 'Klass')
         self.assertEqual(self._factory.description, 'Klassier')
+        self.assertEqual(self._factory2.title, 'Func')
+        self.assertEqual(self._factory2.description, 'Function')
+        self.assertEqual(self._factory3.title, 'Func')
+        self.assertEqual(self._factory3.description, 'Function')
 
     def testGetInterfaces(self):
         implemented = self._factory.getInterfaces()
         self.assert_(implemented.isOrExtends(IKlass))
         self.assertEqual(list(implemented), [IKlass])
+        self.assertEqual(implemented.__name__,
+                         'zope.component.tests.test_factory.Klass')
+
         implemented2 = self._factory2.getInterfaces()
         self.assertEqual(list(implemented2), [])
+        self.assertEqual(implemented2.__name__, '<lambda>')
 
+        implemented3 = self._factory3.getInterfaces()
+        self.assertEqual(list(implemented3), [IFunction])
+        self.assertEqual(implemented3.__name__, '<lambda>')
 
+
 class TestFactoryZAPIFunctions(PlacelessSetup, unittest.TestCase):
 
     def setUp(self):



More information about the Zope3-Checkins mailing list