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

James Y Knight foom at fuhm.net
Mon Jul 12 18:48:29 EDT 2004

Yes. Sockets can only transmit and files can only store bytes, not 
characters. The python 'str' unfortunately does double-duty as a 
byte-string and a character string limited to ASCII and/or ISO8859-1 
characters, which often causes confusion among users.

The python 'unicode' is a character string, and thus has no place being 
written to a socket/file. If you want to write unicode data, first 
convert to the appropriate byte encoding, using .encode(encname). I'd 
suggest something like "UTF-8" or, perhaps you'd prefer "UTF-16LE". Or 
maybe something else...whatever your app requires.


On Jul 12, 2004, at 6:30 PM, 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)??

More information about the Twisted-Python mailing list