summaryrefslogtreecommitdiff
path: root/docs/adapter.rst
diff options
context:
space:
mode:
authorJason Madden <jamadden@gmail.com>2020-03-05 09:52:35 -0600
committerJason Madden <jamadden@gmail.com>2020-03-09 12:51:18 -0500
commit1e720c3819b8daa07aaad0ee0258d9035597f27d (patch)
tree93f0ad828f0d8c8d23670aefeeeccaead854794a /docs/adapter.rst
parentc931999371b106ca4638a5cfb40fe0e431840358 (diff)
downloadzope-interface-issue11.tar.gz
Make provided/implementedBy and adapter registries respect super().issue11
The query functions now start by looking at the next class in the MRO (interfaces directly provided by the underlying object are not found). Adapter registries automatically pick up providedBy change to start finding the correct implementations of adapters, but to make that really useful they needed to change to unpack super() arguments and pass __self__ to the factory. Fixes #11 Unfortunately, this makes PyPy unable to build the C extensions. Additional crash-safety for adapter lookup. Make the C functions get the cache only after resolving the ``required`` into a tuple, in case of side-effects like...clearing the cache. This could lead to the ``cache`` object being deallocated before we used it. Drop the ``tuplefy`` function in favor of a direct call to ``PySequence_Tuple``. It's what the ``tuple`` constructor would do anyway and saves a few steps. Make sure that getting ``providedBy(super())`` and ``implementedBy(super())`` have no side effects.
Diffstat (limited to 'docs/adapter.rst')
-rw-r--r--docs/adapter.rst27
1 files changed, 25 insertions, 2 deletions
diff --git a/docs/adapter.rst b/docs/adapter.rst
index 6a728a3..50fcf8c 100644
--- a/docs/adapter.rst
+++ b/docs/adapter.rst
@@ -202,11 +202,11 @@ factories:
... pass
>>> @zope.interface.implementer(IR)
- ... class X:
+ ... class X(object):
... pass
>>> @zope.interface.implementer(IProvide1)
- ... class Y:
+ ... class Y(object):
... def __init__(self, context):
... self.context = context
@@ -238,6 +238,29 @@ We can register and lookup by name too:
>>> y.context is x
True
+Passing ``super`` objects works as expected to find less specific adapters:
+
+.. doctest::
+
+ >>> class IDerived(IR):
+ ... pass
+ >>> @zope.interface.implementer(IDerived)
+ ... class Derived(X):
+ ... pass
+ >>> class DerivedAdapter(Y):
+ ... def query_next(self):
+ ... return registry.queryAdapter(
+ ... super(type(self.context), self.context),
+ ... IProvide1)
+ >>> registry.register([IDerived], IProvide1, '', DerivedAdapter)
+ >>> derived = Derived()
+ >>> adapter = registry.queryAdapter(derived, IProvide1)
+ >>> adapter.__class__.__name__
+ 'DerivedAdapter'
+ >>> adapter = adapter.query_next()
+ >>> adapter.__class__.__name__
+ 'Y'
+
When the adapter factory produces ``None``, then this is treated as if no
adapter has been found. This allows us to prevent adaptation (when desired)
and let the adapter factory determine whether adaptation is possible based on