[mod_python] UnicodeDecodeError with util.FieldStorage(req).Field.value

Martin gzlist at googlemail.com
Wed Jun 27 11:15:43 EDT 2007


On 25/06/07, Anastasios Hatzis <ah at hatzis.de> wrote:
>
> Graham, Martin
>
> Thank you for your help. Charsets and encoding is definitely the most
> difficult thing for me in programming. In case of Python I always expect
> encode() to the opposite of what it is actually doing and vice versa. :/
>
> However, the following worked for me:
>
>         email_body = u'\r\n'
>         for param in store.list:
>             email_body += unicode(param.name, 'utf-8') + ': ' + \
>                               unicode(param.value, 'utf-8') + '\r\n'

You've missed two important things there.

The main one being that the calls to unicode() can throw, and anyone
who posted a form that wasn't perfect utf-8 will get a blank 500
Internal Server Error rather than any indication of what was wrong.
You're also still relying on implicit string<->unicode conversion
there, which makes the intention of the line less clear.

Second, style point really, is that using + for building up
strings/unicode is a bad habit as you pay a not-insignificant
performance penalty with sufficiently large input.

Martin


More information about the Mod_python mailing list