Skip to content

ExpatParser raise exception during close action #138371

@yabb85

Description

@yabb85

Hi,

I try to understand why ExpatParser raise exception during close action.
I create a xml parser with asyncio protocol to parse xmpp message.

class XMLProtocol(asyncio.Protocol):

during connection_made call I create parser and attach handler:

self._parser = make_parser()                            
self._handler = XMLHandler(self._transport, self._bunny)
self._parser.setContentHandler(self._handler)           

In data_received function I parse data on close document:

def data_received(self, data):
    try:
        data = data.replace(b"<?xml version='1.0' encoding='UTF-8'?>", b"").decode().strip()
        self._parser.feed(data)
        self._parser.close()
        self._parser.setContentHandler(self._handler)
        self._parser.reset()
    except UnicodeDecodeError:
        pass

But the close() call raise exception:

xml.sax._exceptions.SAXParseException: <unknown>:1:109: no element found

I check with pdb and the issue come from:

self.feed(b"", isFinal=True)

The following line not support empty data and isFinal at the same time:

self._parser.Parse(data, isFinal)

In my debugger I can see the following values for this line:

(Pdb) self._parser
<pyexpat.xmlparser object at 0x7f5041bfffa0>

(Pdb) self._parser.Parse
<built-in method Parse of pyexpat.xmlparser object at 0x7f5041bfffa0>

(Pdb) data
b''
(Pdb) isFinal
True

CPython versions tested on:
3.11.2

Operating systems tested on:
Linux (debian)

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions