[mod_python] psp problem

Graham Dumpleton grahamd at dscpl.com.au
Mon May 16 07:31:37 EDT 2005


On 13/05/2005, at 1:15 AM, Jim Gallacher wrote:

> Wouter van Marle wrote:
>> On Wed, 2005-05-11 at 23:49 -0400, Gregory (Grisha) Trubetskoy wrote:
>>> On Wed, 11 May 2005, Jim Gallacher wrote:
>>>
>>>
>>>> You found one of the warts in psp. Since your for loop contains no 
>>>> further python statements, the parser gets confused. Try adding a 
>>>> single comment at the beginning of your for loop:
>>>>
>>>> <p> some text for the page </p>
>>>> <%
>>>> for r in Results:
>>>>   # begin indentation - comment fixes the syntax error
>>>> %>
>>>> I got the result <%=r%><br>
>>>> <%
>>>> # end indentation
>>>> %>
>>>
>>>
>>> Actually, (IIRC purposely undocumented), PSP is smart enough to 
>>> guess that a if code ends on a ":" you mean to start a new block.
>> I have found documentation where this is indeed confirmed; a line 
>> ending
>> in a colon (:) starts an indentation.
>
> I used indented comment trick after the for statment for a problem 
> unrelated to Wouter's. I'm now in the habit of always using it.
>
> For example, if you have a nested for loop such as the following, an 
> IndentationError is raised.
>
> <%
> for title,doc_list in unit_list:
> %>
>   <h4>Unit <%= title %><h4>
> <%
>     for doc in doc_list:
> %>
>     <p><%= doc['url'] %>: <%= doc['title'] %></p>
> <%
>     # end of block
> # end of block
> %>
>
> I noticed that if there was some python statement immediately after 
> the first for:, the error went away. Now I always stick an indented 
> comment immediately after the for statement. I also found problems 
> when generating tables in the inner loop, where I was only getting a 
> single closing </table> tag instead of one for each iteration of the 
> inner loop (or something like that - I can't generate an example off 
> the top of my head and the offending code is now gone).
>
> I haven't commented on this before as it was a problem I had when I 
> first starting using psp pages a few months ago, and I wasn't sure if 
> it was a feature or a bug. :)

I just got bitten by this gotcha as well. Although the documentation 
says:

   The parser is also smart enough to figure out the indent if the last 
line of Python ends with ":" (colon).

What isn't clear is that when PSP performs auto indentation, it does it 
with an indent
level of 8 spaces or 1 tab equivalent. Thus unless using the comment 
workaround it
means that if further down within that block you want to put additional 
code, you
MUST ensure that it is indented by 8 characters or 1 tab else you get 
an error.

For example, the following code which uses 4 spaces for indenting will 
fail.

<ul>
<%
for i in range(3):
%>
     <li><%=i%>
<%
     for j in range(2):
%>
         <p><%=i%>-<%=j%></p>
<%
     #end for
%>
     </li>
<%
#end for
%>
</ul>

The actual error is:

   IndentationError: unindent does not match any outer indentation level 
(indent.psp, line 7)

On first glance, it isn't really exactly clear what is going wrong.

The solution is to use either comment at start of block as described 
above.

<ul>
<%
for i in range(3):
     # indent
%>
     <li><%=i%>
<%
     for j in range(2):
         # indent
%>
         <p><%=i%>-<%=j%></p>
<%
     #end for
%>
     </li>
<%
#end for
%>
</ul>

or use 8 spaces consistently.

<ul>
<%
for i in range(3):
%>
         <li><%=i%>
<%
         for j in range(2):
%>
                 <p><%=i%>-<%=j%></p>
<%
%>
         </li>
<%
#end for
%>
</ul>

Thus, would it be worthwhile to add specific note in documentation 
about 8 space/1 tab
indent to make it clear for the auto indent case.

Graham



More information about the Mod_python mailing list