Crosstips.org

My fun Crossword solver project. Crosstips.org & Krysstips.se

Kung Fu

Fujian White Crane Kung Fu

Fry-IT

Fry-IT is the company I work for

Photos

Photoalbum, both old and new.

Zope

What I have and am doing with Zope

Receptsamlingen

In Swedish only. About my "Collection of Recipes" website.

Contact me

My contact details and how to contact me.

 

KungFuPeople.com
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com


Mobile version of this page Mobile version of this page


 

Lesson learned: Unicodifying request variables in Zope


15th of April 2009

This cost me a good hour of debugging so I thought I'd share it in case anybody else stumbles across the same problem. In the end, to solve my problem I had to add debug statements to StringIO.py to be able to find out where in my Zope Page Template a non-unicode string with non-ascii characters appeared and messed things up.

The error I was getting was this, which I suspect several Zope developers have encountered before:

 UnicodeDecodeError: \
 'ascii' codec can't decode byte 0xc3 in position 46: ordinal not in range(128)

The traceback only mentions files in the innards of ZPT of which none you can really do anything about. We all know that the key to avoid Unicode error is to be consistent. You can do this:

 >>> '\xc3' + 'string'
 '\xc3string'
 >>> u'\xc3' + u'string'
 u'\xc3string'

But you can't do this:

 >>> '\xc3' + u'string'
 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: 
 ordinal not in range(128)

So, how did I get these non-unicode strings into my application in first place. Simple, I have a search parameter q and end up with a URL like this:

 /bla/bla?q=régime

And the template had this little innocent piece of code:

 <input tal:attributes="value request/q"/>

That's what f'ed everything up. So, I ended up having to add this:

 <input tal:attributes="value python:context.unicodify(request['q'])"/> 

With this little helper function in the base class:

 def unicodify(self, s):
    if isinstance(s, str):
        return unicode(s, 'utf8')
    return s

So, hopefully by writing this it will help someone else making the same trivial mistake and not wasting their evening with sporadic print statements all over their frameworks code.



Comment

Anonymous - 16th April 2009  [«« Reply to this]
Wouldn't bla/bla?q:ustring:utf8=régime be a simpler solution ?
Peter Bengtsson - 17th April 2009   [«« Reply to this]
Sure, ...but uglier. I often use, but rarely rely on Zope's magic type casting. In the same sense I often use but don't rely on Javascript
Damian - 18th April 2009  [«« Reply to this]
Hello, finally found your blog :) and it is very interesting. I got question, what is better Python or PHP in web development?
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.