[Zope-dev] Interfaces vs ZCA concepts

Tres Seaver tseaver at palladion.com
Fri Dec 18 12:42:20 EST 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thomas Lotze wrote:
> 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.

Those advantages don't obtain if the call site itself catches the CLE:
at that point, all you have is a more expensive and less clear way to
get what 'queryUtility' gives you.


Tres
- --
===================================================================
Tres Seaver          +1 540-429-0999          tseaver at palladion.com
Palladion Software   "Excellence by Design"    http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAksrvvwACgkQ+gerLs4ltQ64JgCfc9sKxivpsIZJT8A3ep+pW0cZ
J7AAn0yRW2137WO7cHq92zAXCTj33cwf
=anzG
-----END PGP SIGNATURE-----



More information about the Zope-Dev mailing list