[z3-five] use of aq_base

Jean-Marc Orliaguet jmo at chalmers.se
Sat Oct 29 20:58:00 CEST 2005


Florent Guillaume wrote:

> I'm not sure if it causes bugs or not, but I see this code several 
> times (for instance in site/utility.py):
>
>         utilities = getattr(aq_base(self.context), 'utilities', None)
>
> For instance followed by:
>         if utilities is not None:
>             utility = utilities._getOb(id, None)
>             if utility is not None:
>                 return utility
>
> Because of the aq_base, the "utility" will have an acquisition chain 
> that's truncated to just its parent. It may or not be a problem, but 
> in the context of Zope 2 many objects will fail in these circumstances.
>
> I advocate using:
>         if getattr(aq_base(self.context), 'utilities', None) is not 
> None:
>             utilities = self.context.utilities
>             ....
>
> Florent
>

good idea. It does cause bug, if the utility is used further down in the
code.
A similar mistake was once done on the CPSPortlets code to remove
acquisition on a variable for testing the presence of an attribute, and
it took a couple of hours to figure out what was wrong when the same
variable was used a couple of lines or methods later without the
complete acquisition chain.

/JM


More information about the z3-five mailing list