[Zope] getattr default
Dieter Maurer
dieter@handshake.de
Mon, 29 Oct 2001 22:29:28 +0100 (CET)
Mitch Model <model@mpi.com> (by way of Mitchell Model) writes:
> What's wrong with the 'getattr' call in this DTML method? I get an
> error message: "Name not found or not allowed undefined." (using ZDebug).
> (The problem is not the choice of attribute names -- I get it whatever I type
> that isn't an attribute in the REQUEST.) If I arrange to get into pdb from
> this method, the getattr expression works fine. Is this a Zope bug?
>
> _____________________________________________________________________
> <html>
> <body>
> REQUEST does
> <dtml-if expr="not _.hasattr(REQUEST,'undefined')">
> not
> </dtml-if>
> have the attribute 'undefined'.
> <p>
> value = <dtml-var expr="_.getattr(REQUEST, 'undefined', 'nothing')">
> </body>
> </html>
> _____________________________________________________________________
"_.getattr" does not work with "REQUEST", because "REQUEST" is
primarily a mapping and does only expose its items as attributes, too.
More precisely, its "__getattr__" (which is called by "_.getattr")
is mapped to "REQUEST"'s "__getitem__" and this raises
"KeyError" instead on "AttributeError".
But "_.getattr" only catches "AttributeError" and propagates "KeyError".
I think this is a bug in "ZPublisher.BaseRequest.BaseRequest".
It should map "__getitem__"'s "KeyError" into "__getattr__"'s "AttributeError".
Would you like to write a tracker item?
Dieter