[Zope3-checkins] CVS: Zope3/src/zope/app/container -
constraints.py:1.2
Jim Fulton
cvs-admin at zope.org
Wed Dec 3 00:41:20 EST 2003
Update of /cvs-repository/Zope3/src/zope/app/container
In directory cvs.zope.org:/tmp/cvs-serv19199/src/zope/app/container
Modified Files:
constraints.py
Log Message:
Fixed bugs in checking factiries. Changed to use
IFactory interface (rather than just using implementedBy)
to get factory interfaces.
=== Zope3/src/zope/app/container/constraints.py 1.1 => 1.2 ===
--- Zope3/src/zope/app/container/constraints.py:1.1 Mon Dec 1 11:19:21 2003
+++ Zope3/src/zope/app/container/constraints.py Wed Dec 3 00:41:19 2003
@@ -105,11 +105,19 @@
whether an object produced by a factory can be added. To do this, we
use checkFactory:
- >>> checkFactory(c1, "bob", O)
+ >>> class Factory:
+ ... def __call__(self):
+ ... return O()
+ ... def getInterfaces(self):
+ ... return zope.interface.implementedBy(O)
+
+ >>> factory = Factory()
+
+ >>> checkFactory(c1, "bob", factory)
True
>>> del c1.x
- >>> checkFactory(c1, "bob", O)
+ >>> checkFactory(c1, "bob", factory)
False
Unlike checkObject, checkFactory:
@@ -122,7 +130,7 @@
check the factory:
>>> c1.x = 1
- >>> checkFactory(c1, "bob", O)
+ >>> checkFactory(c1, "Zbob", factory)
True
To work with checkFactory, a container precondition has to
@@ -135,7 +143,7 @@
We can do this (silly thing) because preNoZ doesn't use the object
argument.
- >>> checkFactory(c1, "bob", O)
+ >>> checkFactory(c1, "Zbob", factory)
False
$Id$
@@ -181,12 +189,14 @@
except AttributeError:
pass
else:
- if not precondition(container, name, factory):
+ try:
+ precondition(container, name, factory)
+ except zope.interface.Invalid:
return False
break
# check the constraint on __parent__
- for iface in zope.interface.implementedBy(factory):
+ for iface in factory.getInterfaces():
__parent__ = iface.get('__parent__')
if __parent__ is not None:
try:
@@ -233,6 +243,14 @@
>>> class Ob:
... pass
>>> ob = Ob()
+
+ >>> class Factory:
+ ... def __call__(self):
+ ... return Ob()
+ ... def getInterfaces(self):
+ ... return zope.interface.implementedBy(Ob)
+
+ >>> factory = Factory()
>>> try:
... precondition(None, 'foo', ob)
@@ -243,16 +261,16 @@
None True True
>>> try:
- ... precondition.factory(None, 'foo', Ob)
+ ... precondition.factory(None, 'foo', factory)
... except InvalidItemType, v:
- ... print v[0], (v[1] is Ob), (v[2] == (I1, I2))
+ ... print v[0], (v[1] is factory), (v[2] == (I1, I2))
... else:
... print 'Should have failed'
None True True
>>> zope.interface.classImplements(Ob, I2)
>>> precondition(None, 'foo', ob)
- >>> precondition.factory(None, 'foo', Ob)
+ >>> precondition.factory(None, 'foo', factory)
"""
@@ -268,7 +286,7 @@
raise InvalidItemType(container, object, self.types)
def factory(self, container, name, factory):
- implemented = zope.interface.implementedBy(factory)
+ implemented = factory.getInterfaces()
for iface in self.types:
if implemented.isOrExtends(iface):
More information about the Zope3-Checkins
mailing list