[Zope-dev] Interfaces vs ZCA concepts
Thomas Lotze
tl at gocept.com
Thu Dec 17 07:31:01 EST 2009
Martijn Faassen wrote:
> Hey,
>
> Tres Seaver wrote:
> [snip]
>> Any code today which wants a utility is calling 'getUtilty' (if it
>> *knows* the utility must be registered) or 'queryUtility' (if it thinks
>> it might not be). Less facetiously than my first challenge: please
>> point to actual code in the wild which looks like::
>>
>> try:
>> foo = getUtilty(IFoo, name='bar')
>> except ComponentLookupError:
>> # do something
>>
>> instead of::
>>
>> foo = queryUtility(IFoo, name='bar')
>> if foo is None:
>> # do something
>>
>> I will argue that any code doing the first, outside of maybe tests of
>> the ZCA itself is plain broken.
>
> I have code like that in the wild - I have no real reason why I didn't
> queryUtility, but I didn't think it mattered.
>
> Why is it plain broken? Isn't getUtility supported to raise
> ComponentLookupError if it cannot find the required utility?
The interface declaration for the zope.component API does assert that
getUtility raises a CLE if it cannot find the utility. Also, I wouldn't
say that queryUtility should be used instead of using getUtility and
catching the CLE, as that would deprive us of all the advantages of using
exceptions, such as propagation along the call stack.
--
Thomas
More information about the Zope-Dev
mailing list