Any reason why cStringIO in 2.5 behaves different from 2.4?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Scholl

    Any reason why cStringIO in 2.5 behaves different from 2.4?

    After an hour searching for a potential bug in XML parsing
    (PyXML), after updating from 2.4 to 2.5, I found this one:


    $ python2.5
    Python 2.5 (release25-maint, Dec 9 2006, 14:35:53)
    [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-20)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>import StringIO
    >>x = StringIO.String IO(u"m\xf6p")
    >>import cStringIO
    >>x = cStringIO.Strin gIO(u"m\xf6p")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    UnicodeEncodeEr ror: 'ascii' codec can't encode character u'\xf6' in position 1: ordinal not in range(128)
    >>>
    $ python
    Python 2.4.4 (#2, Apr 5 2007, 20:11:18)
    [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>import StringIO
    >>x = StringIO.String IO(u"m\xf6p")
    >>import cStringIO
    >>x = cStringIO.Strin gIO(u"m\xf6p")
    >>>

    OK, that's why my code was fine with Python 2.4 and breaks with
    2.5.

    {sigh}


    --
    Web (en): http://www.no-spoon.de/ -*- Web (de): http://www.frell.de/
  • Stefan Behnel

    #2
    Re: Any reason why cStringIO in 2.5 behaves different from 2.4?

    Stefan Scholl wrote:
    After an hour searching for a potential bug in XML parsing
    (PyXML), after updating from 2.4 to 2.5, I found this one:
    >
    $ python2.5
    Python 2.5 (release25-maint, Dec 9 2006, 14:35:53)
    [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-20)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>>import StringIO
    >>>x = StringIO.String IO(u"m\xf6p")
    >>>import cStringIO
    >>>x = cStringIO.Strin gIO(u"m\xf6p")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    UnicodeEncodeEr ror: 'ascii' codec can't encode character u'\xf6' in position 1: ordinal not in range(128)
    $ python
    Python 2.4.4 (#2, Apr 5 2007, 20:11:18)
    [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>>import StringIO
    >>>x = StringIO.String IO(u"m\xf6p")
    >>>import cStringIO
    >>>x = cStringIO.Strin gIO(u"m\xf6p")
    >
    >
    OK, that's why my code was fine with Python 2.4 and breaks with
    2.5.
    It wasn't fine with 2.4 either:

    """
    Unlike the memory files implemented by the StringIO module, those provided by
    this module are not able to accept Unicode strings that cannot be encoded as
    plain ASCII strings.
    """



    Read the docs...

    Stefan

    Comment

    • Stefan Scholl

      #3
      Re: Any reason why cStringIO in 2.5 behaves different from 2.4?

      Stefan Behnel <stefan.behne l-n05pAM@web.dewr ote:
      Stefan Scholl wrote:
      >After an hour searching for a potential bug in XML parsing
      >(PyXML), after updating from 2.4 to 2.5, I found this one:
      >>
      >$ python2.5
      >Python 2.5 (release25-maint, Dec 9 2006, 14:35:53)
      >[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-20)] on linux2
      >Type "help", "copyright" , "credits" or "license" for more information.
      >>>>import StringIO
      >>>>x = StringIO.String IO(u"m\xf6p")
      >>>>import cStringIO
      >>>>x = cStringIO.Strin gIO(u"m\xf6p")
      >Traceback (most recent call last):
      > File "<stdin>", line 1, in <module>
      >UnicodeEncodeE rror: 'ascii' codec can't encode character u'\xf6' in position 1: ordinal not in range(128)
      >$ python
      >Python 2.4.4 (#2, Apr 5 2007, 20:11:18)
      >[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
      >Type "help", "copyright" , "credits" or "license" for more information.
      >>>>import StringIO
      >>>>x = StringIO.String IO(u"m\xf6p")
      >>>>import cStringIO
      >>>>x = cStringIO.Strin gIO(u"m\xf6p")
      >>
      >>
      >OK, that's why my code was fine with Python 2.4 and breaks with
      >2.5.
      >
      It wasn't fine with 2.4 either:
      Worked in my test, a few lines above ...
      >
      """
      Unlike the memory files implemented by the StringIO module, those provided by
      this module are not able to accept Unicode strings that cannot be encoded as
      plain ASCII strings.
      """
      >

      >
      Read the docs...
      Well, http://docs.python.org/lib/module-xml.sax.html is missing
      the fact, that I can't use Unicode with parseString().

      This parseString() uses cStringIO.



      --
      Web (en): http://www.no-spoon.de/ -*- Web (de): http://www.frell.de/

      Comment

      • Stefan Behnel

        #4
        Re: Any reason why cStringIO in 2.5 behaves different from 2.4?

        Stefan Scholl wrote:
        Well, http://docs.python.org/lib/module-xml.sax.html is missing
        the fact, that I can't use Unicode with parseString().
        >
        This parseString() uses cStringIO.
        Well, Python unicode is not a valid *byte* encoding for XML.

        lxml.etree can parse unicode, if you really want, but otherwise, you should
        maybe stick to well-formed XML.

        Stefan

        Comment

        • Stefan Scholl

          #5
          Re: Any reason why cStringIO in 2.5 behaves different from 2.4?

          Stefan Behnel <stefan.behne l-n05pAM@web.dewr ote:
          Stefan Scholl wrote:
          >Well, http://docs.python.org/lib/module-xml.sax.html is missing
          >the fact, that I can't use Unicode with parseString().
          >>
          >This parseString() uses cStringIO.
          >
          Well, Python unicode is not a valid *byte* encoding for XML.
          >
          lxml.etree can parse unicode, if you really want, but otherwise, you should
          maybe stick to well-formed XML.
          The XML is well-formed. Works perfect in Python 2.4 with Python
          unicode and Python sax parser.

          This stays all inside Python.


          Comment

          • Stefan Behnel

            #6
            Re: Any reason why cStringIO in 2.5 behaves different from 2.4?

            Stefan Scholl wrote:
            Stefan Behnel <stefan.behne l-n05pAM@web.dewr ote:
            >Stefan Scholl wrote:
            >>Well, http://docs.python.org/lib/module-xml.sax.html is missing
            >>the fact, that I can't use Unicode with parseString().
            >>>
            >>This parseString() uses cStringIO.
            >Well, Python unicode is not a valid *byte* encoding for XML.
            >>
            >lxml.etree can parse unicode, if you really want, but otherwise, you should
            >maybe stick to well-formed XML.
            >
            The XML is well-formed. Works perfect in Python 2.4 with Python
            unicode and Python sax parser.
            The XML is *not* well-formed if you pass Python unicode instead of a byte
            encoded string. Read the XML spec.

            It would be well-formed if you added the proper XML declaration, but that is
            system specific (UCS-4 or UTF-16, BE or LE). So don't even try.

            Stefan

            Comment

            • Stefan Scholl

              #7
              Re: Any reason why cStringIO in 2.5 behaves different from 2.4?

              Stefan Behnel <stefan.behne l-n05pAM@web.dewr ote:
              Stefan Scholl wrote:
              >Stefan Behnel <stefan.behne l-n05pAM@web.dewr ote:
              >>Stefan Scholl wrote:
              >>>Well, http://docs.python.org/lib/module-xml.sax.html is missing
              >>>the fact, that I can't use Unicode with parseString().
              >>>>
              >>>This parseString() uses cStringIO.
              >>Well, Python unicode is not a valid *byte* encoding for XML.
              >>>
              >>lxml.etree can parse unicode, if you really want, but otherwise, you should
              >>maybe stick to well-formed XML.
              >>
              >The XML is well-formed. Works perfect in Python 2.4 with Python
              >unicode and Python sax parser.
              >
              The XML is *not* well-formed if you pass Python unicode instead of a byte
              encoded string. Read the XML spec.
              >
              It would be well-formed if you added the proper XML declaration, but that is
              system specific (UCS-4 or UTF-16, BE or LE). So don't even try.
              Who cares? I'm not calling any external tools.

              Python should know its own strings.

              Comment

              • Stefan Behnel

                #8
                Re: Any reason why cStringIO in 2.5 behaves different from 2.4?

                Stefan Scholl wrote:
                Stefan Behnel <stefan.behne l-n05pAM@web.dewr ote:
                >Stefan Scholl wrote:
                >>Stefan Behnel <stefan.behne l-n05pAM@web.dewr ote:
                >>>Stefan Scholl wrote:
                >>>>Well, http://docs.python.org/lib/module-xml.sax.html is missing
                >>>>the fact, that I can't use Unicode with parseString().
                >>>>>
                >>>>This parseString() uses cStringIO.
                >>>Well, Python unicode is not a valid *byte* encoding for XML.
                >>>>
                >>>lxml.etree can parse unicode, if you really want, but otherwise, you should
                >>>maybe stick to well-formed XML.
                >>The XML is well-formed. Works perfect in Python 2.4 with Python
                >>unicode and Python sax parser.
                >The XML is *not* well-formed if you pass Python unicode instead of a byte
                >encoded string. Read the XML spec.
                >>
                >It would be well-formed if you added the proper XML declaration, but that is
                >system specific (UCS-4 or UTF-16, BE or LE). So don't even try.
                >
                Who cares? I'm not calling any external tools.
                XML cares. If you want to work with something that is not XML, do not expect
                XML tools to help you do it. XML tools work with XML, and there is a spec that
                says what XML is. Your string is not XML.

                Stefan

                Comment

                • Stefan Scholl

                  #9
                  Re: Any reason why cStringIO in 2.5 behaves different from 2.4?

                  Stefan Behnel <stefan.behne l-n05pAM@web.dewr ote:
                  Stefan Scholl wrote:
                  >Stefan Behnel <stefan.behne l-n05pAM@web.dewr ote:
                  >>Stefan Scholl wrote:
                  >>>Stefan Behnel <stefan.behne l-n05pAM@web.dewr ote:
                  >>>>Stefan Scholl wrote:
                  >>>>>Well, http://docs.python.org/lib/module-xml.sax.html is missing
                  >>>>>the fact, that I can't use Unicode with parseString().
                  >>>>>>
                  >>>>>This parseString() uses cStringIO.
                  >>>>Well, Python unicode is not a valid *byte* encoding for XML.
                  >>>>>
                  >>>>lxml.etre e can parse unicode, if you really want, but otherwise, you should
                  >>>>maybe stick to well-formed XML.
                  >>>The XML is well-formed. Works perfect in Python 2.4 with Python
                  >>>unicode and Python sax parser.
                  >>The XML is *not* well-formed if you pass Python unicode instead of a byte
                  >>encoded string. Read the XML spec.
                  >>>
                  >>It would be well-formed if you added the proper XML declaration, but that is
                  >>system specific (UCS-4 or UTF-16, BE or LE). So don't even try.
                  >>
                  >Who cares? I'm not calling any external tools.
                  >
                  XML cares. If you want to work with something that is not XML, do not expect
                  XML tools to help you do it. XML tools work with XML, and there is a spec that
                  says what XML is. Your string is not XML.
                  This isn't some sophisticated XML tool that tells me the string
                  is wrong. It's a changed behavior of cStringIO that throws an
                  exception. While I'm just using the method parseString() of
                  xml.sax.

                  We both repeat ourselves. I don't think this thread brings
                  something new.


                  I'm all for correct XML and hate XML bozos. But there are limits
                  you have to learn after a few years.

                  Comment

                  • Stefan Scholl

                    #10
                    Re: Any reason why cStringIO in 2.5 behaves different from 2.4?

                    Stefan Behnel <stefan.behne l-n05pAM@web.dewr ote:
                    The XML is *not* well-formed if you pass Python unicode instead of a byte
                    encoded string. Read the XML spec.
                    Pointers, please.

                    Last time I read that part of the spec was when a customer's
                    consulting company switched to ISO-8859-15 without saying
                    something beforehand. The old code (PHP) I have to maintain
                    couldn't deal with it.

                    It was wrong to switch encoding without telling somebody about
                    it. And a XML processor isn't required to support ISO-8859-15.
                    But I thought it was too embarrassing not to support this
                    encoding. I fixed that part without making a fuss.


                    A Python XML processor that can't handle the own encoding is
                    embarrassing. It isn't required to support it. It would be OK if
                    it wouldn't support UTF-7. But a parseString() method that
                    doesn't want Python strings? No way!


                    --
                    Web (en): http://www.no-spoon.de/ -*- Web (de): http://www.frell.de/

                    Comment

                    • Stefan Scholl

                      #11
                      Re: Any reason why cStringIO in 2.5 behaves different from 2.4?

                      Chris Mellon <arkanes@gmail. comwrote:
                      On 7/28/07, Stefan Scholl <stesch@no-spoon.dewrote:
                      >Just checked on a system without PyXML: xml/sax/__init__.py
                      >defines parseString() and uses cStringIO (when available).
                      >>
                      >Python 2.5.1
                      >>
                      >
                      Yes, thats the fixed bug. After all this you still do not seem to be
                      clear on what the bug is. The bug is not that your code fails in 2.5,
                      it's that it worked at all in 2.4.
                      Don't let the subject line fool you. I'm OK with cStringIO. The
                      thread is now about xml.sax's parseString().


                      --
                      Web (en): http://www.no-spoon.de/ -*- Web (de): http://www.frell.de/

                      Comment

                      • Stefan Behnel

                        #12
                        Re: Any reason why cStringIO in 2.5 behaves different from 2.4?

                        Stefan Scholl wrote:
                        Chris Mellon <arkanes@gmail. comwrote:
                        >On 7/28/07, Stefan Scholl <stesch@no-spoon.dewrote:
                        >>Just checked on a system without PyXML: xml/sax/__init__.py
                        >>defines parseString() and uses cStringIO (when available).
                        >>>
                        >>Python 2.5.1
                        >>>
                        >Yes, thats the fixed bug. After all this you still do not seem to be
                        >clear on what the bug is. The bug is not that your code fails in 2.5,
                        >it's that it worked at all in 2.4.
                        >
                        Don't let the subject line fool you. I'm OK with cStringIO. The
                        thread is now about xml.sax's parseString().
                        .... which works correctly with Python 2.5 and was broken before.

                        Stefan

                        Comment

                        • Michael L Torrie

                          #13
                          Re: Any reason why cStringIO in 2.5 behaves different from 2.4?

                          Stefan Scholl wrote:
                          Don't let the subject line fool you. I'm OK with cStringIO. The
                          thread is now about xml.sax's parseString().
                          Giving you the benefit of the doubt here, despite the fact that Stefan
                          Behnel has state this over and over again and you just haven't listened.

                          xml.sax's use of parseString() is exactly correct. xml.sax should
                          *never* parse python unicode strings as by definition XML must be
                          encoded as a *byte stream*, which is what a python string is.

                          A python /unicode/ string could be held internally in any number of
                          ways, 2, 3, 4, or even 8 bytes per character if the implementation
                          demanded it (a bit contrived, I admit). Since the xml parser is only
                          ever intended to parse *XML*, why should it ever know what to do with
                          python unicode strings, which could be stored any number of ways, making
                          byte-parsing impossible.

                          So your code is faulty in its assumptions, not xml.sax.
                          >
                          >

                          Comment

                          Working...