[XML-SIG] printing Unicode xml to StringIO

J.R. van Ossenbruggen Jacco.van.Ossenbruggen@cwi.nl
Fri, 28 Dec 2001 11:50:34 +0100 (CET)


Marc-Andre Lemburg wrote:
> 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. 
> 
> 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.
> 
> Please use the .encode() method on Unicode objects before writing
> them to a StringIO object.

I still think the change in StringIO behavior from 2.1 to 2.2 is a 
bit weird.
But you are right about the ability of StringIO to handle encoded
Unicode.  In fact, the code in xml.dom.ext.Print seems to do the
right encoding before writing to the stream.  

Martin v. Loewis wrote:
> P.S. "unicode string" != "utf-8 string"

That was, actually, the error in my head that messed everything up.  
I was printing to StringIO using "utf-8" encoding, expecting to get
a "unicode string" back. Doing a decode('utf-8') on the value returned
by StringIO.getvalue() (this decode _does_ give me the unicode string 
back, right?) seems to solve my problems.  Thanks for pointing this 
out guys, it would have kept me busy for a quite while otherwise!

Jacco

PS: Martin, also thanks for taking this to python-dev.  I'm not sure
whether it still makes sense to file a bug report at SF?