473,221 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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.StringIO(u"m\xf6p")
import cStringIO
x = cStringIO.StringIO(u"m\xf6p")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: '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.StringIO(u"m\xf6p")
import cStringIO
x = cStringIO.StringIO(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/
Jul 26 '07 #1
12 1625
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.StringIO(u"m\xf6p")
import cStringIO
x = cStringIO.StringIO(u"m\xf6p")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: '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.StringIO(u"m\xf6p")
import cStringIO
x = cStringIO.StringIO(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.
"""

http://docs.python.org/lib/module-cStringIO.html

Read the docs...

Stefan
Jul 26 '07 #2
Stefan Behnel <st******************@web.dewrote:
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.StringIO(u"m\xf6p")
import cStringIO
x = cStringIO.StringIO(u"m\xf6p")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: '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.StringIO(u"m\xf6p")
import cStringIO
x = cStringIO.StringIO(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.
"""

http://docs.python.org/lib/module-cStringIO.html

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/
Jul 26 '07 #3
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
Jul 26 '07 #4
Stefan Behnel <st******************@web.dewrote:
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.
Jul 26 '07 #5
Stefan Scholl wrote:
Stefan Behnel <st******************@web.dewrote:
>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
Jul 26 '07 #6
Stefan Behnel <st******************@web.dewrote:
Stefan Scholl wrote:
>Stefan Behnel <st******************@web.dewrote:
>>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.

Jul 26 '07 #7
Stefan Scholl wrote:
Stefan Behnel <st******************@web.dewrote:
>Stefan Scholl wrote:
>>Stefan Behnel <st******************@web.dewrote:
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
Jul 26 '07 #8
Stefan Behnel <st******************@web.dewrote:
Stefan Scholl wrote:
>Stefan Behnel <st******************@web.dewrote:
>>Stefan Scholl wrote:
Stefan Behnel <st******************@web.dewrote:
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.
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.

Jul 26 '07 #9
Stefan Behnel <st******************@web.dewrote:
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/
Jul 26 '07 #10
Chris Mellon <ar*****@gmail.comwrote:
On 7/28/07, Stefan Scholl <st****@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/
Jul 28 '07 #11
Stefan Scholl wrote:
Chris Mellon <ar*****@gmail.comwrote:
>On 7/28/07, Stefan Scholl <st****@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
Jul 28 '07 #12
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.
>
Jul 28 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Scott Matthews | last post by:
I've recently come upon an odd Javascript (and/or browser) behavior, and after hunting around the Web I still can't seem to find an answer. Specifically, I have noticed that the Javascript...
3
by: Laszlo Nagy | last post by:
This program: import sys import traceback import cStringIO a = 1.0 b = 0.0 try: c=a/b
1
by: garyjefferson123 | last post by:
I want to accept a cStringIO object in a function in a python extension module. How do I do this? e.g., static PyObject *myfunc(PyObject *self, PyObject *args) { PyObject *cstringio; if...
3
by: =?ISO-8859-1?Q?Markus_Sch=F6pflin?= | last post by:
Hello, I just stumbled accross a difference between cStringIO in Python 2.4 and 2.5. You can no longer feed arrays to cStringIO. Python 2.4: ---%<--- ActivePython 2.4.3 Build 12...
3
by: Paul Rubin | last post by:
Python 2.5 (r25:51908, Oct 6 2006, 15:24:43) on linux2 Type "help", "copyright", "credits" or "license" for more information. 'a' 'a' u'a' 'a\x00\x00\x00' I would have thought StringIO and...
1
by: grbgooglefan | last post by:
I am importing cStringIO module in my PythonC++ embedded program. The import is failing with the following error: ImportError: /usr/lib/python2.3/lib-dynload/cStringIO.so: undefined symbol:...
1
by: Borse, Ganesh | last post by:
Hi, Can you please guide me for the following problem? The call to "PyImport_ImportModule("cStringIO");" is failing with an error of "undefined symbol: PyObject_SelfIter". Before importing this...
1
by: grbgooglefan | last post by:
I am in a perculiar situation. I want to use PyRun_SimpleString for creating Python functions in embedded Python in C++. But there could be cases when Python function code compilation could fail &...
5
by: peppergrower | last post by:
I've been experimenting with the 'with' statement (in __future__), and so far I like it. However, I can't get it to work with a cStringIO object. Here's a minimum working example: ### from...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.