Skip to content

xml.sax.parser() doesn't terminate when given a filename #46753

Closed

Description

@mark-summerfield
BPO 2501
Nosy @warsaw, @tiran, @devdanzin, @mark-summerfield, @benjaminp
Files
  • xmlreader_buffer.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/benjaminp'
    closed_at = <Date 2008-09-04.02:23:01.849>
    created_at = <Date 2008-03-28.10:15:13.330>
    labels = ['expert-XML', 'type-bug', 'release-blocker']
    title = "xml.sax.parser() doesn't terminate when given a filename"
    updated_at = <Date 2012-08-24.08:34:14.926>
    user = 'https://github.com/mark-summerfield'

    bugs.python.org fields:

    activity = <Date 2012-08-24.08:34:14.926>
    actor = 'ncoghlan'
    assignee = 'benjamin.peterson'
    closed = True
    closed_date = <Date 2008-09-04.02:23:01.849>
    closer = 'benjamin.peterson'
    components = ['XML']
    creation = <Date 2008-03-28.10:15:13.330>
    creator = 'mark'
    dependencies = []
    files = ['11295']
    hgrepos = []
    issue_num = 2501
    keywords = ['patch']
    message_count = 8.0
    messages = ['64625', '64626', '72102', '72103', '72253', '72288', '72461', '72463']
    nosy_count = 6.0
    nosy_names = ['barry', 'christian.heimes', 'ajaksu2', 'mark', 'benjamin.peterson', 'python-dev']
    pr_nums = []
    priority = 'release blocker'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue2501'
    versions = ['Python 3.0']

    Activity

    1. mark-summerfield commented on Mar 28, 2008

      mark-summerfieldmannequin
      MannequinAuthor

      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>

    2. tiran commented on Mar 28, 2008

      @tiran
      Member

      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.

    3. devdanzin commented on Aug 28, 2008

      devdanzinmannequin
      Mannequin

      ISTM that this release blocker can be solved by changing
      xml.sax.xmlreader.py line 122 from:
      while buffer != "":
      to
      while buffer != b"":

    4. tiran commented on Aug 28, 2008

      @tiran
      Member

      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.

    5. benjaminp commented on Sep 1, 2008

      @benjaminp
      Contributor

      The patch looks great. (I love enabling disabled tests!)

    6. devdanzin commented on Sep 1, 2008

      devdanzinmannequin
      Mannequin

      Looks like this is a duplicate of bpo-3590, so this patch fixes two
      release blockers ;)

    7. warsaw commented on Sep 4, 2008

      @warsaw
      Member

      Benjamin will commit this.

    8. benjaminp commented on Sep 4, 2008

      @benjaminp
      Contributor

      Applied in r66203.

    9. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions