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.

Comments

Anonymous

Wouldn't bla/bla?q:ustring:utf8=régime be a simpler solution ?

Peter Bengtsson

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

Hello, finally found your blog :) and it is very interesting. I got question, what is better Python or PHP in web development?

Your email will never ever be published.

Previous:
Fujitsu FLEPia April 14, 2009 Books
Next:
Git + Twitter = Friedcode April 22, 2009 Python, Linux
Related by category:
'Cache-Control' or Expires September 16, 2005 Zope
To all Zope developers: Does this sound familiar? March 8, 2011 Zope
IssueTrackerProduct now officially abandoned March 30, 2012 Zope
Sending HTML emails in Zope October 26, 2006 Zope
Related by keyword:
Umlauts (non-ascii characters) with git on macOS March 22, 2021 Python, macOS
Zope compared to PHP June 22, 2005 Zope
Emacs html-mode for .zpt and .dtml April 15, 2004 Zope
Entering multilanguage data in Zope November 26, 2006