[Python-Dev] Re: [XML-SIG] printing Unicode xml to StringIO

Martin v. Loewis martin@v.loewis.de
Fri, 28 Dec 2001 11:13:17 +0100


[Over to python-dev. Jaco noticed that writing Unicode objects to
 a StringIO object stopped working in 2.2, see
http://mail.python.org/pipermail/xml-sig/2001-December/006891.html
]

Marc-Andre writes
> Actually, I think that this is a bug in the documentation, not the
> code. StringIO and cStringIO were never meant to work on anything but
> strings and memory buffers. 

IMO, "strings" should include both byte strings and Unicode strings.
Mixing them may not be allowed, but that is a different story. 

In fact, there is an open bug (#216388) that cStringIO rejects Unicode
objects. If that gets fixed, we get the funny scenario that StringIO
rejects Unicode object, whereas cStringIO accepts them.

> The note that Fred added to the docs about StringIO's capability of
> storing Unicode in it's buffer list is simply an artifact of the
> implementation.

There are many developers who take this note literally. Claiming that
this was not intentional is a mistake.

> Please use the .encode() method on Unicode objects before writing
> them to a StringIO object.

If you want to end up with a byte string, this is a good idea. But I
think it is pointless to require encoding them when you want to end up
with a Unicode string; you'd have to invoke unicode() on the result,
for no apparent reason but a bug in the StringIO implementation.

Regards,
Martin