[mod_python] PSP/Mac OS X line ending gotcha

Jeffrey A. Zelt jeffreyz at broadpark.no
Sat Apr 29 13:14:10 EDT 2006

I've only just started using mod_python, so please excuse this if it  
is documented in an obvious place.  Let me put it very simply.  On  
Mac OS X, make sure that your HTML files are terminated by :

    a line feed (ascii 10) and NOT a carriage return (ascii 13).

The Windows line ending of CRLF is also OK because it includes the  
line feed.  PSP does *not* like lines terminated by only a carriage  
return.  Here is how I encountered this.  It should also indicate  
when you should look out for this issue.

I successfully implemented some functionality through a custom  
generic python handler - that went pretty smoothly.  Then I wanted to  
use PSP for simple templating.  Everything worked perfectly when I  
first introduced python expressions delimited by:

    ‘<%=’  and  ‘%>’

However, as soon as I tried using python code delimited by:

    ‘<%’  and  ‘%>’

all hell broke loose and I could not get anything to work.  A  
simplified version of what I was trying was something like:

    for i in range(n):
        # start for loop

         HTML code to repeat n times

    # terminate for loop

This just repeats the block of HTML code n times.  The details of the  
python code here are not important.  This construction triggered  
various errors, depending on the details of the python code I  
inserted into the page.  The problem was that I was using Adobe  
GoLive to edit my web pages and to insert the python code in the page  
(I am talking about the source view, not the WYSIWYG/Layout view).   
My version of GoLive is a little old and still prefers to write files  
with carriage return line endings.  This was the default for the Mac  
OS before Mac OS X.  Everything *looks* OK in the GoLive editor and  
it also looked OK in the other editors I chose to check the code  
(other editors would have tipped me off, but I did not use one of  

However, it seems that the PSP engine, or whatever you want to call  
it, does not treat carriage returns as line endings, and therefore  
chokes badly when encountering code written this way.

GoLive has a menu item where you can select  the desired line  
terminator.  I selected "Unix (LF)".  This made no visible change to  
the document when viewed in GoLive, but then suddenly the <% ... %>  
PSP parts of the document worked perfectly.

It is probably obvious when you think about it, but it is tricky to  
detect if you are bit by it because you may not visibly see anything  
wrong with the code you are editing.

I hope this helps someone avoid what I went through.


More information about the Mod_python mailing list