This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: xml.sax.parser() doesn't terminate when given a filename
Type: behavior Stage: resolved
Components: XML Versions: Python 3.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: benjamin.peterson Nosy List: ajaksu2, barry, benjamin.peterson, christian.heimes, mark, python-dev
Priority: release blocker Keywords: patch

Created on 2008-03-28 10:15 by mark, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
xmlreader_buffer.patch christian.heimes, 2008-08-28 18:34
Messages (8)
msg64625 - (view) Author: Mark Summerfield (mark) * Date: 2008-03-28 10:15
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>
msg64626 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-03-28 10:31
I had to disable three unit tests in test_sax. We didn't noticed the
problem before because the tests weren't actually run. The three tests
are marked clearly with XXX and FIXME.
msg72102 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2008-08-28 18:10
ISTM that this release blocker can be solved by changing
xml.sax.xmlreader.py line 122 from:
        while buffer != "":
to
        while buffer != b"":
msg72103 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-08-28 18:34
I've a better idea:


    while buffer: 

It's faster and works for both empty bytes and str.

The patch fixes the issue and re-enables three unit tests.
msg72253 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-09-01 14:34
The patch looks great. (I love enabling disabled tests!)
msg72288 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2008-09-01 20:00
Looks like this is a duplicate of issue3590, so this patch fixes two
release blockers ;)
msg72461 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2008-09-04 02:20
Benjamin will commit this.
msg72463 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-09-04 02:23
Applied in r66203.
History
Date User Action Args
2022-04-11 14:56:32adminsetgithub: 46753
2012-08-24 08:34:14ncoghlansetmessages: - msg168983
2012-08-24 08:33:04python-devsetnosy: + python-dev

messages: + msg168983
stage: resolved
2008-09-04 02:23:01benjamin.petersonsetstatus: open -> closed
resolution: accepted -> fixed
messages: + msg72463
2008-09-04 02:20:52barrysetassignee: benjamin.peterson
resolution: accepted
messages: + msg72461
nosy: + barry
2008-09-01 20:00:14ajaksu2setmessages: + msg72288
2008-09-01 14:34:10benjamin.petersonsetkeywords: - needs review
nosy: + benjamin.peterson
messages: + msg72253
2008-08-28 18:34:15christian.heimessetkeywords: + needs review, patch
files: + xmlreader_buffer.patch
messages: + msg72103
2008-08-28 18:10:06ajaksu2setnosy: + ajaksu2
messages: + msg72102
2008-08-21 14:55:18benjamin.petersonsetpriority: critical -> release blocker
2008-03-28 10:31:49christian.heimessetpriority: critical
nosy: + christian.heimes
messages: + msg64626
2008-03-28 10:15:13markcreate