scoder · GitHub

Parsing incomplete XML raises an XMLSyntaxError. But if a bespoke target is used, it fails to instantiate another error:

from lxml import etree
parser = etree.XMLParser(target=etree.TreeBuilder())
etree.fromstring('<a><b></b>', parser=parser)
TypeError: __init__() takes at least 5 positional arguments (2 given)

The following raises an error correctly:

parser = etree.XMLParser()
etree.fromstring('<a><b></b>', parser=parser)
lxml.etree.XMLSyntaxError: Premature end of data in tag a line 1, line 1, column 11

It looks like TreeBuilder.close() calls (https://github.com/lxml/lxml/blob/master/src/lxml/saxparser.pxi#L820):

raise XMLSyntaxAssertionError("missing end tags")

which calls the XMLSyntaxError constructor with only two arguments (self and message) and not the expected 5.

This issue also surfaces when any error is raised while processing the XML, which finally closes the TreeBuilder and throws another exception, because the processed XML has unclosed tags (as processing is half-way done).

Fixes https://bugs.launchpad.net/lxml/+bug/1980767.

Read the original on github.com ↗