[z3-five] Re: Traversal issue [was: 0.2 release]

Tres Seaver tseaver at zope.com
Mon Oct 4 15:47:18 MEST 2004


Sidnei da Silva wrote:
> On Mon, Oct 04, 2004 at 09:23:52AM -0400, Tres Seaver wrote:
> | >No 'sane' code seems to implement __bobo_traverse__.
> | 
> | We have a fair amount of code which does implement it, although 
> | __getitem__ is often easier to get right.  Here is code in the core 
> | which *does* implement it:
> | 
> |   $ find . -name "*.py" | xargs grep -l "def __bobo_traverse__"
> |   ./lib/python/AccessControl/securitySuite/SecurityBase.py
> |   ./lib/python/App/ApplicationManager.py
> |   ./lib/python/App/FactoryDispatcher.py
> |   ./lib/python/App/Product.py
> |   ./lib/python/HelpSys/HelpUtil.py
> |   ./lib/python/OFS/Application.py
> |   ./lib/python/OFS/PropertySheets.py
> |   ./lib/python/OFS/tests/testTraverse.py
> |   ./lib/python/Products/OFSP/Draft.py
> |   ./lib/python/Products/PageTemplates/PathIterator.py
> |   ./lib/python/Products/SiteAccess/VirtualHostMonster.py
> |   ./lib/python/Products/ZCatalog/ZCatalogIndexes.py
> |   ./lib/python/Shared/DC/ZRDB/DA.py
> |   ./lib/python/ZClasses/Method.py
> |   ./lib/python/ZODB/ZApplication.py
> |   ./lib/python/webdav/NullResource.py
> 
> Gee, I must have been drunk by the time I grepped :P

LOFL.

> | >In fact, I
> | >couldn't find a example of how it should look like. It's really
> | >annoying that the consumers of __bobo_traverse__ expect it to always
> | >return something and never raise an exception. 
> | 
> | Lots of implementors raise various exceptions. 
> | OFS.Traversable.Traversable.unrestrictedTraverse catches exceptions when 
> | provided with a default.  The 'restrictedTraverse' in PageTemplates 
> | doesn't take a default, and therefore propagates exceptions.
> 
> Yes, looking at it again, that seems correct. Still I don't see an
> easy way to avoid the exception in PageTemplate's
> restrictedTraverse. Or do you mean it should indeed raise the
> exception there?

I don't see why we should be avoiding Attribute / KeyErrors, which get 
handled at the "outermost" layer by the expression machinery. 
Application code should generally *not* raise NotFoundError, preferring 
to let the publisher transmute the "real" error into a 404.

> Yes, but even the Publisher is confused there, afaics. It does
> correctly handle AttributeError/KeyError into a NotFound for
> everything, except for __bobo_traverse__:
> 
> BaseRequest:301 (Zope-2_7-branch)
> ==========================================================================
>         try:
>           <snip>
>                 if hasattr(object,'__bobo_traverse__'):
>                     subobject=object.__bobo_traverse__(request,entry_name)
>                     if type(subobject) is type(()) and len(subobject) > 1:
>                         # Add additional parents into the path                     
>                         parents[-1:] = list(subobject[:-1])
>                         object, subobject = subobject[-2:]
>                 else:
>                   <snip handling of AtributeError into NotFound>
>            <snip>
>          finally:
>             ...
> ==========================================================================
> 
> If I understand the above correctly, it will end up not handling the
> AttributeError for __bobo_traverse__, thus resulting into a 500
> response instead of 404.

OK, then let's fix this in Zope, rather than in Five.

Tres.
-- 
===============================================================
Tres Seaver                                tseaver at zope.com
Zope Corporation      "Zope Dealers"       http://www.zope.com



More information about the z3-five mailing list