Skip to content

Commit

Permalink
Generate better stack traces for XML errors in included files
Browse files Browse the repository at this point in the history
  • Loading branch information
ascholerChemeketa committed Jan 14, 2025
1 parent 42236bf commit e457d66
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pretext/pretext.py
Original file line number Diff line number Diff line change
Expand Up @@ -4070,7 +4070,24 @@ 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
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
2 changes: 2 additions & 0 deletions xsl/pretext-common.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -9157,6 +9157,8 @@ Book (with parts), "section" at level 3
</xsl:for-each>
</xsl:variable>
<xsl:if test="not($hits = 'X')">
<xsl:message>================
<xsl:value-of select="$hits"/></xsl:message>
<!-- drop the failed lookup, plus a separator. A nonempty -->
<!-- result for this template is indicative of a failure -->
<!-- and the list can be reported in the error message -->
Expand Down

0 comments on commit e457d66

Please sign in to comment.