cStringIO unicode weirdness

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Rubin

    #1

    cStringIO unicode weirdness

    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.
    >>import StringIO, cStringIO
    >>StringIO.Stri ngIO('a').getva lue()
    'a'
    >>cStringIO.Str ingIO('a').getv alue()
    'a'
    >>StringIO.Stri ngIO(u'a').getv alue()
    u'a'
    >>cStringIO.Str ingIO(u'a').get value()
    'a\x00\x00\x00'
    >>>
    I would have thought StringIO and cStringIO would return the
    same result for this ascii-encodeable string. Worse:
    >>StringIO.Stri ngIO(u'a').getv alue().encode(' utf-8').decode('utf-8')
    u'a'

    does the right thing, but
    >>cStringIO.Str ingIO(u'a').get value().encode( 'utf-8').decode('utf-8')
    u'a\x00\x00\x00 '

    looks bogus. Am I misunderstandin g something?
  • John Machin

    #2
    Re: cStringIO unicode weirdness

    On Jun 19, 8:56 am, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
    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.
    >>import StringIO, cStringIO
    >>StringIO.Stri ngIO('a').getva lue()
    'a'
    >>cStringIO.Str ingIO('a').getv alue()
    'a'
    >>StringIO.Stri ngIO(u'a').getv alue()
    u'a'
    >>cStringIO.Str ingIO(u'a').get value()
    'a\x00\x00\x00'
    >>>
    >
    I would have thought StringIO and cStringIO would return the
    same result for this ascii-encodeable string.
    Looks like a bug to me.
    Worse:
    >
    >>StringIO.Stri ngIO(u'a').getv alue().encode(' utf-8').decode('utf-8')
    u'a'
    >
    does the right thing, but
    >
    >>cStringIO.Str ingIO(u'a').get value().encode( 'utf-8').decode('utf-8')
    u'a\x00\x00\x00 '
    >
    looks bogus. Am I misunderstandin g 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.
    >>'a\x00\x00\x0 0'.encode('utf-8')
    # IMPLICIT conversion to unicode (effectively .decode('ascii' )), then
    encoding as utf8
    'a\x00\x00\x00' # no change to original buggy result
    >>>
    >>'a\x00\x00\x0 0'.decode('utf-8')
    u'a\x00\x00\x00 ' # as expected
    >>>

    Comment

    • Josiah Carlson

      #3
      Re: cStringIO unicode weirdness

      Paul Rubin wrote:
      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.
      >>import StringIO, cStringIO
      >>StringIO.Stri ngIO('a').getva lue()
      'a'
      >>cStringIO.Str ingIO('a').getv alue()
      'a'
      >>StringIO.Stri ngIO(u'a').getv alue()
      u'a'
      >>cStringIO.Str ingIO(u'a').get value()
      'a\x00\x00\x00'
      >>>
      >
      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...
      >>cs = cStringIO.Strin gIO('a')
      >>cs.write('b ')
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      AttributeError: 'cStringIO.Stri ngI' object has no attribute 'write'
      >>s = StringIO.String IO('a')
      >>s.write('b' )
      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

      Comment

      • Paul Rubin

        #4
        Re: cStringIO unicode weirdness

        Josiah Carlson <josiah.carlson @sbcglobal.netw rites:
        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.

        Comment

        Working...