@@ -1668,6 +1668,17 @@ def test_unknown_event(self):
1668
1668
</document>
1669
1669
""" .format (html .escape (SIMPLE_XMLFILE , True ))
1670
1670
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
+
1671
1682
#
1672
1683
# badly formatted xi:include tags
1673
1684
@@ -1688,6 +1699,31 @@ def test_unknown_event(self):
1688
1699
</div>
1689
1700
"""
1690
1701
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
+
1691
1727
class XIncludeTest (unittest .TestCase ):
1692
1728
1693
1729
def xinclude_loader (self , href , parse = "xml" , encoding = None ):
@@ -1789,6 +1825,13 @@ def test_xinclude(self):
1789
1825
' </ns0:include>\n '
1790
1826
'</div>' ) # C5
1791
1827
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
+
1792
1835
def test_xinclude_failures (self ):
1793
1836
from xml .etree import ElementInclude
1794
1837
@@ -1821,6 +1864,45 @@ def test_xinclude_failures(self):
1821
1864
"xi:fallback tag must be child of xi:include "
1822
1865
"('{http://www.w3.org/2001/XInclude}fallback')" )
1823
1866
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
+
1824
1906
# --------------------------------------------------------------------
1825
1907
# reported bugs
1826
1908
0 commit comments