[Product-Developers] Re: [Plone-developers] plone.app.layout ViewletBase and portal_url

Wichert Akkerman wichert at wiggy.net
Fri Mar 7 12:02:22 UTC 2008


Previously Raphael Ritz wrote:
> Wichert Akkerman wrote:
> > I'm debugging some code that broke in a very unexpected way:
> > 
> > (Pdb) p getToolByName(self, 'portal_url')
> > 'http://localhost:8080/Plone'
> > 
> > That should return a tool, not a string. This turned out to be caused by 
> > plone.app.layout.viewlets.common.ViewletBase.update which sets 
> > self.portal_url to the portal URL. This looks harmless, but due to 
> > acquisition this will break patterns like this:
> > 
> > class MyViewlet(ViewletBase):
> >    def update(self):
> >       super(MyViewlet, self).update()
> >       self.mt=getToolByName(self.context, "portal_membership")
> >       self.somemethod()
> > 
> >    def somemethod(self):
> >       return self.mt.ToolMethod()
> > 
> > the problem here is that in somemethod() self.mt gets acquisition wrapped in 
> > the viewlet, so when ToolMethod does getToolByName(self, "portal_url") it
> > finds the portal_url variable ViewletBase.update set on the viewlet instead
> > of the tool.
> 
> :-(
> 
> (how long did it take you to track this down? Quite a while I
> could imagine)

After dropping PDBDebugMode in the instance about a minute, I happened
to know what ViewletBase.update does, how getToolByName works internally
and what acquisition can do to you which made it reasonably obvious. If
you don't happen to be familiar with those three things I can image this
can be quite painful to debug.

> > I would like to rename that variable in ViewletBase to prevent this problem 
> > but I do not have a good overview of how many people are relying on that 
> > specific variable to be set. If you are relying on this please let me know 
> > ASAP. If I here no objection I intend to rename the variable for the 
> > upcoming Plone 3.1 alpha release.
> 
> +1 on renaming the variable.

Hanno gave his +1 as well after observing the interface does guarantee
portal_url to be there anyway. I'll rename it to site_url and to a
little cleanup in the viewlets.

Wichert.

-- 
Wichert Akkerman <wichert at wiggy.net>    It is simple to make things.
http://www.wiggy.net/                   It is hard to make things simple.




More information about the Product-Developers mailing list