[mod_python] UnicodeEncodeError using mod_python-3.0.3 with Python 2.3

Sean Gillies sgillies at frii.com
Thu Sep 18 15:45:32 EST 2003


On Thursday, September 18, 2003, at 02:25  PM, David Fraser wrote:

> Sean Gillies wrote:
>
>> Hi,
>>
>> I'm running into problems with UnicodeEncodeErrors using   
>> mod_python-3.0.3 with
>> Python 2.3 on OS X.  Yes, I know I'm asking for trouble with this   
>> combination,
>> but I'm leery of replacing Apple's Python 2.2 (which doesn't work  
>> with  mod_python).
>> All in all, my application is working, but will eventually (and   
>> unpredictably)
>> stop and raise this error:
>>
>> Mod_python error: "PythonHandler mod_python.publisher"
>>
>> Traceback (most recent call last):
>>
>>   File   
>> "/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/ 
>> site- packages/mod_python/apache.py", line 332, in HandlerDispatch
>>     result = object(req)
>>
>>   File   
>> "/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/ 
>> site- packages/mod_python/publisher.py", line 201, in handler
>>     result = str(result)
>>
>> UnicodeEncodeError: 'ascii' codec can't encode character '\u3137' in   
>> position 412: ordinal not in range(128)
>>
>> Anybody else using Python 2.3 who has seen this?  I really don't   
>> understand the error,
>> it's always '\u3137', and I'm _not_ (to my knowledge) using any  
>> unicode  strings.  I am
>> using Zope's Page Templates, but I don't see where the page templates  
>>  would be introducing
>> '\u3137' (the hangul letter tikeut?!).
>>
>> My module is published with no problems on Linux (KRUD) with   
>> mod_python-3.0.1 and
>> Python 2.2.3, so I'm presently living with this problem on my  
>> notebook  and counting
>> on its absence on the server.
>>
>> Sean
>>
>> -- 
>
> Try doing things like:
>  if type(result) == unicode:
>    result = result.encode('iso8859')
> You can look up different encodings in the encodings module. The above  
> works well for me...
> I think you should be able to get the browser's desired encodings from  
> the headers sent to the request.
>
> Hope that helps
> David
>

David,

Thanks for the suggestion.  The Zope page template's __call__ method
does indeed return unicode and I've made your change to publisher.py.
Working good.  I also thought about catching the UnicodeEncodeError
like

     if result:
         try:
             result = str(result)
         except UnicodeEncodeError:
             result = str(result.encode('iso8859'))

since str(result) wasn't raising an error every time.

cheers,
Sean

--
Sean Gillies
sgillies at frii dot com
http://www.frii.com/~sgillies



More information about the Mod_python mailing list