[ZPT] Semantics of double slash in path expressions changed from Zope2.5.1 to 2.6?

Clemens Robbenhaar zpt@zope.org
Fri, 6 Dec 2002 14:01:53 +0100


About my last posting with the 'here//get_title_or_id' issue:

 >  In Zope2.5.1, a path expression like 'here//title_or_id' seems to be
 > equivalent to 'here/title_or_id', e.g. consecutive slashes are joined to
 > one slash implicitly. This is what I would expect from path expressions, in
 > analogy e.g. to URL's (though path expressions are not URL's, of course.)
 > 
 >  In Zope2.6.0, the path expression 'here//title_or_id' seems to be
 > loosely equivalent to the python expression:
 > 
 >  python:getattr( getattr(here,''),'title_or_id')
 > 
 > What actually happens is that something having an empty id is first
 > looked up in 'here', before looking up 'title_or_id' in that object.
 > 
 >  Usually this leads to a 'KeyError' (with the empty string as a key).
 > The exception may be prevented if someone manages to add an object with
 > the empty string as id into the ZODB. I have no idea what use case may
 > be there for doing this.

 ... well, some day I will learn not to post things late in the evening,
but to come back the next day before I ask the list :(

 After reading the diff of the sources I found there is an explanation
for this change as a fix for http://collector.zope.org/Zope/581

 The issue has been that one cannot do things like 'd/?k' in 2.5.1,
when "k" evaluates to the empty string. As 'd' may be something with
item access, this may make indeed sense, as the empty string is not a
valid id, but a valid key for an item.

E.g.: 

 <tal:block 
    define="
       d python:{'1':'Yabba', '0':'Yabba', '':'Hey!'}; 
       k string:"
    replace="d/?k">test</tal:block>
 
 renders "Hey!" with 2.6.0, but creates an error with 2.5.1

Now as some 'd.maurer' has collected this issue, who is most probably
Dieter Maurer, I guess there are real use cases for this issue. ;)


 But are there uses cases for empty string access without the '/?'
notation? I cannot think of any, but it could lead to some stupid
errors. I run into someone others code with this issue, who did
something like:

 <tal:block 
    define="location python:'here/%s/absolute_url' % somewhere"
    replace="python:path(location)" />

 Now this prints where the object with path 'somewhere' is located, if
acquired from the current context, except when 'somewhere' is the empty
string, in which case 2.5.1 renders the absolute url of the object
representing the current context. 2.6.0 barfs for the explanation above.


 A possible workaround would be to treat double slashes as a single one
when splitting a path expression. This would leave the solution for
the collector issue #581 intact but avoid the less intuitive behaviour
for multiple consecutive slashes. 

 Actually I as a programmer can live with this behaviour (an expression
like 'python:here..title_or_id()' is not valid as well), but I thought
the path expressions were invented for people coming more from the
HTML designer side.

 Now how should I tell my designer people about item access and such? 
I would prefer to add a patch removing duplicated slashes.

 Or is there a use case for this I have missed?

Cheers,
Clemens