Chris Withers <chris <at>> writes:

> Hi All,
> I have a need to be able to adapting certain objects to None, eg:
> def some_adapter(obj):
>    if something:
>      return None
>    return somethingelse
> This is tricky, since returning None from an adapter results in a TypeError.
> I eventually came up with the idea of having a subclass of Interface do 
> what I needed:
> empty = object()
> class IFieldType(Interface):
>      def __call__(self,*args,**kw):
>          r = Interface.__call__(*args,**kw)
>          if r is empty:
>              return None
>          return r
> I suspect this would work with the python implementation of Interface, 
> but it certainly doesn't with the C implementation.
> What am I doing wrong and how can I achieve what I'm after?

If the result of an adaptation is None, it means something very specific to
the CA.  That TypeError is specifically a 'Could not adapt' TypeError, which
is similar to a ComponentLookupError but with a slightly different 
meaning (not sure why the CA doesn't use a custom exception for this).
This feature allows you to create an adapter which is capable of inspecting
its arguments and determining that it's not capable of adapting those
particular objects.  Because an adapter that returns None has this specific
meaning, you can't actually adapt something to None. You'll have to use
queryAdapter or explicitly handle the exception.


Zope-Dev maillist  -
**  No cross posts or HTML encoding!  **
(Related lists - )

Reply via email to