Any way we go, some dependence on the current implementation seems unavoidable
today. It seems the best we can do here, apart from hoping for an improved email
package in a few years’ time, is to specialize text message construction calls by Unicode
type, and assume both that encoding names match those expected by the package and
that message data is valid for the Unicode type selected. Here is the sort of arguably
magic code that the upcoming mailtools package (again in Example 13-23) will apply
to choose text types:
>>> from email.charset import Charset, BASE64, QP
>>> for e in ('us-ascii', 'latin-1', 'utf8', 'latin1', 'ascii'):
... cset = Charset(e)
... benc = cset.body_encoding
... if benc in (None, QP):
... print(e, benc, 'text') # read/fetch data as str
... else:
... print(e, benc, 'binary') # read/fetch data as bytes
...
us-ascii None text
latin-1 1 text
utf8 2 binary
latin1 2 binary
ascii None text
We’ll proceed this way in this book, with the major caveat that this is almost certainly
likely to require changes in the future because of its strong coupling with the current
email implementation.
Late-breaking news: Like the prior section, it now appears that this sec-
tion’s bug will also be fixed in Python 3.2, making the workaround here
unnecessary in this and later Python releases. The nature of the fix is
unknown, though, and we still need the fix for the version of Python
current when this chapter was written. As of just before publication, the
alpha release of 3.2 is still somewhat type specific on this issue, but now
accepts either str or bytes for text that triggers Base64 encodings, in-
stead of just bytes.
Summary: Solutions and workarounds
The email package in Python 3.1 provides powerful tools for parsing and composing
mails, and can be used as the basis for full-featured mail clients like those in this book
with just a few workarounds. As you can see, though, it is less than fully functional
today. Because of that, further specializing code to its current API is perhaps a tempo-
rary solution. Short of writing our own email parser and composer (not a practical
option in a finitely-sized book!), some compromises are in order here. Moreover, the
inherent complexity of Unicode support in email places some limits on how much we
can pursue this thread in this book.
946 | Chapter 13: Client-Side Scripting