[New-bugs-announce] [issue2501] xml.sax.parser() doesn't terminate when given a filename

Mark Summerfield report at bugs.python.org
Fri Mar 28 11:15:13 CET 2008


New submission from Mark Summerfield <mark at qtrac.eu>:

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>

----------
components: XML
messages: 64625
nosy: mark
severity: normal
status: open
title: xml.sax.parser() doesn't terminate when given a filename
type: behavior
versions: Python 3.0

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2501>
__________________________________


More information about the New-bugs-announce mailing list