diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2019-11-25 15:36:25 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-25 15:36:25 (GMT) |
commit | c6a7bdb356835c9d7513b1ea6846683d446fe6c3 (patch) | |
tree | 3e98d7da17cbffbfc4ffd97392c5869caccb5185 /Lib/test/test_xml_etree.py | |
parent | ded8888fbc33011dd39b7b1c86a5adfacc4943f3 (diff) | |
download | cpython-c6a7bdb356835c9d7513b1ea6846683d446fe6c3.zip cpython-c6a7bdb356835c9d7513b1ea6846683d446fe6c3.tar.gz cpython-c6a7bdb356835c9d7513b1ea6846683d446fe6c3.tar.bz2 |
bpo-20928: support base-URL and recursive includes in etree.ElementInclude (#5723)
* bpo-20928: bring elementtree's XInclude support en-par with the implementation in lxml by adding support for recursive includes and a base-URL.
* bpo-20928: Support xincluding the same file multiple times, just not recursively.
* bpo-20928: Add 'max_depth' parameter to xinclude that limits the maximum recursion depth to 6 by default.
* Add news entry for updated ElementInclude support
Diffstat (limited to 'Lib/test/test_xml_etree.py')
-rw-r--r-- | Lib/test/test_xml_etree.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 9fbb4ac..09c234c 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -1668,6 +1668,17 @@ XINCLUDE["default.xml"] = """\ </document> """.format(html.escape(SIMPLE_XMLFILE, True)) +XINCLUDE["include_c1_repeated.xml"] = """\ +<?xml version='1.0'?> +<document xmlns:xi="http://www.w3.org/2001/XInclude"> + <p>The following is the source code of Recursive1.xml:</p> + <xi:include href="C1.xml"/> + <xi:include href="C1.xml"/> + <xi:include href="C1.xml"/> + <xi:include href="C1.xml"/> +</document> +""" + # # badly formatted xi:include tags @@ -1688,6 +1699,31 @@ XINCLUDE_BAD["B2.xml"] = """\ </div> """ +XINCLUDE["Recursive1.xml"] = """\ +<?xml version='1.0'?> +<document xmlns:xi="http://www.w3.org/2001/XInclude"> + <p>The following is the source code of Recursive2.xml:</p> + <xi:include href="Recursive2.xml"/> +</document> +""" + +XINCLUDE["Recursive2.xml"] = """\ +<?xml version='1.0'?> +<document xmlns:xi="http://www.w3.org/2001/XInclude"> + <p>The following is the source code of Recursive3.xml:</p> + <xi:include href="Recursive3.xml"/> +</document> +""" + +XINCLUDE["Recursive3.xml"] = """\ +<?xml version='1.0'?> +<document xmlns:xi="http://www.w3.org/2001/XInclude"> + <p>The following is the source code of Recursive1.xml:</p> + <xi:include href="Recursive1.xml"/> +</document> +""" + + class XIncludeTest(unittest.TestCase): def xinclude_loader(self, href, parse="xml", encoding=None): @@ -1789,6 +1825,13 @@ class XIncludeTest(unittest.TestCase): ' </ns0:include>\n' '</div>') # C5 + def test_xinclude_repeated(self): + from xml.etree import ElementInclude + + document = self.xinclude_loader("include_c1_repeated.xml") + ElementInclude.include(document, self.xinclude_loader) + self.assertEqual(1+4*2, len(document.findall(".//p"))) + def test_xinclude_failures(self): from xml.etree import ElementInclude @@ -1821,6 +1864,45 @@ class XIncludeTest(unittest.TestCase): "xi:fallback tag must be child of xi:include " "('{http://www.w3.org/2001/XInclude}fallback')") + # Test infinitely recursive includes. + document = self.xinclude_loader("Recursive1.xml") + with self.assertRaises(ElementInclude.FatalIncludeError) as cm: + ElementInclude.include(document, self.xinclude_loader) + self.assertEqual(str(cm.exception), + "recursive include of Recursive2.xml") + + # Test 'max_depth' limitation. + document = self.xinclude_loader("Recursive1.xml") + with self.assertRaises(ElementInclude.FatalIncludeError) as cm: + ElementInclude.include(document, self.xinclude_loader, max_depth=None) + self.assertEqual(str(cm.exception), + "recursive include of Recursive2.xml") + + document = self.xinclude_loader("Recursive1.xml") + with self.assertRaises(ElementInclude.LimitedRecursiveIncludeError) as cm: + ElementInclude.include(document, self.xinclude_loader, max_depth=0) + self.assertEqual(str(cm.exception), + "maximum xinclude depth reached when including file Recursive2.xml") + + document = self.xinclude_loader("Recursive1.xml") + with self.assertRaises(ElementInclude.LimitedRecursiveIncludeError) as cm: + ElementInclude.include(document, self.xinclude_loader, max_depth=1) + self.assertEqual(str(cm.exception), + "maximum xinclude depth reached when including file Recursive3.xml") + + document = self.xinclude_loader("Recursive1.xml") + with self.assertRaises(ElementInclude.LimitedRecursiveIncludeError) as cm: + ElementInclude.include(document, self.xinclude_loader, max_depth=2) + self.assertEqual(str(cm.exception), + "maximum xinclude depth reached when including file Recursive1.xml") + + document = self.xinclude_loader("Recursive1.xml") + with self.assertRaises(ElementInclude.FatalIncludeError) as cm: + ElementInclude.include(document, self.xinclude_loader, max_depth=3) + self.assertEqual(str(cm.exception), + "recursive include of Recursive2.xml") + + # -------------------------------------------------------------------- # reported bugs |