@@ -1668,6 +1668,17 @@ def test_unknown_event(self):
16681668</document>
16691669""" .format (html .escape (SIMPLE_XMLFILE , True ))
16701670
1671+ XINCLUDE ["include_c1_repeated.xml" ] = """\
1672+ <?xml version='1.0'?>
1673+ <document xmlns:xi="http://www.w3.org/2001/XInclude">
1674+ <p>The following is the source code of Recursive1.xml:</p>
1675+ <xi:include href="C1.xml"/>
1676+ <xi:include href="C1.xml"/>
1677+ <xi:include href="C1.xml"/>
1678+ <xi:include href="C1.xml"/>
1679+ </document>
1680+ """
1681+
16711682#
16721683# badly formatted xi:include tags
16731684
@@ -1688,6 +1699,31 @@ def test_unknown_event(self):
16881699</div>
16891700"""
16901701
1702+ XINCLUDE ["Recursive1.xml" ] = """\
1703+ <?xml version='1.0'?>
1704+ <document xmlns:xi="http://www.w3.org/2001/XInclude">
1705+ <p>The following is the source code of Recursive2.xml:</p>
1706+ <xi:include href="Recursive2.xml"/>
1707+ </document>
1708+ """
1709+
1710+ XINCLUDE ["Recursive2.xml" ] = """\
1711+ <?xml version='1.0'?>
1712+ <document xmlns:xi="http://www.w3.org/2001/XInclude">
1713+ <p>The following is the source code of Recursive3.xml:</p>
1714+ <xi:include href="Recursive3.xml"/>
1715+ </document>
1716+ """
1717+
1718+ XINCLUDE ["Recursive3.xml" ] = """\
1719+ <?xml version='1.0'?>
1720+ <document xmlns:xi="http://www.w3.org/2001/XInclude">
1721+ <p>The following is the source code of Recursive1.xml:</p>
1722+ <xi:include href="Recursive1.xml"/>
1723+ </document>
1724+ """
1725+
1726+
16911727class XIncludeTest (unittest .TestCase ):
16921728
16931729 def xinclude_loader (self , href , parse = "xml" , encoding = None ):
@@ -1789,6 +1825,13 @@ def test_xinclude(self):
17891825 ' </ns0:include>\n '
17901826 '</div>' ) # C5
17911827
1828+ def test_xinclude_repeated (self ):
1829+ from xml .etree import ElementInclude
1830+
1831+ document = self .xinclude_loader ("include_c1_repeated.xml" )
1832+ ElementInclude .include (document , self .xinclude_loader )
1833+ self .assertEqual (1 + 4 * 2 , len (document .findall (".//p" )))
1834+
17921835 def test_xinclude_failures (self ):
17931836 from xml .etree import ElementInclude
17941837
@@ -1821,6 +1864,45 @@ def test_xinclude_failures(self):
18211864 "xi:fallback tag must be child of xi:include "
18221865 "('{http://www.w3.org/2001/XInclude}fallback')" )
18231866
1867+ # Test infinitely recursive includes.
1868+ document = self .xinclude_loader ("Recursive1.xml" )
1869+ with self .assertRaises (ElementInclude .FatalIncludeError ) as cm :
1870+ ElementInclude .include (document , self .xinclude_loader )
1871+ self .assertEqual (str (cm .exception ),
1872+ "recursive include of Recursive2.xml" )
1873+
1874+ # Test 'max_depth' limitation.
1875+ document = self .xinclude_loader ("Recursive1.xml" )
1876+ with self .assertRaises (ElementInclude .FatalIncludeError ) as cm :
1877+ ElementInclude .include (document , self .xinclude_loader , max_depth = None )
1878+ self .assertEqual (str (cm .exception ),
1879+ "recursive include of Recursive2.xml" )
1880+
1881+ document = self .xinclude_loader ("Recursive1.xml" )
1882+ with self .assertRaises (ElementInclude .LimitedRecursiveIncludeError ) as cm :
1883+ ElementInclude .include (document , self .xinclude_loader , max_depth = 0 )
1884+ self .assertEqual (str (cm .exception ),
1885+ "maximum xinclude depth reached when including file Recursive2.xml" )
1886+
1887+ document = self .xinclude_loader ("Recursive1.xml" )
1888+ with self .assertRaises (ElementInclude .LimitedRecursiveIncludeError ) as cm :
1889+ ElementInclude .include (document , self .xinclude_loader , max_depth = 1 )
1890+ self .assertEqual (str (cm .exception ),
1891+ "maximum xinclude depth reached when including file Recursive3.xml" )
1892+
1893+ document = self .xinclude_loader ("Recursive1.xml" )
1894+ with self .assertRaises (ElementInclude .LimitedRecursiveIncludeError ) as cm :
1895+ ElementInclude .include (document , self .xinclude_loader , max_depth = 2 )
1896+ self .assertEqual (str (cm .exception ),
1897+ "maximum xinclude depth reached when including file Recursive1.xml" )
1898+
1899+ document = self .xinclude_loader ("Recursive1.xml" )
1900+ with self .assertRaises (ElementInclude .FatalIncludeError ) as cm :
1901+ ElementInclude .include (document , self .xinclude_loader , max_depth = 3 )
1902+ self .assertEqual (str (cm .exception ),
1903+ "recursive include of Recursive2.xml" )
1904+
1905+
18241906# --------------------------------------------------------------------
18251907# reported bugs
18261908
0 commit comments