[Zope3-checkins] SVN: Zope3/trunk/src/zope/interface/ Make sure
that the implementedBy(interface, obj) call always succeeds. An
Stephan Richter
srichter at cosmos.phy.tufts.edu
Wed Nov 16 17:42:26 EST 2005
Log message for revision 40184:
Make sure that the implementedBy(interface, obj) call always succeeds. An
attribute error was raised, if obj did not have an attribute called
__name__.
This fix addresses issue 470, but using a different solution than
suggested.
.--This line, and those below, will be ignored--
M interface/interface.py
M interface/declarations.py
Changed:
U Zope3/trunk/src/zope/interface/declarations.py
U Zope3/trunk/src/zope/interface/interface.py
-=-
Modified: Zope3/trunk/src/zope/interface/declarations.py
===================================================================
--- Zope3/trunk/src/zope/interface/declarations.py 2005-11-16 22:05:03 UTC (rev 40183)
+++ Zope3/trunk/src/zope/interface/declarations.py 2005-11-16 22:42:26 UTC (rev 40184)
@@ -29,6 +29,7 @@
__docformat__ = 'restructuredtext'
import sys
+import types
import weakref
from zope.interface.interface import InterfaceClass, Specification
from ro import mergeOrderings, ro
@@ -264,7 +265,7 @@
# interfaces actually declared for a class
declared = ()
-
+
__name__ = '?'
def __repr__(self):
@@ -293,8 +294,20 @@
... implements(I3)
>>> [i.getName() for i in implementedBy(C2)]
['I3', 'I2']
+
+ Really, any object should be able to receive a successful answer, even
+ an instance:
+
+ >>> class Callable(object):
+ ... def __call__(self):
+ ... return self
+
+ >>> implementedBy(Callable())
+ <implementedBy zope.interface.declarations.?>
+
+ Note that the name of the spec ends with a '?', because the `Callable`
+ instance does not have a `__name__` attribute.
"""
-
# This also manages storage of implementation specifications
try:
@@ -356,7 +369,7 @@
spec.inherit = cls
spec.__name__ = (getattr(cls, '__module__', '?') or '?') + \
- '.' + cls.__name__
+ '.' + (getattr(cls, '__name__', '?') or '?')
try:
cls.__implemented__ = spec
Modified: Zope3/trunk/src/zope/interface/interface.py
===================================================================
--- Zope3/trunk/src/zope/interface/interface.py 2005-11-16 22:05:03 UTC (rev 40183)
+++ Zope3/trunk/src/zope/interface/interface.py 2005-11-16 22:42:26 UTC (rev 40184)
@@ -106,7 +106,7 @@
>>> directlyProvides(C, I1)
>>> I1.providedBy(C)
True
-
+
"""
spec = providedBy(ob)
return self in spec._implied
@@ -195,7 +195,7 @@
>>> I3.extends(I1)
0
-
+
"""
# Copy some base class methods for speed
@@ -237,16 +237,16 @@
# Register ourselves as a dependent of our old bases
for b in self.__bases__:
b.unsubscribe(self)
-
+
# Register ourselves as a dependent of our bases
self.__dict__['__bases__'] = bases
for b in bases:
b.subscribe(self)
-
+
self.changed()
__bases__ = property(
-
+
lambda self: self.__dict__.get('__bases__', ()),
__setBases,
)
@@ -305,8 +305,8 @@
if interface not in seen:
seen[interface] = 1
yield interface
-
+
def extends(self, interface, strict=True):
"""Does the specification extend the given interface?
@@ -369,7 +369,7 @@
if attr is not None:
attrs[name] = attr
break
-
+
if attr is None:
return default
else:
@@ -453,7 +453,7 @@
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
- >>>
+ >>>
>>> i = I1.interfaces()
>>> i.next().getName()
'I1'
@@ -582,7 +582,7 @@
# This provides some consistency with the PEP 246 adapt method.
marker = object()
-
+
def __call__(self, obj, alternate=marker):
"""Adapt an object to the interface
@@ -684,7 +684,7 @@
This method is normally not called directly. It is called by
the PEP 246 adapt framework and by the interface __call__
- operator.
+ operator.
The adapt method is responsible for adapting an object to
the reciever.
More information about the Zope3-Checkins
mailing list