Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix raising XMLSyntaxError on close #379

Closed
wants to merge 1 commit into from

Conversation

EnricoMi
Copy link

@EnricoMi EnricoMi commented Jul 12, 2023

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.

@scoder scoder closed this in 762f62c Aug 24, 2023
@scoder
Copy link
Member

scoder commented Aug 24, 2023

Thanks for the analysis and the tests. I committed a slightly different implementation but kept your tests as they were.

@EnricoMi
Copy link
Author

Thanks!

@EnricoMi EnricoMi deleted the fix-xmlsyntaxerror-on-close branch August 25, 2023 13:35
scoder added a commit that referenced this pull request Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants