[Python-Dev] Printing and __unicode__

M.-A. Lemburg mal@lemburg.com
Wed, 13 Nov 2002 19:50:24 +0100


Martin v. Loewis wrote:
> "M.-A. Lemburg" <mal@lemburg.com> writes:
> 
> 
>>>In case the stream is "natively" Unicode (i.e. doesn't ever convert to
>>>byte strings), setting encoding to None should be allowed (this
>>>actually indicates that StringIO should have the encoding attribute).
>>
>>-1
>>
>>The presence of .encoding should indicate that it is
>>safe to write Unicode objects to .write(). Let the stream
>>decide what to do with the Unicode object (e.g. it would
>>probably encode the Unicode object using the .encoding
>>and only then write it to the outside world).
> 
> 
> So should StringIO object have an .encoding attribute or not?
> 
> If not, should
> 
> f = StringIO.StringIO()
> print >>f,x
> 
> try to invoke Unicode conversion or not? 

StringIO should be considered a non-Unicode aware stream,
so it should not implement .encoding. Instead, PyFile_WriteObject()
will simply call __str__ on the Unicode object and thus use
the default encoding for conversion (this is what StringIO
does currently).

If somebody wants to use a StringIO object as Unicode aware
stream, the tools in codecs.py can be used for this (basically
by doing the same kind of wrapping as codecs.open() does).

> If it should, how should it
> find out that this is safe to do?

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
_______________________________________________________________________
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting:                               http://www.egenix.com/
Python Software:                    http://www.egenix.com/files/python/