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

Generate better stack traces for XML errors in included files #2364

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion pretext/pretext.py
Original file line number Diff line number Diff line change
Expand Up @@ -4070,7 +4070,25 @@ def xsltproc(xsl, xml, result, output_dir=None, stringparams={}):
# lxml.etree.XMLSyntaxError: Excessive depth in document: 256 use XML_PARSE_HUGE option
huge_parser = ET.XMLParser(huge_tree=True)
src_tree = ET.parse(xml, parser=huge_parser)
src_tree.xinclude()
try:
src_tree.xinclude()
except ET.XIncludeError as e:
# xinclude() does not show what file a parsing error occured in
# So if there was an error, build a custom loader and redo with ElementInclude
# which will include the file name in the stack dump.
# ElementInclude is a limited version of xinclude(), so can't rely
# on it for the real include process.

# Generate custom loader
from lxml import ElementInclude
def my_loader(href, parse, encoding=None, parser=None):
ret = ElementInclude._lxml_default_loader(href, parse, encoding, parser)
return ret

# Reparse the tree (was modified in try clause) and run ElementInclude
# This should also fail, but will give a better error message
src_tree = ET.parse(xml, parser=huge_parser)
ElementInclude.include(src_tree, loader=my_loader, max_depth=100)

# parse xsl, and build a transformation object
# allow writing if an output directory is given
Expand Down