cStringIO unicode weirdness

P

Paul Rubin

Python 2.5 (r25:51908, Oct 6 2006, 15:24:43)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
I would have thought StringIO and cStringIO would return the
same result for this ascii-encodeable string. Worse:
u'a'

does the right thing, but
u'a\x00\x00\x00'

looks bogus. Am I misunderstanding something?
 
J

John Machin

Python 2.5 (r25:51908, Oct 6 2006, 15:24:43)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
I would have thought StringIO and cStringIO would return the
same result for this ascii-encodeable string.

Looks like a bug to me.
Worse:

u'a'

does the right thing, but

u'a\x00\x00\x00'

looks bogus. Am I misunderstanding something?

Not worse, no more bogus than before. Note that an explicit design
feature of utf8 is that ASCII characters (ord(c) < 128) are unchanged
by the transformation.
# IMPLICIT conversion to unicode (effectively .decode('ascii')), then
encoding as utf8
'a\x00\x00\x00' # no change to original buggy result
 
J

Josiah Carlson

Paul said:
Python 2.5 (r25:51908, Oct 6 2006, 15:24:43)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
I would have thought StringIO and cStringIO would return the
same result for this ascii-encodeable string. Worse:

You would be wrong. The behavior of StringIO and cStringIO are
different under certain circumstances, and those differences are
intended. Among them is when they are confronted with unicode, as you
saw. Another is when provided with an initializer...
Traceback (most recent call last):

There is a summer of code project that is working towards making them
behave the same, but the results will need to wait until Python 2.6
and/or 3.0 . Note that there are a few "closed, won't fix" bug reports
regarding these exact same issues in the Python bug tracker at sourceforge.

- Josiah
 
P

Paul Rubin

Josiah Carlson said:
You would be wrong. The behavior of StringIO and cStringIO are
different under certain circumstances, and those differences are
intended. Among them is when they are confronted with unicode, as you
saw. Another is when provided with an initializer...

The doc says there's only supposed to be a difference if the unicode
can't be represented as ascii. That is not the case with the example
I posted.
There is a summer of code project that is working towards making them
behave the same, but the results will need to wait until Python 2.6
and/or 3.0 . Note that there are a few "closed, won't fix" bug
reports regarding these exact same issues in the Python bug tracker at
sourceforge.

Thanks, this helps. At minimum the 2.5 docs should be updated to
explain the issues.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top