Message64625
The tiny program at the end of this message runs under Python 2.5 &
30a3. Under 2 it gives the following output:
: python sax.py test.xml
('+', u'document')
('+', u'outer')
('+', u'inner')
('-', u'inner')
('-', u'outer')
('-', u'document')
Done
Under 3 it does not terminate:
: python3 sax.py test.xml
+ document
+ outer
+ inner
- inner
- outer
- document
Traceback (most recent call last):
File "sax.py", line 19, in <module>
parser.parse(sys.argv[1])
File "/home/mark/opt/python30a3/lib/python3.0/xml/sax/expatreader.py",
line 107, in parse
xmlreader.IncrementalParser.parse(self, source)
File "/home/mark/opt/python30a3/lib/python3.0/xml/sax/xmlreader.py",
line 124, in parse
buffer = file.read(self._bufsize)
File "/home/mark/opt/python30a3/lib/python3.0/io.py", line 774, in read
current = self.raw.read(to_read)
KeyboardInterrupt
The xml.sax.parser() function seems to work fine if you give it an open
file object and close the file after the call. But the documentation
says you can give it a filename, but if you do that the parser does not
terminate in Python 3 although it works fine in Python 2.
# sax.py
import sys
import xml.sax
BUG = True
class SaxHandler(xml.sax.handler.ContentHandler):
def startElement(self, name, attributes):
print("+", name)
def endElement(self, name):
print("-", name)
handler = SaxHandler()
parser = xml.sax.make_parser()
parser.setContentHandler(handler)
if BUG:
parser.parse(sys.argv[1])
else:
fh = open(sys.argv[1], encoding="utf8")
parser.parse(fh)
fh.close()
print("Done")
# end of sax.py
Here is the test file:
<?xml version="1.0" encoding="UTF-8"?>
<document>
<outer>
<inner>
</inner>
</outer>
</document> |
|
Date |
User |
Action |
Args |
2008-03-28 10:15:14 | mark | set | spambayes_score: 0.269788 -> 0.2697877 recipients:
+ mark |
2008-03-28 10:15:13 | mark | set | spambayes_score: 0.269788 -> 0.269788 messageid: <1206699313.91.0.35803015757.issue2501@psf.upfronthosting.co.za> |
2008-03-28 10:15:13 | mark | link | issue2501 messages |
2008-03-28 10:15:12 | mark | create | |
|