[Zope3-Users] Re: UnicodeEncodeErrors from zope/app/maildir.py

Raphael Ritz r.ritz at biologie.hu-berlin.de
Wed Nov 29 08:08:31 EST 2006

Rupert Redington schrieb:

> How is it possible to send emails containing non-ascii encodings from
> zope? Is there a problem with the python smtplib which is forcing this
> behaviour? (Or do I still not get it...?)
[I guess the latter ... :-) ]

Well, I only responsed to the 'pointer to Unicode' part of your question
ignoring the actual problem you are facing. ;-)

But now to this: Almost surely there is an implicit cast of a
byte string to a unicode string involved as it usually happens
when you are trying to combine unicode and bytestrings like here:

[ritz at pitts ~]$ python
Python 2.3.5 (#1, Jun 15 2005, 14:15:25)
[GCC 3.4.3 20050227 (Red Hat 3.4.3-22.fc3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> s = u"höhö"
 >>> s_utf8 = s.encode('utf8')
 >>> s + s_utf8
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: 
ordinal not in range(128)
 >>> s + s_utf8.decode('utf8')

What happens here is that s + s_utf8 concatenates an unicode string 's'
with a byte string 's_utf8' without actually telling Python the
bytestring's encoding. But as such operations result in 's_utf8'
being casted to unicode, Python tries to decode it using its default
encoding which happens to be 'ascii' (don't question this; that has
been a long debate at the time and the only solution that was seen
as "politically correct").

Now, you could change your python's default encoding to something else
(in 'sitecustomize') but this would be more of a workaround than a
solution to your problem.

Better, figure out where you are implicitly mixing string types
and decode byte strings properly yourself.

Hope that's closer to your real question,


> Thanks for your help,
> Rupert
> R

More information about the Zope3-users mailing list