[Twisted-Python] twisted.internet.abstract.FileDescription.write vs unicode

Jeff Bowden jlb at houseofdistraction.com
Tue Jul 13 00:50:43 EDT 2004

Mary Gardiner wrote:

>On Mon, Jul 12, 2004, Jeff Bowden wrote:
>>Is there a good reason for 
>>twisted.internet.abstract.FileDescription.write to require 
>>isinstance(data, str) rather than also allowing isinstance(type, unicode)??
>This is one of those rare things, an FAQ that really is a "Frequently
>Asked Question" not a "Question We Wish You'd Ask".
>Unfortunately, I haven't actually added it to the FAQs yet :( See the
>discussion at
>http://www.twistedmatrix.com/users/roundup.twistd/twisted/issue617, in
>particular Glyph Lefkowitz's comment:
>    "Q. Why doesn't {API X} accept both string objects and unicode
>    objects?  Isn't it better to use unicode so you can support
>    internationalization?
>    A. Unicode is for talking about strings of human-readable text.
>    String objects can also be used for this purpose, and when they are,
>    it is better to use unicode, you are correct.  However, {API X} is
>    dealing with raw data, probably coming from a network connection,
>    and is using String objects as containers of sequences of bytes.
>    Unicode has no way of representing sequences of bytes and streams of
>    'raw', unparsed data.  The data has to be translated at some level
>    *above* that, in order to get things like unicode character
>    alignment correct.
>    For more information and some idea of the complexity involved, read
>    http://www.sidhe.org/~dan/blog/archives/000255.html and
>    http://www.joelonsoftware.com/articles/Unicode.html"

Right, so I worked around this problem by calling .encode('utf8') in all 
the places where strings go out (after making appropriate changes to the 

It wasn't that complicated but it was a PITA and it will be an ongoing 
maintenance headache.  It would be a lot nicer if the framework dealt 
with it transparently.

FileDescriptor.write does seem like the wrong place to handle it even 
though that's where the error message pops out.  Apparently what's 
needed is another layer on top of the http layer.   Has anyone attempted 
to write one?

More information about the Twisted-Python mailing list