blob: 1c6a939c29a6b0f2b5bf45c9d881f9e4f162b757 [file] [log] [blame]
Eli Bendersky865756a2012-03-09 13:38:15 +02001# IMPORTANT: the same tests are run from "test_xml_etree_c" in order
2# to ensure consistency between the C implementation and the Python
3# implementation.
Florent Xiclunaf15351d2010-03-13 23:24:31 +00004#
5# For this purpose, the module-level "ET" symbol is temporarily
6# monkey-patched when running the "test_xml_etree_c" test suite.
Florent Xiclunaf15351d2010-03-13 23:24:31 +00007
Georg Brandl1f7fffb2010-10-15 15:57:45 +00008import html
Eli Benderskyf996e772012-03-16 05:53:30 +02009import io
Eli Bendersky698bdb22013-01-10 06:01:06 -080010import operator
Eli Bendersky7ec45f72012-12-30 06:17:49 -080011import pickle
Eli Bendersky0192ba32012-03-30 16:38:33 +030012import sys
Eli Benderskye26fa1b2013-05-19 17:49:54 -070013import types
Victor Stinner6c6f8512010-08-07 10:09:35 +000014import unittest
Serhiy Storchaka05744ac2015-06-29 22:35:58 +030015import warnings
Eli Benderskya5e82202012-03-31 13:55:38 +030016import weakref
Armin Rigo9ed73062005-12-14 18:10:45 +000017
Eli Bendersky698bdb22013-01-10 06:01:06 -080018from itertools import product
Benjamin Petersonee8712c2008-05-20 21:35:26 +000019from test import support
Eli Bendersky23687042013-02-26 05:53:23 -080020from test.support import TESTFN, findfile, import_fresh_module, gc_collect
Armin Rigo9ed73062005-12-14 18:10:45 +000021
Eli Bendersky698bdb22013-01-10 06:01:06 -080022# pyET is the pure-Python implementation.
Eli Bendersky458c0d52013-01-10 06:07:00 -080023#
Eli Bendersky698bdb22013-01-10 06:01:06 -080024# ET is pyET in test_xml_etree and is the C accelerated version in
25# test_xml_etree_c.
Eli Bendersky64d11e62012-06-15 07:42:50 +030026pyET = None
27ET = None
Florent Xiclunaf15351d2010-03-13 23:24:31 +000028
29SIMPLE_XMLFILE = findfile("simple.xml", subdir="xmltestdata")
Victor Stinner6c6f8512010-08-07 10:09:35 +000030try:
Marc-André Lemburg8f36af72011-02-25 15:42:01 +000031 SIMPLE_XMLFILE.encode("utf-8")
Victor Stinner6c6f8512010-08-07 10:09:35 +000032except UnicodeEncodeError:
33 raise unittest.SkipTest("filename is not encodable to utf8")
Florent Xiclunaf15351d2010-03-13 23:24:31 +000034SIMPLE_NS_XMLFILE = findfile("simple-ns.xml", subdir="xmltestdata")
35
36SAMPLE_XML = """\
Armin Rigo9ed73062005-12-14 18:10:45 +000037<body>
Florent Xiclunaf15351d2010-03-13 23:24:31 +000038 <tag class='a'>text</tag>
39 <tag class='b' />
Armin Rigo9ed73062005-12-14 18:10:45 +000040 <section>
Florent Xiclunaf15351d2010-03-13 23:24:31 +000041 <tag class='b' id='inner'>subtext</tag>
Armin Rigo9ed73062005-12-14 18:10:45 +000042 </section>
43</body>
44"""
45
Florent Xiclunaf15351d2010-03-13 23:24:31 +000046SAMPLE_SECTION = """\
47<section>
48 <tag class='b' id='inner'>subtext</tag>
49 <nexttag />
50 <nextsection>
51 <tag />
52 </nextsection>
53</section>
54"""
55
Armin Rigo9ed73062005-12-14 18:10:45 +000056SAMPLE_XML_NS = """
57<body xmlns="http://effbot.org/ns">
58 <tag>text</tag>
59 <tag />
60 <section>
61 <tag>subtext</tag>
62 </section>
63</body>
64"""
65
Eli Bendersky737b1732012-05-29 06:02:56 +030066SAMPLE_XML_NS_ELEMS = """
67<root>
68<h:table xmlns:h="hello">
69 <h:tr>
70 <h:td>Apples</h:td>
71 <h:td>Bananas</h:td>
72 </h:tr>
73</h:table>
74
75<f:table xmlns:f="foo">
76 <f:name>African Coffee Table</f:name>
77 <f:width>80</f:width>
78 <f:length>120</f:length>
79</f:table>
80</root>
81"""
Florent Xiclunaf15351d2010-03-13 23:24:31 +000082
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +020083ENTITY_XML = """\
84<!DOCTYPE points [
85<!ENTITY % user-entities SYSTEM 'user-entities.xml'>
86%user-entities;
87]>
88<document>&entity;</document>
89"""
Armin Rigo9ed73062005-12-14 18:10:45 +000090
Armin Rigo9ed73062005-12-14 18:10:45 +000091
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +020092class ModuleTest(unittest.TestCase):
Eli Bendersky23687042013-02-26 05:53:23 -080093 # TODO: this should be removed once we get rid of the global module vars
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +020094
95 def test_sanity(self):
96 # Import sanity.
97
98 from xml.etree import ElementTree
99 from xml.etree import ElementInclude
100 from xml.etree import ElementPath
101
Armin Rigo9ed73062005-12-14 18:10:45 +0000102
Florent Xiclunac17f1722010-08-08 19:48:29 +0000103def serialize(elem, to_string=True, encoding='unicode', **options):
Florent Xiclunac17f1722010-08-08 19:48:29 +0000104 if encoding != 'unicode':
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000105 file = io.BytesIO()
106 else:
107 file = io.StringIO()
Armin Rigo9ed73062005-12-14 18:10:45 +0000108 tree = ET.ElementTree(elem)
Florent Xiclunac17f1722010-08-08 19:48:29 +0000109 tree.write(file, encoding=encoding, **options)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000110 if to_string:
111 return file.getvalue()
112 else:
113 file.seek(0)
114 return file
Armin Rigo9ed73062005-12-14 18:10:45 +0000115
Armin Rigo9ed73062005-12-14 18:10:45 +0000116def summarize_list(seq):
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200117 return [elem.tag for elem in seq]
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000118
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000119
Eli Bendersky698bdb22013-01-10 06:01:06 -0800120class ElementTestCase:
121 @classmethod
122 def setUpClass(cls):
123 cls.modules = {pyET, ET}
124
Serhiy Storchakabad12572014-12-15 14:03:42 +0200125 def pickleRoundTrip(self, obj, name, dumper, loader, proto):
Eli Bendersky698bdb22013-01-10 06:01:06 -0800126 save_m = sys.modules[name]
127 try:
128 sys.modules[name] = dumper
Serhiy Storchakabad12572014-12-15 14:03:42 +0200129 temp = pickle.dumps(obj, proto)
Eli Bendersky698bdb22013-01-10 06:01:06 -0800130 sys.modules[name] = loader
131 result = pickle.loads(temp)
132 except pickle.PicklingError as pe:
133 # pyET must be second, because pyET may be (equal to) ET.
134 human = dict([(ET, "cET"), (pyET, "pyET")])
135 raise support.TestFailed("Failed to round-trip %r from %r to %r"
136 % (obj,
137 human.get(dumper, dumper),
138 human.get(loader, loader))) from pe
139 finally:
140 sys.modules[name] = save_m
141 return result
142
143 def assertEqualElements(self, alice, bob):
144 self.assertIsInstance(alice, (ET.Element, pyET.Element))
145 self.assertIsInstance(bob, (ET.Element, pyET.Element))
146 self.assertEqual(len(list(alice)), len(list(bob)))
147 for x, y in zip(alice, bob):
148 self.assertEqualElements(x, y)
149 properties = operator.attrgetter('tag', 'tail', 'text', 'attrib')
150 self.assertEqual(properties(alice), properties(bob))
151
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000152# --------------------------------------------------------------------
153# element tree tests
Armin Rigo9ed73062005-12-14 18:10:45 +0000154
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200155class ElementTreeTest(unittest.TestCase):
Armin Rigo9ed73062005-12-14 18:10:45 +0000156
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200157 def serialize_check(self, elem, expected):
158 self.assertEqual(serialize(elem), expected)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000159
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200160 def test_interface(self):
161 # Test element tree interface.
Armin Rigo9ed73062005-12-14 18:10:45 +0000162
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200163 def check_string(string):
164 len(string)
165 for char in string:
166 self.assertEqual(len(char), 1,
167 msg="expected one-character string, got %r" % char)
168 new_string = string + ""
169 new_string = string + " "
170 string[:0]
Armin Rigo9ed73062005-12-14 18:10:45 +0000171
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200172 def check_mapping(mapping):
173 len(mapping)
174 keys = mapping.keys()
175 items = mapping.items()
176 for key in keys:
177 item = mapping[key]
178 mapping["key"] = "value"
179 self.assertEqual(mapping["key"], "value",
180 msg="expected value string, got %r" % mapping["key"])
Armin Rigo9ed73062005-12-14 18:10:45 +0000181
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200182 def check_element(element):
183 self.assertTrue(ET.iselement(element), msg="not an element")
184 self.assertTrue(hasattr(element, "tag"), msg="no tag member")
185 self.assertTrue(hasattr(element, "attrib"), msg="no attrib member")
186 self.assertTrue(hasattr(element, "text"), msg="no text member")
187 self.assertTrue(hasattr(element, "tail"), msg="no tail member")
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000188
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200189 check_string(element.tag)
190 check_mapping(element.attrib)
191 if element.text is not None:
192 check_string(element.text)
193 if element.tail is not None:
194 check_string(element.tail)
195 for elem in element:
196 check_element(elem)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000197
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200198 element = ET.Element("tag")
199 check_element(element)
200 tree = ET.ElementTree(element)
201 check_element(tree.getroot())
202 element = ET.Element("t\xe4g", key="value")
203 tree = ET.ElementTree(element)
204 self.assertRegex(repr(element), r"^<Element 't\xe4g' at 0x.*>$")
205 element = ET.Element("tag", key="value")
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000206
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200207 # Make sure all standard element methods exist.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000208
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200209 def check_method(method):
210 self.assertTrue(hasattr(method, '__call__'),
211 msg="%s not callable" % method)
Armin Rigo9ed73062005-12-14 18:10:45 +0000212
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200213 check_method(element.append)
214 check_method(element.extend)
215 check_method(element.insert)
216 check_method(element.remove)
217 check_method(element.getchildren)
218 check_method(element.find)
219 check_method(element.iterfind)
220 check_method(element.findall)
221 check_method(element.findtext)
222 check_method(element.clear)
223 check_method(element.get)
224 check_method(element.set)
225 check_method(element.keys)
226 check_method(element.items)
227 check_method(element.iter)
228 check_method(element.itertext)
229 check_method(element.getiterator)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000230
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200231 # These methods return an iterable. See bug 6472.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000232
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200233 def check_iter(it):
234 check_method(it.__next__)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000235
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200236 check_iter(element.iterfind("tag"))
237 check_iter(element.iterfind("*"))
238 check_iter(tree.iterfind("tag"))
239 check_iter(tree.iterfind("*"))
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000240
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200241 # These aliases are provided:
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000242
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200243 self.assertEqual(ET.XML, ET.fromstring)
244 self.assertEqual(ET.PI, ET.ProcessingInstruction)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000245
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200246 def test_simpleops(self):
247 # Basic method sanity checks.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000248
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200249 elem = ET.XML("<body><tag/></body>")
250 self.serialize_check(elem, '<body><tag /></body>')
251 e = ET.Element("tag2")
252 elem.append(e)
253 self.serialize_check(elem, '<body><tag /><tag2 /></body>')
254 elem.remove(e)
255 self.serialize_check(elem, '<body><tag /></body>')
256 elem.insert(0, e)
257 self.serialize_check(elem, '<body><tag2 /><tag /></body>')
258 elem.remove(e)
259 elem.extend([e])
260 self.serialize_check(elem, '<body><tag /><tag2 /></body>')
261 elem.remove(e)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000262
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200263 element = ET.Element("tag", key="value")
264 self.serialize_check(element, '<tag key="value" />') # 1
265 subelement = ET.Element("subtag")
266 element.append(subelement)
267 self.serialize_check(element, '<tag key="value"><subtag /></tag>') # 2
268 element.insert(0, subelement)
269 self.serialize_check(element,
270 '<tag key="value"><subtag /><subtag /></tag>') # 3
271 element.remove(subelement)
272 self.serialize_check(element, '<tag key="value"><subtag /></tag>') # 4
273 element.remove(subelement)
274 self.serialize_check(element, '<tag key="value" />') # 5
275 with self.assertRaises(ValueError) as cm:
276 element.remove(subelement)
277 self.assertEqual(str(cm.exception), 'list.remove(x): x not in list')
278 self.serialize_check(element, '<tag key="value" />') # 6
279 element[0:0] = [subelement, subelement, subelement]
280 self.serialize_check(element[1], '<subtag />')
281 self.assertEqual(element[1:9], [element[1], element[2]])
282 self.assertEqual(element[:9:2], [element[0], element[2]])
283 del element[1:2]
284 self.serialize_check(element,
285 '<tag key="value"><subtag /><subtag /></tag>')
Florent Xiclunaa72a98f2012-02-13 11:03:30 +0100286
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200287 def test_cdata(self):
288 # Test CDATA handling (etc).
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000289
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200290 self.serialize_check(ET.XML("<tag>hello</tag>"),
291 '<tag>hello</tag>')
292 self.serialize_check(ET.XML("<tag>&#104;&#101;&#108;&#108;&#111;</tag>"),
293 '<tag>hello</tag>')
294 self.serialize_check(ET.XML("<tag><![CDATA[hello]]></tag>"),
295 '<tag>hello</tag>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000296
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200297 def test_file_init(self):
298 stringfile = io.BytesIO(SAMPLE_XML.encode("utf-8"))
299 tree = ET.ElementTree(file=stringfile)
300 self.assertEqual(tree.find("tag").tag, 'tag')
301 self.assertEqual(tree.find("section/tag").tag, 'tag')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000302
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200303 tree = ET.ElementTree(file=SIMPLE_XMLFILE)
304 self.assertEqual(tree.find("element").tag, 'element')
305 self.assertEqual(tree.find("element/../empty-element").tag,
306 'empty-element')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000307
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200308 def test_path_cache(self):
309 # Check that the path cache behaves sanely.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000310
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200311 from xml.etree import ElementPath
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000312
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200313 elem = ET.XML(SAMPLE_XML)
314 for i in range(10): ET.ElementTree(elem).find('./'+str(i))
315 cache_len_10 = len(ElementPath._cache)
316 for i in range(10): ET.ElementTree(elem).find('./'+str(i))
317 self.assertEqual(len(ElementPath._cache), cache_len_10)
318 for i in range(20): ET.ElementTree(elem).find('./'+str(i))
319 self.assertGreater(len(ElementPath._cache), cache_len_10)
320 for i in range(600): ET.ElementTree(elem).find('./'+str(i))
321 self.assertLess(len(ElementPath._cache), 500)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000322
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200323 def test_copy(self):
324 # Test copy handling (etc).
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000325
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200326 import copy
327 e1 = ET.XML("<tag>hello<foo/></tag>")
328 e2 = copy.copy(e1)
329 e3 = copy.deepcopy(e1)
330 e1.find("foo").tag = "bar"
331 self.serialize_check(e1, '<tag>hello<bar /></tag>')
332 self.serialize_check(e2, '<tag>hello<bar /></tag>')
333 self.serialize_check(e3, '<tag>hello<foo /></tag>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000334
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200335 def test_attrib(self):
336 # Test attribute handling.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000337
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200338 elem = ET.Element("tag")
339 elem.get("key") # 1.1
340 self.assertEqual(elem.get("key", "default"), 'default') # 1.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000341
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200342 elem.set("key", "value")
343 self.assertEqual(elem.get("key"), 'value') # 1.3
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000344
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200345 elem = ET.Element("tag", key="value")
346 self.assertEqual(elem.get("key"), 'value') # 2.1
347 self.assertEqual(elem.attrib, {'key': 'value'}) # 2.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000348
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200349 attrib = {"key": "value"}
350 elem = ET.Element("tag", attrib)
351 attrib.clear() # check for aliasing issues
352 self.assertEqual(elem.get("key"), 'value') # 3.1
353 self.assertEqual(elem.attrib, {'key': 'value'}) # 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000354
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200355 attrib = {"key": "value"}
356 elem = ET.Element("tag", **attrib)
357 attrib.clear() # check for aliasing issues
358 self.assertEqual(elem.get("key"), 'value') # 4.1
359 self.assertEqual(elem.attrib, {'key': 'value'}) # 4.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000360
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200361 elem = ET.Element("tag", {"key": "other"}, key="value")
362 self.assertEqual(elem.get("key"), 'value') # 5.1
363 self.assertEqual(elem.attrib, {'key': 'value'}) # 5.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000364
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200365 elem = ET.Element('test')
366 elem.text = "aa"
367 elem.set('testa', 'testval')
368 elem.set('testb', 'test2')
369 self.assertEqual(ET.tostring(elem),
370 b'<test testa="testval" testb="test2">aa</test>')
371 self.assertEqual(sorted(elem.keys()), ['testa', 'testb'])
372 self.assertEqual(sorted(elem.items()),
373 [('testa', 'testval'), ('testb', 'test2')])
374 self.assertEqual(elem.attrib['testb'], 'test2')
375 elem.attrib['testb'] = 'test1'
376 elem.attrib['testc'] = 'test2'
377 self.assertEqual(ET.tostring(elem),
378 b'<test testa="testval" testb="test1" testc="test2">aa</test>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000379
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200380 def test_makeelement(self):
381 # Test makeelement handling.
Antoine Pitroub86680e2010-10-14 21:15:17 +0000382
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200383 elem = ET.Element("tag")
384 attrib = {"key": "value"}
385 subelem = elem.makeelement("subtag", attrib)
386 self.assertIsNot(subelem.attrib, attrib, msg="attrib aliasing")
387 elem.append(subelem)
388 self.serialize_check(elem, '<tag><subtag key="value" /></tag>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000389
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200390 elem.clear()
391 self.serialize_check(elem, '<tag />')
392 elem.append(subelem)
393 self.serialize_check(elem, '<tag><subtag key="value" /></tag>')
394 elem.extend([subelem, subelem])
395 self.serialize_check(elem,
396 '<tag><subtag key="value" /><subtag key="value" /><subtag key="value" /></tag>')
397 elem[:] = [subelem]
398 self.serialize_check(elem, '<tag><subtag key="value" /></tag>')
399 elem[:] = tuple([subelem])
400 self.serialize_check(elem, '<tag><subtag key="value" /></tag>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000401
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200402 def test_parsefile(self):
403 # Test parsing from file.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000404
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200405 tree = ET.parse(SIMPLE_XMLFILE)
406 stream = io.StringIO()
407 tree.write(stream, encoding='unicode')
408 self.assertEqual(stream.getvalue(),
409 '<root>\n'
410 ' <element key="value">text</element>\n'
411 ' <element>text</element>tail\n'
412 ' <empty-element />\n'
413 '</root>')
414 tree = ET.parse(SIMPLE_NS_XMLFILE)
415 stream = io.StringIO()
416 tree.write(stream, encoding='unicode')
417 self.assertEqual(stream.getvalue(),
418 '<ns0:root xmlns:ns0="namespace">\n'
419 ' <ns0:element key="value">text</ns0:element>\n'
420 ' <ns0:element>text</ns0:element>tail\n'
421 ' <ns0:empty-element />\n'
422 '</ns0:root>')
Armin Rigo9ed73062005-12-14 18:10:45 +0000423
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200424 with open(SIMPLE_XMLFILE) as f:
425 data = f.read()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000426
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200427 parser = ET.XMLParser()
428 self.assertRegex(parser.version, r'^Expat ')
429 parser.feed(data)
430 self.serialize_check(parser.close(),
431 '<root>\n'
432 ' <element key="value">text</element>\n'
433 ' <element>text</element>tail\n'
434 ' <empty-element />\n'
435 '</root>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000436
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200437 target = ET.TreeBuilder()
438 parser = ET.XMLParser(target=target)
439 parser.feed(data)
440 self.serialize_check(parser.close(),
441 '<root>\n'
442 ' <element key="value">text</element>\n'
443 ' <element>text</element>tail\n'
444 ' <empty-element />\n'
445 '</root>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000446
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200447 def test_parseliteral(self):
448 element = ET.XML("<html><body>text</body></html>")
449 self.assertEqual(ET.tostring(element, encoding='unicode'),
450 '<html><body>text</body></html>')
451 element = ET.fromstring("<html><body>text</body></html>")
452 self.assertEqual(ET.tostring(element, encoding='unicode'),
453 '<html><body>text</body></html>')
454 sequence = ["<html><body>", "text</bo", "dy></html>"]
455 element = ET.fromstringlist(sequence)
456 self.assertEqual(ET.tostring(element),
457 b'<html><body>text</body></html>')
458 self.assertEqual(b"".join(ET.tostringlist(element)),
459 b'<html><body>text</body></html>')
460 self.assertEqual(ET.tostring(element, "ascii"),
461 b"<?xml version='1.0' encoding='ascii'?>\n"
462 b"<html><body>text</body></html>")
463 _, ids = ET.XMLID("<html><body>text</body></html>")
464 self.assertEqual(len(ids), 0)
465 _, ids = ET.XMLID("<html><body id='body'>text</body></html>")
466 self.assertEqual(len(ids), 1)
467 self.assertEqual(ids["body"].tag, 'body')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000468
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200469 def test_iterparse(self):
470 # Test iterparse interface.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000471
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200472 iterparse = ET.iterparse
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000473
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200474 context = iterparse(SIMPLE_XMLFILE)
475 action, elem = next(context)
476 self.assertEqual((action, elem.tag), ('end', 'element'))
477 self.assertEqual([(action, elem.tag) for action, elem in context], [
478 ('end', 'element'),
479 ('end', 'empty-element'),
480 ('end', 'root'),
481 ])
482 self.assertEqual(context.root.tag, 'root')
483
484 context = iterparse(SIMPLE_NS_XMLFILE)
485 self.assertEqual([(action, elem.tag) for action, elem in context], [
486 ('end', '{namespace}element'),
487 ('end', '{namespace}element'),
488 ('end', '{namespace}empty-element'),
489 ('end', '{namespace}root'),
490 ])
491
492 events = ()
493 context = iterparse(SIMPLE_XMLFILE, events)
494 self.assertEqual([(action, elem.tag) for action, elem in context], [])
495
496 events = ()
497 context = iterparse(SIMPLE_XMLFILE, events=events)
498 self.assertEqual([(action, elem.tag) for action, elem in context], [])
499
500 events = ("start", "end")
501 context = iterparse(SIMPLE_XMLFILE, events)
502 self.assertEqual([(action, elem.tag) for action, elem in context], [
503 ('start', 'root'),
504 ('start', 'element'),
505 ('end', 'element'),
506 ('start', 'element'),
507 ('end', 'element'),
508 ('start', 'empty-element'),
509 ('end', 'empty-element'),
510 ('end', 'root'),
511 ])
512
513 events = ("start", "end", "start-ns", "end-ns")
514 context = iterparse(SIMPLE_NS_XMLFILE, events)
Eli Bendersky23687042013-02-26 05:53:23 -0800515 self.assertEqual([(action, elem.tag) if action in ("start", "end")
516 else (action, elem)
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200517 for action, elem in context], [
518 ('start-ns', ('', 'namespace')),
519 ('start', '{namespace}root'),
520 ('start', '{namespace}element'),
521 ('end', '{namespace}element'),
522 ('start', '{namespace}element'),
523 ('end', '{namespace}element'),
524 ('start', '{namespace}empty-element'),
525 ('end', '{namespace}empty-element'),
526 ('end', '{namespace}root'),
527 ('end-ns', None),
528 ])
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000529
Eli Bendersky5dd40e52013-11-28 06:31:58 -0800530 events = ('start-ns', 'end-ns')
531 context = iterparse(io.StringIO(r"<root xmlns=''/>"), events)
532 res = [action for action, elem in context]
533 self.assertEqual(res, ['start-ns', 'end-ns'])
534
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200535 events = ("start", "end", "bogus")
536 with self.assertRaises(ValueError) as cm:
537 with open(SIMPLE_XMLFILE, "rb") as f:
538 iterparse(f, events)
539 self.assertEqual(str(cm.exception), "unknown event 'bogus'")
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000540
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200541 source = io.BytesIO(
542 b"<?xml version='1.0' encoding='iso-8859-1'?>\n"
543 b"<body xmlns='http://&#233;ffbot.org/ns'\n"
544 b" xmlns:cl\xe9='http://effbot.org/ns'>text</body>\n")
545 events = ("start-ns",)
546 context = iterparse(source, events)
547 self.assertEqual([(action, elem) for action, elem in context], [
548 ('start-ns', ('', 'http://\xe9ffbot.org/ns')),
549 ('start-ns', ('cl\xe9', 'http://effbot.org/ns')),
550 ])
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000551
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200552 source = io.StringIO("<document />junk")
553 it = iterparse(source)
554 action, elem = next(it)
555 self.assertEqual((action, elem.tag), ('end', 'document'))
556 with self.assertRaises(ET.ParseError) as cm:
557 next(it)
558 self.assertEqual(str(cm.exception),
559 'junk after document element: line 1, column 12')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000560
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200561 def test_writefile(self):
562 elem = ET.Element("tag")
563 elem.text = "text"
564 self.serialize_check(elem, '<tag>text</tag>')
565 ET.SubElement(elem, "subtag").text = "subtext"
566 self.serialize_check(elem, '<tag>text<subtag>subtext</subtag></tag>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000567
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200568 # Test tag suppression
569 elem.tag = None
570 self.serialize_check(elem, 'text<subtag>subtext</subtag>')
571 elem.insert(0, ET.Comment("comment"))
572 self.serialize_check(elem,
573 'text<!--comment--><subtag>subtext</subtag>') # assumes 1.3
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000574
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200575 elem[0] = ET.PI("key", "value")
576 self.serialize_check(elem, 'text<?key value?><subtag>subtext</subtag>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000577
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200578 def test_custom_builder(self):
579 # Test parser w. custom builder.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000580
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200581 with open(SIMPLE_XMLFILE) as f:
582 data = f.read()
583 class Builder(list):
584 def start(self, tag, attrib):
585 self.append(("start", tag))
586 def end(self, tag):
587 self.append(("end", tag))
588 def data(self, text):
589 pass
590 builder = Builder()
591 parser = ET.XMLParser(target=builder)
592 parser.feed(data)
593 self.assertEqual(builder, [
594 ('start', 'root'),
595 ('start', 'element'),
596 ('end', 'element'),
597 ('start', 'element'),
598 ('end', 'element'),
599 ('start', 'empty-element'),
600 ('end', 'empty-element'),
601 ('end', 'root'),
602 ])
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000603
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200604 with open(SIMPLE_NS_XMLFILE) as f:
605 data = f.read()
606 class Builder(list):
607 def start(self, tag, attrib):
608 self.append(("start", tag))
609 def end(self, tag):
610 self.append(("end", tag))
611 def data(self, text):
612 pass
613 def pi(self, target, data):
614 self.append(("pi", target, data))
615 def comment(self, data):
616 self.append(("comment", data))
617 builder = Builder()
618 parser = ET.XMLParser(target=builder)
619 parser.feed(data)
620 self.assertEqual(builder, [
621 ('pi', 'pi', 'data'),
622 ('comment', ' comment '),
623 ('start', '{namespace}root'),
624 ('start', '{namespace}element'),
625 ('end', '{namespace}element'),
626 ('start', '{namespace}element'),
627 ('end', '{namespace}element'),
628 ('start', '{namespace}empty-element'),
629 ('end', '{namespace}empty-element'),
630 ('end', '{namespace}root'),
631 ])
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000632
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000633
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200634 def test_getchildren(self):
635 # Test Element.getchildren()
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000636
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200637 with open(SIMPLE_XMLFILE, "rb") as f:
638 tree = ET.parse(f)
639 self.assertEqual([summarize_list(elem.getchildren())
640 for elem in tree.getroot().iter()], [
641 ['element', 'element', 'empty-element'],
642 [],
643 [],
644 [],
645 ])
646 self.assertEqual([summarize_list(elem.getchildren())
647 for elem in tree.getiterator()], [
648 ['element', 'element', 'empty-element'],
649 [],
650 [],
651 [],
652 ])
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000653
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200654 elem = ET.XML(SAMPLE_XML)
655 self.assertEqual(len(elem.getchildren()), 3)
656 self.assertEqual(len(elem[2].getchildren()), 1)
657 self.assertEqual(elem[:], elem.getchildren())
658 child1 = elem[0]
659 child2 = elem[2]
660 del elem[1:2]
661 self.assertEqual(len(elem.getchildren()), 2)
662 self.assertEqual(child1, elem[0])
663 self.assertEqual(child2, elem[1])
664 elem[0:2] = [child2, child1]
665 self.assertEqual(child2, elem[0])
666 self.assertEqual(child1, elem[1])
667 self.assertNotEqual(child1, elem[0])
668 elem.clear()
669 self.assertEqual(elem.getchildren(), [])
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000670
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200671 def test_writestring(self):
672 elem = ET.XML("<html><body>text</body></html>")
673 self.assertEqual(ET.tostring(elem), b'<html><body>text</body></html>')
674 elem = ET.fromstring("<html><body>text</body></html>")
675 self.assertEqual(ET.tostring(elem), b'<html><body>text</body></html>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000676
Serhiy Storchaka66d53fa2013-05-22 17:07:51 +0300677 def test_encoding(self):
678 def check(encoding, body=''):
679 xml = ("<?xml version='1.0' encoding='%s'?><xml>%s</xml>" %
680 (encoding, body))
681 self.assertEqual(ET.XML(xml.encode(encoding)).text, body)
682 self.assertEqual(ET.XML(xml).text, body)
683 check("ascii", 'a')
684 check("us-ascii", 'a')
685 check("iso-8859-1", '\xbd')
686 check("iso-8859-15", '\u20ac')
687 check("cp437", '\u221a')
688 check("mac-roman", '\u02da')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000689
Eli Bendersky6dc32b32013-05-25 05:25:48 -0700690 def xml(encoding):
691 return "<?xml version='1.0' encoding='%s'?><xml />" % encoding
692 def bxml(encoding):
693 return xml(encoding).encode(encoding)
694 supported_encodings = [
695 'ascii', 'utf-8', 'utf-8-sig', 'utf-16', 'utf-16be', 'utf-16le',
696 'iso8859-1', 'iso8859-2', 'iso8859-3', 'iso8859-4', 'iso8859-5',
697 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10',
698 'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16',
699 'cp437', 'cp720', 'cp737', 'cp775', 'cp850', 'cp852',
700 'cp855', 'cp856', 'cp857', 'cp858', 'cp860', 'cp861', 'cp862',
Serhiy Storchakabe0c3252013-11-23 18:52:23 +0200701 'cp863', 'cp865', 'cp866', 'cp869', 'cp874', 'cp1006', 'cp1125',
702 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255',
703 'cp1256', 'cp1257', 'cp1258',
Eli Bendersky6dc32b32013-05-25 05:25:48 -0700704 'mac-cyrillic', 'mac-greek', 'mac-iceland', 'mac-latin2',
705 'mac-roman', 'mac-turkish',
706 'iso2022-jp', 'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004',
707 'iso2022-jp-3', 'iso2022-jp-ext',
708 'koi8-r', 'koi8-u',
709 'hz', 'ptcp154',
710 ]
711 for encoding in supported_encodings:
712 self.assertEqual(ET.tostring(ET.XML(bxml(encoding))), b'<xml />')
713
714 unsupported_ascii_compatible_encodings = [
715 'big5', 'big5hkscs',
716 'cp932', 'cp949', 'cp950',
717 'euc-jp', 'euc-jis-2004', 'euc-jisx0213', 'euc-kr',
718 'gb2312', 'gbk', 'gb18030',
719 'iso2022-kr', 'johab',
720 'shift-jis', 'shift-jis-2004', 'shift-jisx0213',
721 'utf-7',
722 ]
723 for encoding in unsupported_ascii_compatible_encodings:
724 self.assertRaises(ValueError, ET.XML, bxml(encoding))
725
726 unsupported_ascii_incompatible_encodings = [
727 'cp037', 'cp424', 'cp500', 'cp864', 'cp875', 'cp1026', 'cp1140',
728 'utf_32', 'utf_32_be', 'utf_32_le',
729 ]
730 for encoding in unsupported_ascii_incompatible_encodings:
731 self.assertRaises(ET.ParseError, ET.XML, bxml(encoding))
732
733 self.assertRaises(ValueError, ET.XML, xml('undefined').encode('ascii'))
734 self.assertRaises(LookupError, ET.XML, xml('xxx').encode('ascii'))
735
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200736 def test_methods(self):
737 # Test serialization methods.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000738
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200739 e = ET.XML("<html><link/><script>1 &lt; 2</script></html>")
740 e.tail = "\n"
741 self.assertEqual(serialize(e),
742 '<html><link /><script>1 &lt; 2</script></html>\n')
743 self.assertEqual(serialize(e, method=None),
744 '<html><link /><script>1 &lt; 2</script></html>\n')
745 self.assertEqual(serialize(e, method="xml"),
746 '<html><link /><script>1 &lt; 2</script></html>\n')
747 self.assertEqual(serialize(e, method="html"),
748 '<html><link><script>1 < 2</script></html>\n')
749 self.assertEqual(serialize(e, method="text"), '1 < 2\n')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000750
Christian Heimes54ad7e32013-07-05 01:39:49 +0200751 def test_issue18347(self):
752 e = ET.XML('<html><CamelCase>text</CamelCase></html>')
753 self.assertEqual(serialize(e),
754 '<html><CamelCase>text</CamelCase></html>')
755 self.assertEqual(serialize(e, method="html"),
756 '<html><CamelCase>text</CamelCase></html>')
757
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200758 def test_entity(self):
759 # Test entity handling.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000760
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200761 # 1) good entities
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000762
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200763 e = ET.XML("<document title='&#x8230;'>test</document>")
764 self.assertEqual(serialize(e, encoding="us-ascii"),
765 b'<document title="&#33328;">test</document>')
766 self.serialize_check(e, '<document title="\u8230">test</document>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000767
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200768 # 2) bad entities
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000769
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200770 with self.assertRaises(ET.ParseError) as cm:
771 ET.XML("<document>&entity;</document>")
772 self.assertEqual(str(cm.exception),
773 'undefined entity: line 1, column 10')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000774
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200775 with self.assertRaises(ET.ParseError) as cm:
776 ET.XML(ENTITY_XML)
777 self.assertEqual(str(cm.exception),
778 'undefined entity &entity;: line 5, column 10')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000779
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200780 # 3) custom entity
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000781
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200782 parser = ET.XMLParser()
783 parser.entity["entity"] = "text"
784 parser.feed(ENTITY_XML)
785 root = parser.close()
786 self.serialize_check(root, '<document>text</document>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000787
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200788 def test_namespace(self):
789 # Test namespace issues.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000790
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200791 # 1) xml namespace
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000792
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200793 elem = ET.XML("<tag xml:lang='en' />")
794 self.serialize_check(elem, '<tag xml:lang="en" />') # 1.1
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000795
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200796 # 2) other "well-known" namespaces
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000797
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200798 elem = ET.XML("<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' />")
799 self.serialize_check(elem,
800 '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" />') # 2.1
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000801
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200802 elem = ET.XML("<html:html xmlns:html='http://www.w3.org/1999/xhtml' />")
803 self.serialize_check(elem,
804 '<html:html xmlns:html="http://www.w3.org/1999/xhtml" />') # 2.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000805
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200806 elem = ET.XML("<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope' />")
807 self.serialize_check(elem,
808 '<ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope" />') # 2.3
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000809
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200810 # 3) unknown namespaces
811 elem = ET.XML(SAMPLE_XML_NS)
812 self.serialize_check(elem,
813 '<ns0:body xmlns:ns0="http://effbot.org/ns">\n'
814 ' <ns0:tag>text</ns0:tag>\n'
815 ' <ns0:tag />\n'
816 ' <ns0:section>\n'
817 ' <ns0:tag>subtext</ns0:tag>\n'
818 ' </ns0:section>\n'
819 '</ns0:body>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000820
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200821 def test_qname(self):
822 # Test QName handling.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000823
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200824 # 1) decorated tags
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000825
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200826 elem = ET.Element("{uri}tag")
827 self.serialize_check(elem, '<ns0:tag xmlns:ns0="uri" />') # 1.1
828 elem = ET.Element(ET.QName("{uri}tag"))
829 self.serialize_check(elem, '<ns0:tag xmlns:ns0="uri" />') # 1.2
830 elem = ET.Element(ET.QName("uri", "tag"))
831 self.serialize_check(elem, '<ns0:tag xmlns:ns0="uri" />') # 1.3
832 elem = ET.Element(ET.QName("uri", "tag"))
833 subelem = ET.SubElement(elem, ET.QName("uri", "tag1"))
834 subelem = ET.SubElement(elem, ET.QName("uri", "tag2"))
835 self.serialize_check(elem,
836 '<ns0:tag xmlns:ns0="uri"><ns0:tag1 /><ns0:tag2 /></ns0:tag>') # 1.4
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000837
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200838 # 2) decorated attributes
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000839
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200840 elem.clear()
841 elem.attrib["{uri}key"] = "value"
842 self.serialize_check(elem,
843 '<ns0:tag xmlns:ns0="uri" ns0:key="value" />') # 2.1
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000844
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200845 elem.clear()
846 elem.attrib[ET.QName("{uri}key")] = "value"
847 self.serialize_check(elem,
848 '<ns0:tag xmlns:ns0="uri" ns0:key="value" />') # 2.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000849
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200850 # 3) decorated values are not converted by default, but the
851 # QName wrapper can be used for values
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000852
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200853 elem.clear()
854 elem.attrib["{uri}key"] = "{uri}value"
855 self.serialize_check(elem,
856 '<ns0:tag xmlns:ns0="uri" ns0:key="{uri}value" />') # 3.1
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000857
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200858 elem.clear()
859 elem.attrib["{uri}key"] = ET.QName("{uri}value")
860 self.serialize_check(elem,
861 '<ns0:tag xmlns:ns0="uri" ns0:key="ns0:value" />') # 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000862
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200863 elem.clear()
864 subelem = ET.Element("tag")
865 subelem.attrib["{uri1}key"] = ET.QName("{uri2}value")
866 elem.append(subelem)
867 elem.append(subelem)
868 self.serialize_check(elem,
869 '<ns0:tag xmlns:ns0="uri" xmlns:ns1="uri1" xmlns:ns2="uri2">'
870 '<tag ns1:key="ns2:value" />'
871 '<tag ns1:key="ns2:value" />'
872 '</ns0:tag>') # 3.3
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000873
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200874 # 4) Direct QName tests
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000875
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200876 self.assertEqual(str(ET.QName('ns', 'tag')), '{ns}tag')
877 self.assertEqual(str(ET.QName('{ns}tag')), '{ns}tag')
878 q1 = ET.QName('ns', 'tag')
879 q2 = ET.QName('ns', 'tag')
880 self.assertEqual(q1, q2)
881 q2 = ET.QName('ns', 'other-tag')
882 self.assertNotEqual(q1, q2)
883 self.assertNotEqual(q1, 'ns:tag')
884 self.assertEqual(q1, '{ns}tag')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000885
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200886 def test_doctype_public(self):
887 # Test PUBLIC doctype.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000888
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200889 elem = ET.XML('<!DOCTYPE html PUBLIC'
890 ' "-//W3C//DTD XHTML 1.0 Transitional//EN"'
891 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
892 '<html>text</html>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000893
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200894 def test_xpath_tokenizer(self):
895 # Test the XPath tokenizer.
896 from xml.etree import ElementPath
897 def check(p, expected):
898 self.assertEqual([op or tag
899 for op, tag in ElementPath.xpath_tokenizer(p)],
900 expected)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000901
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200902 # tests from the xml specification
903 check("*", ['*'])
904 check("text()", ['text', '()'])
905 check("@name", ['@', 'name'])
906 check("@*", ['@', '*'])
907 check("para[1]", ['para', '[', '1', ']'])
908 check("para[last()]", ['para', '[', 'last', '()', ']'])
909 check("*/para", ['*', '/', 'para'])
910 check("/doc/chapter[5]/section[2]",
911 ['/', 'doc', '/', 'chapter', '[', '5', ']',
912 '/', 'section', '[', '2', ']'])
913 check("chapter//para", ['chapter', '//', 'para'])
914 check("//para", ['//', 'para'])
915 check("//olist/item", ['//', 'olist', '/', 'item'])
916 check(".", ['.'])
917 check(".//para", ['.', '//', 'para'])
918 check("..", ['..'])
919 check("../@lang", ['..', '/', '@', 'lang'])
920 check("chapter[title]", ['chapter', '[', 'title', ']'])
921 check("employee[@secretary and @assistant]", ['employee',
922 '[', '@', 'secretary', '', 'and', '', '@', 'assistant', ']'])
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000923
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200924 # additional tests
925 check("{http://spam}egg", ['{http://spam}egg'])
926 check("./spam.egg", ['.', '/', 'spam.egg'])
927 check(".//{http://spam}egg", ['.', '//', '{http://spam}egg'])
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000928
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200929 def test_processinginstruction(self):
930 # Test ProcessingInstruction directly
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000931
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200932 self.assertEqual(ET.tostring(ET.ProcessingInstruction('test', 'instruction')),
933 b'<?test instruction?>')
934 self.assertEqual(ET.tostring(ET.PI('test', 'instruction')),
935 b'<?test instruction?>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000936
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200937 # Issue #2746
Antoine Pitrou99f69ee2010-02-09 17:25:47 +0000938
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200939 self.assertEqual(ET.tostring(ET.PI('test', '<testing&>')),
940 b'<?test <testing&>?>')
941 self.assertEqual(ET.tostring(ET.PI('test', '<testing&>\xe3'), 'latin-1'),
942 b"<?xml version='1.0' encoding='latin-1'?>\n"
943 b"<?test <testing&>\xe3?>")
Antoine Pitrou99f69ee2010-02-09 17:25:47 +0000944
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +0200945 def test_html_empty_elems_serialization(self):
946 # issue 15970
947 # from http://www.w3.org/TR/html401/index/elements.html
948 for element in ['AREA', 'BASE', 'BASEFONT', 'BR', 'COL', 'FRAME', 'HR',
949 'IMG', 'INPUT', 'ISINDEX', 'LINK', 'META', 'PARAM']:
950 for elem in [element, element.lower()]:
951 expected = '<%s>' % elem
952 serialized = serialize(ET.XML('<%s />' % elem), method='html')
953 self.assertEqual(serialized, expected)
954 serialized = serialize(ET.XML('<%s></%s>' % (elem,elem)),
955 method='html')
956 self.assertEqual(serialized, expected)
Antoine Pitrou99f69ee2010-02-09 17:25:47 +0000957
Fredrik Lundh8911ca3d2005-12-16 22:07:17 +0000958
Eli Benderskyb5869342013-08-30 05:51:20 -0700959class XMLPullParserTest(unittest.TestCase):
Antoine Pitrou5b235d02013-04-18 19:37:06 +0200960
961 def _feed(self, parser, data, chunk_size=None):
962 if chunk_size is None:
Eli Benderskyb5869342013-08-30 05:51:20 -0700963 parser.feed(data)
Antoine Pitrou5b235d02013-04-18 19:37:06 +0200964 else:
965 for i in range(0, len(data), chunk_size):
Eli Benderskyb5869342013-08-30 05:51:20 -0700966 parser.feed(data[i:i+chunk_size])
Antoine Pitrou5b235d02013-04-18 19:37:06 +0200967
968 def assert_event_tags(self, parser, expected):
Eli Benderskyb5869342013-08-30 05:51:20 -0700969 events = parser.read_events()
Antoine Pitrou5b235d02013-04-18 19:37:06 +0200970 self.assertEqual([(action, elem.tag) for action, elem in events],
971 expected)
972
973 def test_simple_xml(self):
974 for chunk_size in (None, 1, 5):
975 with self.subTest(chunk_size=chunk_size):
Eli Benderskyb5869342013-08-30 05:51:20 -0700976 parser = ET.XMLPullParser()
Antoine Pitrou5b235d02013-04-18 19:37:06 +0200977 self.assert_event_tags(parser, [])
978 self._feed(parser, "<!-- comment -->\n", chunk_size)
979 self.assert_event_tags(parser, [])
980 self._feed(parser,
981 "<root>\n <element key='value'>text</element",
982 chunk_size)
983 self.assert_event_tags(parser, [])
984 self._feed(parser, ">\n", chunk_size)
985 self.assert_event_tags(parser, [('end', 'element')])
986 self._feed(parser, "<element>text</element>tail\n", chunk_size)
987 self._feed(parser, "<empty-element/>\n", chunk_size)
988 self.assert_event_tags(parser, [
989 ('end', 'element'),
990 ('end', 'empty-element'),
991 ])
992 self._feed(parser, "</root>\n", chunk_size)
993 self.assert_event_tags(parser, [('end', 'root')])
Nick Coghlan4cc2afa2013-09-28 23:50:35 +1000994 self.assertIsNone(parser.close())
Antoine Pitrou5b235d02013-04-18 19:37:06 +0200995
Eli Benderskyb5869342013-08-30 05:51:20 -0700996 def test_feed_while_iterating(self):
997 parser = ET.XMLPullParser()
998 it = parser.read_events()
Antoine Pitrou5b235d02013-04-18 19:37:06 +0200999 self._feed(parser, "<root>\n <element key='value'>text</element>\n")
1000 action, elem = next(it)
1001 self.assertEqual((action, elem.tag), ('end', 'element'))
1002 self._feed(parser, "</root>\n")
1003 action, elem = next(it)
1004 self.assertEqual((action, elem.tag), ('end', 'root'))
1005 with self.assertRaises(StopIteration):
1006 next(it)
1007
1008 def test_simple_xml_with_ns(self):
Eli Benderskyb5869342013-08-30 05:51:20 -07001009 parser = ET.XMLPullParser()
Antoine Pitrou5b235d02013-04-18 19:37:06 +02001010 self.assert_event_tags(parser, [])
1011 self._feed(parser, "<!-- comment -->\n")
1012 self.assert_event_tags(parser, [])
1013 self._feed(parser, "<root xmlns='namespace'>\n")
1014 self.assert_event_tags(parser, [])
1015 self._feed(parser, "<element key='value'>text</element")
1016 self.assert_event_tags(parser, [])
1017 self._feed(parser, ">\n")
1018 self.assert_event_tags(parser, [('end', '{namespace}element')])
1019 self._feed(parser, "<element>text</element>tail\n")
1020 self._feed(parser, "<empty-element/>\n")
1021 self.assert_event_tags(parser, [
1022 ('end', '{namespace}element'),
1023 ('end', '{namespace}empty-element'),
1024 ])
1025 self._feed(parser, "</root>\n")
1026 self.assert_event_tags(parser, [('end', '{namespace}root')])
Nick Coghlan4cc2afa2013-09-28 23:50:35 +10001027 self.assertIsNone(parser.close())
Antoine Pitrou5b235d02013-04-18 19:37:06 +02001028
Eli Bendersky3a4fbd82013-05-19 09:01:49 -07001029 def test_ns_events(self):
Eli Benderskyb5869342013-08-30 05:51:20 -07001030 parser = ET.XMLPullParser(events=('start-ns', 'end-ns'))
Eli Bendersky3a4fbd82013-05-19 09:01:49 -07001031 self._feed(parser, "<!-- comment -->\n")
1032 self._feed(parser, "<root xmlns='namespace'>\n")
1033 self.assertEqual(
Eli Benderskyb5869342013-08-30 05:51:20 -07001034 list(parser.read_events()),
Eli Bendersky3a4fbd82013-05-19 09:01:49 -07001035 [('start-ns', ('', 'namespace'))])
1036 self._feed(parser, "<element key='value'>text</element")
1037 self._feed(parser, ">\n")
1038 self._feed(parser, "<element>text</element>tail\n")
1039 self._feed(parser, "<empty-element/>\n")
1040 self._feed(parser, "</root>\n")
Eli Benderskyb5869342013-08-30 05:51:20 -07001041 self.assertEqual(list(parser.read_events()), [('end-ns', None)])
Nick Coghlan4cc2afa2013-09-28 23:50:35 +10001042 self.assertIsNone(parser.close())
Eli Bendersky3a4fbd82013-05-19 09:01:49 -07001043
Antoine Pitrou5b235d02013-04-18 19:37:06 +02001044 def test_events(self):
Eli Benderskyb5869342013-08-30 05:51:20 -07001045 parser = ET.XMLPullParser(events=())
Antoine Pitrou5b235d02013-04-18 19:37:06 +02001046 self._feed(parser, "<root/>\n")
1047 self.assert_event_tags(parser, [])
1048
Eli Benderskyb5869342013-08-30 05:51:20 -07001049 parser = ET.XMLPullParser(events=('start', 'end'))
Antoine Pitrou5b235d02013-04-18 19:37:06 +02001050 self._feed(parser, "<!-- comment -->\n")
1051 self.assert_event_tags(parser, [])
1052 self._feed(parser, "<root>\n")
1053 self.assert_event_tags(parser, [('start', 'root')])
1054 self._feed(parser, "<element key='value'>text</element")
1055 self.assert_event_tags(parser, [('start', 'element')])
1056 self._feed(parser, ">\n")
1057 self.assert_event_tags(parser, [('end', 'element')])
1058 self._feed(parser,
1059 "<element xmlns='foo'>text<empty-element/></element>tail\n")
1060 self.assert_event_tags(parser, [
1061 ('start', '{foo}element'),
1062 ('start', '{foo}empty-element'),
1063 ('end', '{foo}empty-element'),
1064 ('end', '{foo}element'),
1065 ])
1066 self._feed(parser, "</root>")
Nick Coghlan4cc2afa2013-09-28 23:50:35 +10001067 self.assertIsNone(parser.close())
Antoine Pitrou5b235d02013-04-18 19:37:06 +02001068 self.assert_event_tags(parser, [('end', 'root')])
Antoine Pitrou5b235d02013-04-18 19:37:06 +02001069
Eli Benderskyb5869342013-08-30 05:51:20 -07001070 parser = ET.XMLPullParser(events=('start',))
Antoine Pitrou5b235d02013-04-18 19:37:06 +02001071 self._feed(parser, "<!-- comment -->\n")
1072 self.assert_event_tags(parser, [])
1073 self._feed(parser, "<root>\n")
1074 self.assert_event_tags(parser, [('start', 'root')])
1075 self._feed(parser, "<element key='value'>text</element")
1076 self.assert_event_tags(parser, [('start', 'element')])
1077 self._feed(parser, ">\n")
1078 self.assert_event_tags(parser, [])
1079 self._feed(parser,
1080 "<element xmlns='foo'>text<empty-element/></element>tail\n")
1081 self.assert_event_tags(parser, [
1082 ('start', '{foo}element'),
1083 ('start', '{foo}empty-element'),
1084 ])
1085 self._feed(parser, "</root>")
Nick Coghlan4cc2afa2013-09-28 23:50:35 +10001086 self.assertIsNone(parser.close())
Antoine Pitrou5b235d02013-04-18 19:37:06 +02001087
Eli Bendersky3a4fbd82013-05-19 09:01:49 -07001088 def test_events_sequence(self):
1089 # Test that events can be some sequence that's not just a tuple or list
1090 eventset = {'end', 'start'}
Eli Benderskyb5869342013-08-30 05:51:20 -07001091 parser = ET.XMLPullParser(events=eventset)
Eli Bendersky3a4fbd82013-05-19 09:01:49 -07001092 self._feed(parser, "<foo>bar</foo>")
1093 self.assert_event_tags(parser, [('start', 'foo'), ('end', 'foo')])
1094
1095 class DummyIter:
1096 def __init__(self):
1097 self.events = iter(['start', 'end', 'start-ns'])
1098 def __iter__(self):
1099 return self
1100 def __next__(self):
1101 return next(self.events)
1102
Eli Benderskyb5869342013-08-30 05:51:20 -07001103 parser = ET.XMLPullParser(events=DummyIter())
Eli Bendersky3a4fbd82013-05-19 09:01:49 -07001104 self._feed(parser, "<foo>bar</foo>")
1105 self.assert_event_tags(parser, [('start', 'foo'), ('end', 'foo')])
1106
1107
Antoine Pitrou5b235d02013-04-18 19:37:06 +02001108 def test_unknown_event(self):
1109 with self.assertRaises(ValueError):
Eli Benderskyb5869342013-08-30 05:51:20 -07001110 ET.XMLPullParser(events=('start', 'end', 'bogus'))
Antoine Pitrou5b235d02013-04-18 19:37:06 +02001111
1112
Armin Rigo9ed73062005-12-14 18:10:45 +00001113#
1114# xinclude tests (samples from appendix C of the xinclude specification)
1115
1116XINCLUDE = {}
1117
1118XINCLUDE["C1.xml"] = """\
1119<?xml version='1.0'?>
1120<document xmlns:xi="http://www.w3.org/2001/XInclude">
1121 <p>120 Mz is adequate for an average home user.</p>
1122 <xi:include href="disclaimer.xml"/>
1123</document>
1124"""
1125
1126XINCLUDE["disclaimer.xml"] = """\
1127<?xml version='1.0'?>
1128<disclaimer>
1129 <p>The opinions represented herein represent those of the individual
1130 and should not be interpreted as official policy endorsed by this
1131 organization.</p>
1132</disclaimer>
1133"""
1134
1135XINCLUDE["C2.xml"] = """\
1136<?xml version='1.0'?>
1137<document xmlns:xi="http://www.w3.org/2001/XInclude">
1138 <p>This document has been accessed
1139 <xi:include href="count.txt" parse="text"/> times.</p>
1140</document>
1141"""
1142
1143XINCLUDE["count.txt"] = "324387"
1144
Florent Xiclunaba8a9862010-08-08 23:08:41 +00001145XINCLUDE["C2b.xml"] = """\
1146<?xml version='1.0'?>
1147<document xmlns:xi="http://www.w3.org/2001/XInclude">
1148 <p>This document has been <em>accessed</em>
1149 <xi:include href="count.txt" parse="text"/> times.</p>
1150</document>
1151"""
1152
Armin Rigo9ed73062005-12-14 18:10:45 +00001153XINCLUDE["C3.xml"] = """\
1154<?xml version='1.0'?>
1155<document xmlns:xi="http://www.w3.org/2001/XInclude">
1156 <p>The following is the source of the "data.xml" resource:</p>
1157 <example><xi:include href="data.xml" parse="text"/></example>
1158</document>
1159"""
1160
1161XINCLUDE["data.xml"] = """\
1162<?xml version='1.0'?>
1163<data>
1164 <item><![CDATA[Brooks & Shields]]></item>
1165</data>
1166"""
1167
1168XINCLUDE["C5.xml"] = """\
1169<?xml version='1.0'?>
1170<div xmlns:xi="http://www.w3.org/2001/XInclude">
1171 <xi:include href="example.txt" parse="text">
1172 <xi:fallback>
1173 <xi:include href="fallback-example.txt" parse="text">
1174 <xi:fallback><a href="mailto:bob@example.org">Report error</a></xi:fallback>
1175 </xi:include>
1176 </xi:fallback>
1177 </xi:include>
1178</div>
1179"""
1180
1181XINCLUDE["default.xml"] = """\
1182<?xml version='1.0'?>
1183<document xmlns:xi="http://www.w3.org/2001/XInclude">
1184 <p>Example.</p>
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001185 <xi:include href="{}"/>
Armin Rigo9ed73062005-12-14 18:10:45 +00001186</document>
Georg Brandl1f7fffb2010-10-15 15:57:45 +00001187""".format(html.escape(SIMPLE_XMLFILE, True))
Armin Rigo9ed73062005-12-14 18:10:45 +00001188
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001189#
1190# badly formatted xi:include tags
1191
1192XINCLUDE_BAD = {}
1193
1194XINCLUDE_BAD["B1.xml"] = """\
1195<?xml version='1.0'?>
1196<document xmlns:xi="http://www.w3.org/2001/XInclude">
1197 <p>120 Mz is adequate for an average home user.</p>
1198 <xi:include href="disclaimer.xml" parse="BAD_TYPE"/>
1199</document>
1200"""
1201
1202XINCLUDE_BAD["B2.xml"] = """\
1203<?xml version='1.0'?>
1204<div xmlns:xi="http://www.w3.org/2001/XInclude">
1205 <xi:fallback></xi:fallback>
1206</div>
1207"""
1208
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001209class XIncludeTest(unittest.TestCase):
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001210
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001211 def xinclude_loader(self, href, parse="xml", encoding=None):
1212 try:
1213 data = XINCLUDE[href]
1214 except KeyError:
1215 raise OSError("resource not found")
1216 if parse == "xml":
1217 data = ET.XML(data)
1218 return data
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001219
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001220 def none_loader(self, href, parser, encoding=None):
1221 return None
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001222
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001223 def _my_loader(self, href, parse):
1224 # Used to avoid a test-dependency problem where the default loader
1225 # of ElementInclude uses the pyET parser for cET tests.
1226 if parse == 'xml':
1227 with open(href, 'rb') as f:
1228 return ET.parse(f).getroot()
1229 else:
1230 return None
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001231
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001232 def test_xinclude_default(self):
1233 from xml.etree import ElementInclude
1234 doc = self.xinclude_loader('default.xml')
1235 ElementInclude.include(doc, self._my_loader)
1236 self.assertEqual(serialize(doc),
1237 '<document>\n'
1238 ' <p>Example.</p>\n'
1239 ' <root>\n'
1240 ' <element key="value">text</element>\n'
1241 ' <element>text</element>tail\n'
1242 ' <empty-element />\n'
1243 '</root>\n'
1244 '</document>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001245
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001246 def test_xinclude(self):
1247 from xml.etree import ElementInclude
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001248
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001249 # Basic inclusion example (XInclude C.1)
1250 document = self.xinclude_loader("C1.xml")
1251 ElementInclude.include(document, self.xinclude_loader)
1252 self.assertEqual(serialize(document),
1253 '<document>\n'
1254 ' <p>120 Mz is adequate for an average home user.</p>\n'
1255 ' <disclaimer>\n'
1256 ' <p>The opinions represented herein represent those of the individual\n'
1257 ' and should not be interpreted as official policy endorsed by this\n'
1258 ' organization.</p>\n'
1259 '</disclaimer>\n'
1260 '</document>') # C1
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001261
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001262 # Textual inclusion example (XInclude C.2)
1263 document = self.xinclude_loader("C2.xml")
1264 ElementInclude.include(document, self.xinclude_loader)
1265 self.assertEqual(serialize(document),
1266 '<document>\n'
1267 ' <p>This document has been accessed\n'
1268 ' 324387 times.</p>\n'
1269 '</document>') # C2
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001270
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001271 # Textual inclusion after sibling element (based on modified XInclude C.2)
1272 document = self.xinclude_loader("C2b.xml")
1273 ElementInclude.include(document, self.xinclude_loader)
1274 self.assertEqual(serialize(document),
1275 '<document>\n'
1276 ' <p>This document has been <em>accessed</em>\n'
1277 ' 324387 times.</p>\n'
1278 '</document>') # C2b
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001279
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001280 # Textual inclusion of XML example (XInclude C.3)
1281 document = self.xinclude_loader("C3.xml")
1282 ElementInclude.include(document, self.xinclude_loader)
1283 self.assertEqual(serialize(document),
1284 '<document>\n'
1285 ' <p>The following is the source of the "data.xml" resource:</p>\n'
1286 " <example>&lt;?xml version='1.0'?&gt;\n"
1287 '&lt;data&gt;\n'
1288 ' &lt;item&gt;&lt;![CDATA[Brooks &amp; Shields]]&gt;&lt;/item&gt;\n'
1289 '&lt;/data&gt;\n'
1290 '</example>\n'
1291 '</document>') # C3
1292
1293 # Fallback example (XInclude C.5)
1294 # Note! Fallback support is not yet implemented
1295 document = self.xinclude_loader("C5.xml")
1296 with self.assertRaises(OSError) as cm:
1297 ElementInclude.include(document, self.xinclude_loader)
1298 self.assertEqual(str(cm.exception), 'resource not found')
1299 self.assertEqual(serialize(document),
1300 '<div xmlns:ns0="http://www.w3.org/2001/XInclude">\n'
1301 ' <ns0:include href="example.txt" parse="text">\n'
1302 ' <ns0:fallback>\n'
1303 ' <ns0:include href="fallback-example.txt" parse="text">\n'
1304 ' <ns0:fallback><a href="mailto:bob@example.org">Report error</a></ns0:fallback>\n'
1305 ' </ns0:include>\n'
1306 ' </ns0:fallback>\n'
1307 ' </ns0:include>\n'
1308 '</div>') # C5
1309
1310 def test_xinclude_failures(self):
1311 from xml.etree import ElementInclude
1312
1313 # Test failure to locate included XML file.
1314 document = ET.XML(XINCLUDE["C1.xml"])
1315 with self.assertRaises(ElementInclude.FatalIncludeError) as cm:
1316 ElementInclude.include(document, loader=self.none_loader)
1317 self.assertEqual(str(cm.exception),
1318 "cannot load 'disclaimer.xml' as 'xml'")
1319
1320 # Test failure to locate included text file.
1321 document = ET.XML(XINCLUDE["C2.xml"])
1322 with self.assertRaises(ElementInclude.FatalIncludeError) as cm:
1323 ElementInclude.include(document, loader=self.none_loader)
1324 self.assertEqual(str(cm.exception),
1325 "cannot load 'count.txt' as 'text'")
1326
1327 # Test bad parse type.
1328 document = ET.XML(XINCLUDE_BAD["B1.xml"])
1329 with self.assertRaises(ElementInclude.FatalIncludeError) as cm:
1330 ElementInclude.include(document, loader=self.none_loader)
1331 self.assertEqual(str(cm.exception),
1332 "unknown parse type in xi:include tag ('BAD_TYPE')")
1333
1334 # Test xi:fallback outside xi:include.
1335 document = ET.XML(XINCLUDE_BAD["B2.xml"])
1336 with self.assertRaises(ElementInclude.FatalIncludeError) as cm:
1337 ElementInclude.include(document, loader=self.none_loader)
1338 self.assertEqual(str(cm.exception),
1339 "xi:fallback tag must be child of xi:include "
1340 "('{http://www.w3.org/2001/XInclude}fallback')")
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001341
1342# --------------------------------------------------------------------
1343# reported bugs
1344
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001345class BugsTest(unittest.TestCase):
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001346
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001347 def test_bug_xmltoolkit21(self):
1348 # marshaller gives obscure errors for non-string values
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001349
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001350 def check(elem):
1351 with self.assertRaises(TypeError) as cm:
1352 serialize(elem)
1353 self.assertEqual(str(cm.exception),
1354 'cannot serialize 123 (type int)')
Armin Rigo9ed73062005-12-14 18:10:45 +00001355
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001356 elem = ET.Element(123)
1357 check(elem) # tag
Armin Rigo9ed73062005-12-14 18:10:45 +00001358
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001359 elem = ET.Element("elem")
1360 elem.text = 123
1361 check(elem) # text
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001362
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001363 elem = ET.Element("elem")
1364 elem.tail = 123
1365 check(elem) # tail
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001366
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001367 elem = ET.Element("elem")
1368 elem.set(123, "123")
1369 check(elem) # attribute key
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001370
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001371 elem = ET.Element("elem")
1372 elem.set("123", 123)
1373 check(elem) # attribute value
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001374
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001375 def test_bug_xmltoolkit25(self):
1376 # typo in ElementTree.findtext
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001377
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001378 elem = ET.XML(SAMPLE_XML)
1379 tree = ET.ElementTree(elem)
1380 self.assertEqual(tree.findtext("tag"), 'text')
1381 self.assertEqual(tree.findtext("section/tag"), 'subtext')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001382
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001383 def test_bug_xmltoolkit28(self):
1384 # .//tag causes exceptions
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001385
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001386 tree = ET.XML("<doc><table><tbody/></table></doc>")
1387 self.assertEqual(summarize_list(tree.findall(".//thead")), [])
1388 self.assertEqual(summarize_list(tree.findall(".//tbody")), ['tbody'])
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001389
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001390 def test_bug_xmltoolkitX1(self):
1391 # dump() doesn't flush the output buffer
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001392
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001393 tree = ET.XML("<doc><table><tbody/></table></doc>")
1394 with support.captured_stdout() as stdout:
1395 ET.dump(tree)
1396 self.assertEqual(stdout.getvalue(), '<doc><table><tbody /></table></doc>\n')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001397
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001398 def test_bug_xmltoolkit39(self):
1399 # non-ascii element and attribute names doesn't work
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001400
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001401 tree = ET.XML(b"<?xml version='1.0' encoding='iso-8859-1'?><t\xe4g />")
1402 self.assertEqual(ET.tostring(tree, "utf-8"), b'<t\xc3\xa4g />')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001403
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001404 tree = ET.XML(b"<?xml version='1.0' encoding='iso-8859-1'?>"
1405 b"<tag \xe4ttr='v&#228;lue' />")
1406 self.assertEqual(tree.attrib, {'\xe4ttr': 'v\xe4lue'})
1407 self.assertEqual(ET.tostring(tree, "utf-8"),
1408 b'<tag \xc3\xa4ttr="v\xc3\xa4lue" />')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001409
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001410 tree = ET.XML(b"<?xml version='1.0' encoding='iso-8859-1'?>"
1411 b'<t\xe4g>text</t\xe4g>')
1412 self.assertEqual(ET.tostring(tree, "utf-8"),
1413 b'<t\xc3\xa4g>text</t\xc3\xa4g>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001414
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001415 tree = ET.Element("t\u00e4g")
1416 self.assertEqual(ET.tostring(tree, "utf-8"), b'<t\xc3\xa4g />')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001417
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001418 tree = ET.Element("tag")
1419 tree.set("\u00e4ttr", "v\u00e4lue")
1420 self.assertEqual(ET.tostring(tree, "utf-8"),
1421 b'<tag \xc3\xa4ttr="v\xc3\xa4lue" />')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001422
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001423 def test_bug_xmltoolkit54(self):
1424 # problems handling internally defined entities
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001425
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001426 e = ET.XML("<!DOCTYPE doc [<!ENTITY ldots '&#x8230;'>]>"
1427 '<doc>&ldots;</doc>')
1428 self.assertEqual(serialize(e, encoding="us-ascii"),
1429 b'<doc>&#33328;</doc>')
1430 self.assertEqual(serialize(e), '<doc>\u8230</doc>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001431
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001432 def test_bug_xmltoolkit55(self):
1433 # make sure we're reporting the first error, not the last
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001434
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001435 with self.assertRaises(ET.ParseError) as cm:
1436 ET.XML(b"<!DOCTYPE doc SYSTEM 'doc.dtd'>"
1437 b'<doc>&ldots;&ndots;&rdots;</doc>')
1438 self.assertEqual(str(cm.exception),
1439 'undefined entity &ldots;: line 1, column 36')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001440
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001441 def test_bug_xmltoolkit60(self):
1442 # Handle crash in stream source.
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001443
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001444 class ExceptionFile:
1445 def read(self, x):
1446 raise OSError
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001447
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001448 self.assertRaises(OSError, ET.parse, ExceptionFile())
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001449
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001450 def test_bug_xmltoolkit62(self):
1451 # Don't crash when using custom entities.
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001452
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001453 ENTITIES = {'rsquo': '\u2019', 'lsquo': '\u2018'}
Eli Benderskyc4e98a62013-05-19 09:24:43 -07001454 parser = ET.XMLParser()
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001455 parser.entity.update(ENTITIES)
1456 parser.feed("""<?xml version="1.0" encoding="UTF-8"?>
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001457<!DOCTYPE patent-application-publication SYSTEM "pap-v15-2001-01-31.dtd" []>
1458<patent-application-publication>
1459<subdoc-abstract>
1460<paragraph id="A-0001" lvl="0">A new cultivar of Begonia plant named &lsquo;BCT9801BEG&rsquo;.</paragraph>
1461</subdoc-abstract>
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001462</patent-application-publication>""")
1463 t = parser.close()
1464 self.assertEqual(t.find('.//paragraph').text,
1465 'A new cultivar of Begonia plant named \u2018BCT9801BEG\u2019.')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001466
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001467 def test_bug_xmltoolkit63(self):
1468 # Check reference leak.
1469 def xmltoolkit63():
1470 tree = ET.TreeBuilder()
1471 tree.start("tag", {})
1472 tree.data("text")
1473 tree.end("tag")
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001474
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001475 xmltoolkit63()
1476 count = sys.getrefcount(None)
1477 for i in range(1000):
1478 xmltoolkit63()
1479 self.assertEqual(sys.getrefcount(None), count)
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001480
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001481 def test_bug_200708_newline(self):
1482 # Preserve newlines in attributes.
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001483
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001484 e = ET.Element('SomeTag', text="def _f():\n return 3\n")
1485 self.assertEqual(ET.tostring(e),
1486 b'<SomeTag text="def _f():&#10; return 3&#10;" />')
1487 self.assertEqual(ET.XML(ET.tostring(e)).get("text"),
1488 'def _f():\n return 3\n')
1489 self.assertEqual(ET.tostring(ET.XML(ET.tostring(e))),
1490 b'<SomeTag text="def _f():&#10; return 3&#10;" />')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001491
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001492 def test_bug_200708_close(self):
1493 # Test default builder.
1494 parser = ET.XMLParser() # default
1495 parser.feed("<element>some text</element>")
1496 self.assertEqual(parser.close().tag, 'element')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001497
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001498 # Test custom builder.
1499 class EchoTarget:
1500 def close(self):
1501 return ET.Element("element") # simulate root
1502 parser = ET.XMLParser(EchoTarget())
1503 parser.feed("<element>some text</element>")
1504 self.assertEqual(parser.close().tag, 'element')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001505
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001506 def test_bug_200709_default_namespace(self):
1507 e = ET.Element("{default}elem")
1508 s = ET.SubElement(e, "{default}elem")
1509 self.assertEqual(serialize(e, default_namespace="default"), # 1
1510 '<elem xmlns="default"><elem /></elem>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001511
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001512 e = ET.Element("{default}elem")
1513 s = ET.SubElement(e, "{default}elem")
1514 s = ET.SubElement(e, "{not-default}elem")
1515 self.assertEqual(serialize(e, default_namespace="default"), # 2
1516 '<elem xmlns="default" xmlns:ns1="not-default">'
1517 '<elem />'
1518 '<ns1:elem />'
1519 '</elem>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001520
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001521 e = ET.Element("{default}elem")
1522 s = ET.SubElement(e, "{default}elem")
1523 s = ET.SubElement(e, "elem") # unprefixed name
1524 with self.assertRaises(ValueError) as cm:
1525 serialize(e, default_namespace="default") # 3
1526 self.assertEqual(str(cm.exception),
1527 'cannot use non-qualified names with default_namespace option')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001528
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001529 def test_bug_200709_register_namespace(self):
1530 e = ET.Element("{http://namespace.invalid/does/not/exist/}title")
1531 self.assertEqual(ET.tostring(e),
1532 b'<ns0:title xmlns:ns0="http://namespace.invalid/does/not/exist/" />')
1533 ET.register_namespace("foo", "http://namespace.invalid/does/not/exist/")
1534 e = ET.Element("{http://namespace.invalid/does/not/exist/}title")
1535 self.assertEqual(ET.tostring(e),
1536 b'<foo:title xmlns:foo="http://namespace.invalid/does/not/exist/" />')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001537
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001538 # And the Dublin Core namespace is in the default list:
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001539
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001540 e = ET.Element("{http://purl.org/dc/elements/1.1/}title")
1541 self.assertEqual(ET.tostring(e),
1542 b'<dc:title xmlns:dc="http://purl.org/dc/elements/1.1/" />')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001543
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001544 def test_bug_200709_element_comment(self):
1545 # Not sure if this can be fixed, really (since the serializer needs
1546 # ET.Comment, not cET.comment).
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001547
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001548 a = ET.Element('a')
1549 a.append(ET.Comment('foo'))
1550 self.assertEqual(a[0].tag, ET.Comment)
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001551
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001552 a = ET.Element('a')
1553 a.append(ET.PI('foo'))
1554 self.assertEqual(a[0].tag, ET.PI)
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001555
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001556 def test_bug_200709_element_insert(self):
1557 a = ET.Element('a')
1558 b = ET.SubElement(a, 'b')
1559 c = ET.SubElement(a, 'c')
1560 d = ET.Element('d')
1561 a.insert(0, d)
1562 self.assertEqual(summarize_list(a), ['d', 'b', 'c'])
1563 a.insert(-1, d)
1564 self.assertEqual(summarize_list(a), ['d', 'b', 'd', 'c'])
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001565
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001566 def test_bug_200709_iter_comment(self):
1567 a = ET.Element('a')
1568 b = ET.SubElement(a, 'b')
1569 comment_b = ET.Comment("TEST-b")
1570 b.append(comment_b)
1571 self.assertEqual(summarize_list(a.iter(ET.Comment)), [ET.Comment])
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001572
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001573 # --------------------------------------------------------------------
1574 # reported on bugs.python.org
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001575
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001576 def test_bug_1534630(self):
1577 bob = ET.TreeBuilder()
1578 e = bob.data("data")
1579 e = bob.start("tag", {})
1580 e = bob.end("tag")
1581 e = bob.close()
1582 self.assertEqual(serialize(e), '<tag />')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001583
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001584 def test_issue6233(self):
1585 e = ET.XML(b"<?xml version='1.0' encoding='utf-8'?>"
1586 b'<body>t\xc3\xa3g</body>')
1587 self.assertEqual(ET.tostring(e, 'ascii'),
1588 b"<?xml version='1.0' encoding='ascii'?>\n"
1589 b'<body>t&#227;g</body>')
1590 e = ET.XML(b"<?xml version='1.0' encoding='iso-8859-1'?>"
1591 b'<body>t\xe3g</body>')
1592 self.assertEqual(ET.tostring(e, 'ascii'),
1593 b"<?xml version='1.0' encoding='ascii'?>\n"
1594 b'<body>t&#227;g</body>')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001595
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001596 def test_issue3151(self):
1597 e = ET.XML('<prefix:localname xmlns:prefix="${stuff}"/>')
1598 self.assertEqual(e.tag, '{${stuff}}localname')
1599 t = ET.ElementTree(e)
1600 self.assertEqual(ET.tostring(e), b'<ns0:localname xmlns:ns0="${stuff}" />')
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001601
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001602 def test_issue6565(self):
1603 elem = ET.XML("<body><tag/></body>")
1604 self.assertEqual(summarize_list(elem), ['tag'])
1605 newelem = ET.XML(SAMPLE_XML)
1606 elem[:] = newelem[:]
1607 self.assertEqual(summarize_list(elem), ['tag', 'tag', 'section'])
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001608
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001609 def test_issue10777(self):
1610 # Registering a namespace twice caused a "dictionary changed size during
1611 # iteration" bug.
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001612
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001613 ET.register_namespace('test10777', 'http://myuri/')
1614 ET.register_namespace('test10777', 'http://myuri/')
Georg Brandl90b20672010-12-28 10:38:33 +00001615
Antoine Pitrou5b235d02013-04-18 19:37:06 +02001616
Florent Xiclunaf15351d2010-03-13 23:24:31 +00001617# --------------------------------------------------------------------
1618
1619
Eli Bendersky698bdb22013-01-10 06:01:06 -08001620class BasicElementTest(ElementTestCase, unittest.TestCase):
Eli Bendersky396e8fc2012-03-23 14:24:20 +02001621 def test_augmentation_type_errors(self):
1622 e = ET.Element('joe')
1623 self.assertRaises(TypeError, e.append, 'b')
1624 self.assertRaises(TypeError, e.extend, [ET.Element('bar'), 'foo'])
1625 self.assertRaises(TypeError, e.insert, 0, 'foo')
Florent Xicluna41fe6152010-04-02 18:52:12 +00001626
Eli Bendersky0192ba32012-03-30 16:38:33 +03001627 def test_cyclic_gc(self):
Eli Benderskya5e82202012-03-31 13:55:38 +03001628 class Dummy:
1629 pass
Eli Bendersky0192ba32012-03-30 16:38:33 +03001630
Eli Benderskya5e82202012-03-31 13:55:38 +03001631 # Test the shortest cycle: d->element->d
1632 d = Dummy()
1633 d.dummyref = ET.Element('joe', attr=d)
1634 wref = weakref.ref(d)
1635 del d
1636 gc_collect()
1637 self.assertIsNone(wref())
Eli Bendersky0192ba32012-03-30 16:38:33 +03001638
Eli Benderskyebf37a22012-04-03 22:02:37 +03001639 # A longer cycle: d->e->e2->d
1640 e = ET.Element('joe')
1641 d = Dummy()
1642 d.dummyref = e
1643 wref = weakref.ref(d)
1644 e2 = ET.SubElement(e, 'foo', attr=d)
1645 del d, e, e2
1646 gc_collect()
1647 self.assertIsNone(wref())
1648
1649 # A cycle between Element objects as children of one another
1650 # e1->e2->e3->e1
1651 e1 = ET.Element('e1')
1652 e2 = ET.Element('e2')
1653 e3 = ET.Element('e3')
1654 e1.append(e2)
1655 e2.append(e2)
1656 e3.append(e1)
1657 wref = weakref.ref(e1)
1658 del e1, e2, e3
1659 gc_collect()
1660 self.assertIsNone(wref())
1661
1662 def test_weakref(self):
1663 flag = False
1664 def wref_cb(w):
1665 nonlocal flag
1666 flag = True
1667 e = ET.Element('e')
1668 wref = weakref.ref(e, wref_cb)
1669 self.assertEqual(wref().tag, 'e')
1670 del e
1671 self.assertEqual(flag, True)
1672 self.assertEqual(wref(), None)
1673
Eli Benderskya8736902013-01-05 06:26:39 -08001674 def test_get_keyword_args(self):
1675 e1 = ET.Element('foo' , x=1, y=2, z=3)
1676 self.assertEqual(e1.get('x', default=7), 1)
1677 self.assertEqual(e1.get('w', default=7), 7)
1678
Eli Bendersky7ec45f72012-12-30 06:17:49 -08001679 def test_pickle(self):
Eli Bendersky698bdb22013-01-10 06:01:06 -08001680 # issue #16076: the C implementation wasn't pickleable.
Serhiy Storchakabad12572014-12-15 14:03:42 +02001681 for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
1682 for dumper, loader in product(self.modules, repeat=2):
1683 e = dumper.Element('foo', bar=42)
1684 e.text = "text goes here"
1685 e.tail = "opposite of head"
1686 dumper.SubElement(e, 'child').append(dumper.Element('grandchild'))
1687 e.append(dumper.Element('child'))
1688 e.findall('.//grandchild')[0].set('attr', 'other value')
Eli Bendersky7ec45f72012-12-30 06:17:49 -08001689
Serhiy Storchakabad12572014-12-15 14:03:42 +02001690 e2 = self.pickleRoundTrip(e, 'xml.etree.ElementTree',
1691 dumper, loader, proto)
Eli Bendersky698bdb22013-01-10 06:01:06 -08001692
Serhiy Storchakabad12572014-12-15 14:03:42 +02001693 self.assertEqual(e2.tag, 'foo')
1694 self.assertEqual(e2.attrib['bar'], 42)
1695 self.assertEqual(len(e2), 2)
1696 self.assertEqualElements(e, e2)
Eli Bendersky396e8fc2012-03-23 14:24:20 +02001697
Eli Benderskydd3661e2013-09-13 06:24:25 -07001698 def test_pickle_issue18997(self):
Serhiy Storchakabad12572014-12-15 14:03:42 +02001699 for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
1700 for dumper, loader in product(self.modules, repeat=2):
1701 XMLTEXT = """<?xml version="1.0"?>
1702 <group><dogs>4</dogs>
1703 </group>"""
1704 e1 = dumper.fromstring(XMLTEXT)
1705 if hasattr(e1, '__getstate__'):
1706 self.assertEqual(e1.__getstate__()['tag'], 'group')
1707 e2 = self.pickleRoundTrip(e1, 'xml.etree.ElementTree',
1708 dumper, loader, proto)
1709 self.assertEqual(e2.tag, 'group')
1710 self.assertEqual(e2[0].tag, 'dogs')
Eli Benderskydd3661e2013-09-13 06:24:25 -07001711
Eli Bendersky23687042013-02-26 05:53:23 -08001712
Serhiy Storchaka5bf31202015-05-18 18:29:33 +03001713class BadElementTest(ElementTestCase, unittest.TestCase):
1714 def test_extend_mutable_list(self):
1715 class X:
1716 @property
1717 def __class__(self):
1718 L[:] = [ET.Element('baz')]
1719 return ET.Element
1720 L = [X()]
1721 e = ET.Element('foo')
1722 try:
1723 e.extend(L)
1724 except TypeError:
1725 pass
1726
1727 class Y(X, ET.Element):
1728 pass
1729 L = [Y('x')]
1730 e = ET.Element('foo')
1731 e.extend(L)
1732
1733 def test_extend_mutable_list2(self):
1734 class X:
1735 @property
1736 def __class__(self):
1737 del L[:]
1738 return ET.Element
1739 L = [X(), ET.Element('baz')]
1740 e = ET.Element('foo')
1741 try:
1742 e.extend(L)
1743 except TypeError:
1744 pass
1745
1746 class Y(X, ET.Element):
1747 pass
1748 L = [Y('bar'), ET.Element('baz')]
1749 e = ET.Element('foo')
1750 e.extend(L)
1751
1752 def test_remove_with_mutating(self):
1753 class X(ET.Element):
1754 def __eq__(self, o):
1755 del e[:]
1756 return False
1757 e = ET.Element('foo')
1758 e.extend([X('bar')])
1759 self.assertRaises(ValueError, e.remove, ET.Element('baz'))
1760
1761 e = ET.Element('foo')
1762 e.extend([ET.Element('bar')])
1763 self.assertRaises(ValueError, e.remove, X('baz'))
1764
1765
1766class MutatingElementPath(str):
1767 def __new__(cls, elem, *args):
1768 self = str.__new__(cls, *args)
1769 self.elem = elem
1770 return self
1771 def __eq__(self, o):
1772 del self.elem[:]
1773 return True
1774MutatingElementPath.__hash__ = str.__hash__
1775
1776class BadElementPath(str):
1777 def __eq__(self, o):
1778 raise 1/0
1779BadElementPath.__hash__ = str.__hash__
1780
1781class BadElementPathTest(ElementTestCase, unittest.TestCase):
1782 def setUp(self):
1783 super().setUp()
1784 from xml.etree import ElementPath
1785 self.path_cache = ElementPath._cache
1786 ElementPath._cache = {}
1787
1788 def tearDown(self):
1789 from xml.etree import ElementPath
1790 ElementPath._cache = self.path_cache
1791 super().tearDown()
1792
1793 def test_find_with_mutating(self):
1794 e = ET.Element('foo')
1795 e.extend([ET.Element('bar')])
1796 e.find(MutatingElementPath(e, 'x'))
1797
1798 def test_find_with_error(self):
1799 e = ET.Element('foo')
1800 e.extend([ET.Element('bar')])
1801 try:
1802 e.find(BadElementPath('x'))
1803 except ZeroDivisionError:
1804 pass
1805
1806 def test_findtext_with_mutating(self):
1807 e = ET.Element('foo')
1808 e.extend([ET.Element('bar')])
1809 e.findtext(MutatingElementPath(e, 'x'))
1810
1811 def test_findtext_with_error(self):
1812 e = ET.Element('foo')
1813 e.extend([ET.Element('bar')])
1814 try:
1815 e.findtext(BadElementPath('x'))
1816 except ZeroDivisionError:
1817 pass
1818
1819 def test_findall_with_mutating(self):
1820 e = ET.Element('foo')
1821 e.extend([ET.Element('bar')])
1822 e.findall(MutatingElementPath(e, 'x'))
1823
1824 def test_findall_with_error(self):
1825 e = ET.Element('foo')
1826 e.extend([ET.Element('bar')])
1827 try:
1828 e.findall(BadElementPath('x'))
1829 except ZeroDivisionError:
1830 pass
1831
1832
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02001833class ElementTreeTypeTest(unittest.TestCase):
Florent Xicluna75b5e7e2012-03-05 10:42:19 +01001834 def test_istype(self):
1835 self.assertIsInstance(ET.ParseError, type)
1836 self.assertIsInstance(ET.QName, type)
1837 self.assertIsInstance(ET.ElementTree, type)
Eli Bendersky092af1f2012-03-04 07:14:03 +02001838 self.assertIsInstance(ET.Element, type)
Eli Bendersky64d11e62012-06-15 07:42:50 +03001839 self.assertIsInstance(ET.TreeBuilder, type)
1840 self.assertIsInstance(ET.XMLParser, type)
Eli Bendersky092af1f2012-03-04 07:14:03 +02001841
1842 def test_Element_subclass_trivial(self):
1843 class MyElement(ET.Element):
1844 pass
1845
1846 mye = MyElement('foo')
1847 self.assertIsInstance(mye, ET.Element)
1848 self.assertIsInstance(mye, MyElement)
1849 self.assertEqual(mye.tag, 'foo')
1850
Eli Benderskyb20df952012-05-20 06:33:29 +03001851 # test that attribute assignment works (issue 14849)
1852 mye.text = "joe"
1853 self.assertEqual(mye.text, "joe")
1854
Eli Bendersky092af1f2012-03-04 07:14:03 +02001855 def test_Element_subclass_constructor(self):
1856 class MyElement(ET.Element):
1857 def __init__(self, tag, attrib={}, **extra):
1858 super(MyElement, self).__init__(tag + '__', attrib, **extra)
1859
1860 mye = MyElement('foo', {'a': 1, 'b': 2}, c=3, d=4)
1861 self.assertEqual(mye.tag, 'foo__')
1862 self.assertEqual(sorted(mye.items()),
1863 [('a', 1), ('b', 2), ('c', 3), ('d', 4)])
1864
1865 def test_Element_subclass_new_method(self):
1866 class MyElement(ET.Element):
1867 def newmethod(self):
1868 return self.tag
1869
1870 mye = MyElement('joe')
1871 self.assertEqual(mye.newmethod(), 'joe')
Eli Benderskyda578192012-02-16 06:52:39 +02001872
Eli Benderskyceab1a92013-01-12 07:42:46 -08001873
1874class ElementFindTest(unittest.TestCase):
1875 def test_find_simple(self):
1876 e = ET.XML(SAMPLE_XML)
1877 self.assertEqual(e.find('tag').tag, 'tag')
1878 self.assertEqual(e.find('section/tag').tag, 'tag')
1879 self.assertEqual(e.find('./tag').tag, 'tag')
1880
1881 e[2] = ET.XML(SAMPLE_SECTION)
1882 self.assertEqual(e.find('section/nexttag').tag, 'nexttag')
1883
1884 self.assertEqual(e.findtext('./tag'), 'text')
1885 self.assertEqual(e.findtext('section/tag'), 'subtext')
1886
1887 # section/nexttag is found but has no text
1888 self.assertEqual(e.findtext('section/nexttag'), '')
1889 self.assertEqual(e.findtext('section/nexttag', 'default'), '')
1890
1891 # tog doesn't exist and 'default' kicks in
1892 self.assertIsNone(e.findtext('tog'))
1893 self.assertEqual(e.findtext('tog', 'default'), 'default')
1894
Eli Bendersky25771b32013-01-13 05:26:07 -08001895 # Issue #16922
1896 self.assertEqual(ET.XML('<tag><empty /></tag>').findtext('empty'), '')
1897
Eli Benderskya80f7612013-01-22 06:12:54 -08001898 def test_find_xpath(self):
1899 LINEAR_XML = '''
1900 <body>
1901 <tag class='a'/>
1902 <tag class='b'/>
1903 <tag class='c'/>
1904 <tag class='d'/>
1905 </body>'''
1906 e = ET.XML(LINEAR_XML)
1907
1908 # Test for numeric indexing and last()
1909 self.assertEqual(e.find('./tag[1]').attrib['class'], 'a')
1910 self.assertEqual(e.find('./tag[2]').attrib['class'], 'b')
1911 self.assertEqual(e.find('./tag[last()]').attrib['class'], 'd')
1912 self.assertEqual(e.find('./tag[last()-1]').attrib['class'], 'c')
1913 self.assertEqual(e.find('./tag[last()-2]').attrib['class'], 'b')
1914
Eli Bendersky5c6198b2013-01-24 06:29:26 -08001915 self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[0]')
1916 self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[-1]')
1917 self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[last()-0]')
1918 self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[last()+1]')
1919
Eli Benderskyceab1a92013-01-12 07:42:46 -08001920 def test_findall(self):
1921 e = ET.XML(SAMPLE_XML)
1922 e[2] = ET.XML(SAMPLE_SECTION)
1923 self.assertEqual(summarize_list(e.findall('.')), ['body'])
1924 self.assertEqual(summarize_list(e.findall('tag')), ['tag', 'tag'])
1925 self.assertEqual(summarize_list(e.findall('tog')), [])
1926 self.assertEqual(summarize_list(e.findall('tog/foo')), [])
1927 self.assertEqual(summarize_list(e.findall('*')),
1928 ['tag', 'tag', 'section'])
1929 self.assertEqual(summarize_list(e.findall('.//tag')),
1930 ['tag'] * 4)
1931 self.assertEqual(summarize_list(e.findall('section/tag')), ['tag'])
1932 self.assertEqual(summarize_list(e.findall('section//tag')), ['tag'] * 2)
1933 self.assertEqual(summarize_list(e.findall('section/*')),
1934 ['tag', 'nexttag', 'nextsection'])
1935 self.assertEqual(summarize_list(e.findall('section//*')),
1936 ['tag', 'nexttag', 'nextsection', 'tag'])
1937 self.assertEqual(summarize_list(e.findall('section/.//*')),
1938 ['tag', 'nexttag', 'nextsection', 'tag'])
1939 self.assertEqual(summarize_list(e.findall('*/*')),
1940 ['tag', 'nexttag', 'nextsection'])
1941 self.assertEqual(summarize_list(e.findall('*//*')),
1942 ['tag', 'nexttag', 'nextsection', 'tag'])
1943 self.assertEqual(summarize_list(e.findall('*/tag')), ['tag'])
1944 self.assertEqual(summarize_list(e.findall('*/./tag')), ['tag'])
1945 self.assertEqual(summarize_list(e.findall('./tag')), ['tag'] * 2)
1946 self.assertEqual(summarize_list(e.findall('././tag')), ['tag'] * 2)
1947
1948 self.assertEqual(summarize_list(e.findall('.//tag[@class]')),
1949 ['tag'] * 3)
1950 self.assertEqual(summarize_list(e.findall('.//tag[@class="a"]')),
1951 ['tag'])
1952 self.assertEqual(summarize_list(e.findall('.//tag[@class="b"]')),
1953 ['tag'] * 2)
1954 self.assertEqual(summarize_list(e.findall('.//tag[@id]')),
1955 ['tag'])
1956 self.assertEqual(summarize_list(e.findall('.//section[tag]')),
1957 ['section'])
1958 self.assertEqual(summarize_list(e.findall('.//section[element]')), [])
1959 self.assertEqual(summarize_list(e.findall('../tag')), [])
1960 self.assertEqual(summarize_list(e.findall('section/../tag')),
1961 ['tag'] * 2)
1962 self.assertEqual(e.findall('section//'), e.findall('section//*'))
1963
1964 def test_test_find_with_ns(self):
1965 e = ET.XML(SAMPLE_XML_NS)
1966 self.assertEqual(summarize_list(e.findall('tag')), [])
1967 self.assertEqual(
1968 summarize_list(e.findall("{http://effbot.org/ns}tag")),
1969 ['{http://effbot.org/ns}tag'] * 2)
1970 self.assertEqual(
1971 summarize_list(e.findall(".//{http://effbot.org/ns}tag")),
1972 ['{http://effbot.org/ns}tag'] * 3)
1973
Eli Bendersky2acc5252013-08-03 17:47:47 -07001974 def test_findall_different_nsmaps(self):
1975 root = ET.XML('''
1976 <a xmlns:x="X" xmlns:y="Y">
1977 <x:b><c/></x:b>
1978 <b/>
1979 <c><x:b/><b/></c><y:b/>
1980 </a>''')
1981 nsmap = {'xx': 'X'}
1982 self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 2)
1983 self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 2)
1984 nsmap = {'xx': 'Y'}
1985 self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 1)
1986 self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 2)
1987
Eli Benderskyceab1a92013-01-12 07:42:46 -08001988 def test_bad_find(self):
1989 e = ET.XML(SAMPLE_XML)
1990 with self.assertRaisesRegex(SyntaxError, 'cannot use absolute path'):
1991 e.findall('/tag')
Eli Benderskyc31f7732013-01-12 07:44:32 -08001992
Eli Benderskyceab1a92013-01-12 07:42:46 -08001993 def test_find_through_ElementTree(self):
1994 e = ET.XML(SAMPLE_XML)
1995 self.assertEqual(ET.ElementTree(e).find('tag').tag, 'tag')
1996 self.assertEqual(ET.ElementTree(e).findtext('tag'), 'text')
1997 self.assertEqual(summarize_list(ET.ElementTree(e).findall('tag')),
1998 ['tag'] * 2)
1999 # this produces a warning
2000 self.assertEqual(summarize_list(ET.ElementTree(e).findall('//tag')),
2001 ['tag'] * 3)
Eli Benderskyc31f7732013-01-12 07:44:32 -08002002
Eli Benderskyceab1a92013-01-12 07:42:46 -08002003
Eli Bendersky64d11e62012-06-15 07:42:50 +03002004class ElementIterTest(unittest.TestCase):
2005 def _ilist(self, elem, tag=None):
2006 return summarize_list(elem.iter(tag))
2007
2008 def test_basic(self):
2009 doc = ET.XML("<html><body>this is a <i>paragraph</i>.</body>..</html>")
2010 self.assertEqual(self._ilist(doc), ['html', 'body', 'i'])
2011 self.assertEqual(self._ilist(doc.find('body')), ['body', 'i'])
2012 self.assertEqual(next(doc.iter()).tag, 'html')
2013 self.assertEqual(''.join(doc.itertext()), 'this is a paragraph...')
2014 self.assertEqual(''.join(doc.find('body').itertext()),
2015 'this is a paragraph.')
2016 self.assertEqual(next(doc.itertext()), 'this is a ')
2017
2018 # iterparse should return an iterator
2019 sourcefile = serialize(doc, to_string=False)
2020 self.assertEqual(next(ET.iterparse(sourcefile))[0], 'end')
2021
Eli Benderskyaaa97802013-01-24 07:15:19 -08002022 # With an explitit parser too (issue #9708)
2023 sourcefile = serialize(doc, to_string=False)
2024 parser = ET.XMLParser(target=ET.TreeBuilder())
2025 self.assertEqual(next(ET.iterparse(sourcefile, parser=parser))[0],
2026 'end')
2027
Eli Bendersky64d11e62012-06-15 07:42:50 +03002028 tree = ET.ElementTree(None)
2029 self.assertRaises(AttributeError, tree.iter)
2030
Eli Benderskye6174ca2013-01-10 06:27:53 -08002031 # Issue #16913
2032 doc = ET.XML("<root>a&amp;<sub>b&amp;</sub>c&amp;</root>")
2033 self.assertEqual(''.join(doc.itertext()), 'a&b&c&')
2034
Eli Bendersky64d11e62012-06-15 07:42:50 +03002035 def test_corners(self):
2036 # single root, no subelements
2037 a = ET.Element('a')
2038 self.assertEqual(self._ilist(a), ['a'])
2039
2040 # one child
2041 b = ET.SubElement(a, 'b')
2042 self.assertEqual(self._ilist(a), ['a', 'b'])
2043
2044 # one child and one grandchild
2045 c = ET.SubElement(b, 'c')
2046 self.assertEqual(self._ilist(a), ['a', 'b', 'c'])
2047
2048 # two children, only first with grandchild
2049 d = ET.SubElement(a, 'd')
2050 self.assertEqual(self._ilist(a), ['a', 'b', 'c', 'd'])
2051
2052 # replace first child by second
2053 a[0] = a[1]
2054 del a[1]
2055 self.assertEqual(self._ilist(a), ['a', 'd'])
2056
2057 def test_iter_by_tag(self):
2058 doc = ET.XML('''
2059 <document>
2060 <house>
2061 <room>bedroom1</room>
2062 <room>bedroom2</room>
2063 </house>
2064 <shed>nothing here
2065 </shed>
2066 <house>
2067 <room>bedroom8</room>
2068 </house>
2069 </document>''')
2070
2071 self.assertEqual(self._ilist(doc, 'room'), ['room'] * 3)
2072 self.assertEqual(self._ilist(doc, 'house'), ['house'] * 2)
2073
Eli Benderskya8736902013-01-05 06:26:39 -08002074 # test that iter also accepts 'tag' as a keyword arg
2075 self.assertEqual(
2076 summarize_list(doc.iter(tag='room')),
2077 ['room'] * 3)
2078
Eli Bendersky64d11e62012-06-15 07:42:50 +03002079 # make sure both tag=None and tag='*' return all tags
2080 all_tags = ['document', 'house', 'room', 'room',
2081 'shed', 'house', 'room']
2082 self.assertEqual(self._ilist(doc), all_tags)
2083 self.assertEqual(self._ilist(doc, '*'), all_tags)
2084
2085
Florent Xicluna75b5e7e2012-03-05 10:42:19 +01002086class TreeBuilderTest(unittest.TestCase):
Florent Xicluna75b5e7e2012-03-05 10:42:19 +01002087 sample1 = ('<!DOCTYPE html PUBLIC'
2088 ' "-//W3C//DTD XHTML 1.0 Transitional//EN"'
2089 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
Antoine Pitrouee329312012-10-04 19:53:29 +02002090 '<html>text<div>subtext</div>tail</html>')
Florent Xicluna75b5e7e2012-03-05 10:42:19 +01002091
Eli Bendersky48d358b2012-05-30 17:57:50 +03002092 sample2 = '''<toplevel>sometext</toplevel>'''
2093
Antoine Pitrouee329312012-10-04 19:53:29 +02002094 def _check_sample1_element(self, e):
2095 self.assertEqual(e.tag, 'html')
2096 self.assertEqual(e.text, 'text')
2097 self.assertEqual(e.tail, None)
2098 self.assertEqual(e.attrib, {})
2099 children = list(e)
2100 self.assertEqual(len(children), 1)
2101 child = children[0]
2102 self.assertEqual(child.tag, 'div')
2103 self.assertEqual(child.text, 'subtext')
2104 self.assertEqual(child.tail, 'tail')
2105 self.assertEqual(child.attrib, {})
2106
Florent Xicluna75b5e7e2012-03-05 10:42:19 +01002107 def test_dummy_builder(self):
2108 class BaseDummyBuilder:
2109 def close(self):
2110 return 42
2111
2112 class DummyBuilder(BaseDummyBuilder):
2113 data = start = end = lambda *a: None
2114
2115 parser = ET.XMLParser(target=DummyBuilder())
2116 parser.feed(self.sample1)
2117 self.assertEqual(parser.close(), 42)
2118
2119 parser = ET.XMLParser(target=BaseDummyBuilder())
2120 parser.feed(self.sample1)
2121 self.assertEqual(parser.close(), 42)
2122
2123 parser = ET.XMLParser(target=object())
2124 parser.feed(self.sample1)
2125 self.assertIsNone(parser.close())
2126
Eli Bendersky08231a92013-05-18 15:47:16 -07002127 def test_treebuilder_elementfactory_none(self):
2128 parser = ET.XMLParser(target=ET.TreeBuilder(element_factory=None))
2129 parser.feed(self.sample1)
2130 e = parser.close()
2131 self._check_sample1_element(e)
2132
Eli Bendersky58d548d2012-05-29 15:45:16 +03002133 def test_subclass(self):
2134 class MyTreeBuilder(ET.TreeBuilder):
2135 def foobar(self, x):
2136 return x * 2
2137
2138 tb = MyTreeBuilder()
2139 self.assertEqual(tb.foobar(10), 20)
2140
2141 parser = ET.XMLParser(target=tb)
2142 parser.feed(self.sample1)
2143
2144 e = parser.close()
Antoine Pitrouee329312012-10-04 19:53:29 +02002145 self._check_sample1_element(e)
Eli Bendersky58d548d2012-05-29 15:45:16 +03002146
Eli Bendersky2b711402012-03-16 15:29:50 +02002147 def test_element_factory(self):
Eli Bendersky48d358b2012-05-30 17:57:50 +03002148 lst = []
2149 def myfactory(tag, attrib):
2150 nonlocal lst
2151 lst.append(tag)
2152 return ET.Element(tag, attrib)
2153
2154 tb = ET.TreeBuilder(element_factory=myfactory)
2155 parser = ET.XMLParser(target=tb)
2156 parser.feed(self.sample2)
2157 parser.close()
2158
2159 self.assertEqual(lst, ['toplevel'])
Florent Xicluna75b5e7e2012-03-05 10:42:19 +01002160
Antoine Pitrouee329312012-10-04 19:53:29 +02002161 def _check_element_factory_class(self, cls):
2162 tb = ET.TreeBuilder(element_factory=cls)
2163
2164 parser = ET.XMLParser(target=tb)
2165 parser.feed(self.sample1)
2166 e = parser.close()
2167 self.assertIsInstance(e, cls)
2168 self._check_sample1_element(e)
2169
2170 def test_element_factory_subclass(self):
2171 class MyElement(ET.Element):
2172 pass
2173 self._check_element_factory_class(MyElement)
2174
2175 def test_element_factory_pure_python_subclass(self):
2176 # Mimick SimpleTAL's behaviour (issue #16089): both versions of
2177 # TreeBuilder should be able to cope with a subclass of the
2178 # pure Python Element class.
Eli Bendersky46955b22013-05-19 09:20:50 -07002179 base = ET._Element_Py
Antoine Pitrouee329312012-10-04 19:53:29 +02002180 # Not from a C extension
2181 self.assertEqual(base.__module__, 'xml.etree.ElementTree')
2182 # Force some multiple inheritance with a C class to make things
2183 # more interesting.
2184 class MyElement(base, ValueError):
2185 pass
2186 self._check_element_factory_class(MyElement)
2187
Florent Xicluna75b5e7e2012-03-05 10:42:19 +01002188 def test_doctype(self):
2189 class DoctypeParser:
2190 _doctype = None
2191
2192 def doctype(self, name, pubid, system):
2193 self._doctype = (name, pubid, system)
2194
2195 def close(self):
2196 return self._doctype
2197
2198 parser = ET.XMLParser(target=DoctypeParser())
2199 parser.feed(self.sample1)
2200
2201 self.assertEqual(parser.close(),
2202 ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
2203 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'))
2204
Eli Bendersky175fada2012-06-15 08:37:08 +03002205
Eli Bendersky52467b12012-06-01 07:13:08 +03002206class XMLParserTest(unittest.TestCase):
Serhiy Storchaka66d53fa2013-05-22 17:07:51 +03002207 sample1 = b'<file><line>22</line></file>'
2208 sample2 = (b'<!DOCTYPE html PUBLIC'
2209 b' "-//W3C//DTD XHTML 1.0 Transitional//EN"'
2210 b' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
2211 b'<html>text</html>')
2212 sample3 = ('<?xml version="1.0" encoding="iso-8859-1"?>\n'
2213 '<money value="$\xa3\u20ac\U0001017b">$\xa3\u20ac\U0001017b</money>')
Eli Bendersky52467b12012-06-01 07:13:08 +03002214
2215 def _check_sample_element(self, e):
2216 self.assertEqual(e.tag, 'file')
2217 self.assertEqual(e[0].tag, 'line')
2218 self.assertEqual(e[0].text, '22')
2219
2220 def test_constructor_args(self):
2221 # Positional args. The first (html) is not supported, but should be
2222 # nevertheless correctly accepted.
2223 parser = ET.XMLParser(None, ET.TreeBuilder(), 'utf-8')
2224 parser.feed(self.sample1)
2225 self._check_sample_element(parser.close())
2226
2227 # Now as keyword args.
Eli Bendersky23687042013-02-26 05:53:23 -08002228 parser2 = ET.XMLParser(encoding='utf-8',
2229 html=[{}],
2230 target=ET.TreeBuilder())
Eli Bendersky52467b12012-06-01 07:13:08 +03002231 parser2.feed(self.sample1)
2232 self._check_sample_element(parser2.close())
2233
2234 def test_subclass(self):
2235 class MyParser(ET.XMLParser):
2236 pass
2237 parser = MyParser()
2238 parser.feed(self.sample1)
2239 self._check_sample_element(parser.close())
2240
Serhiy Storchaka05744ac2015-06-29 22:35:58 +03002241 def test_doctype_warning(self):
2242 parser = ET.XMLParser()
2243 with self.assertWarns(DeprecationWarning):
2244 parser.doctype('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
2245 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')
2246 parser.feed('<html/>')
2247 parser.close()
2248
2249 with warnings.catch_warnings():
2250 warnings.simplefilter('error', DeprecationWarning)
2251 parser = ET.XMLParser()
2252 parser.feed(self.sample2)
2253 parser.close()
2254
Eli Bendersky2b6b73e2012-06-01 11:32:34 +03002255 def test_subclass_doctype(self):
2256 _doctype = None
2257 class MyParserWithDoctype(ET.XMLParser):
2258 def doctype(self, name, pubid, system):
2259 nonlocal _doctype
2260 _doctype = (name, pubid, system)
2261
2262 parser = MyParserWithDoctype()
Serhiy Storchaka66d53fa2013-05-22 17:07:51 +03002263 with self.assertWarns(DeprecationWarning):
2264 parser.feed(self.sample2)
Eli Bendersky2b6b73e2012-06-01 11:32:34 +03002265 parser.close()
2266 self.assertEqual(_doctype,
2267 ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
2268 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'))
2269
Serhiy Storchaka05744ac2015-06-29 22:35:58 +03002270 _doctype = _doctype2 = None
2271 with warnings.catch_warnings():
2272 warnings.simplefilter('error', DeprecationWarning)
2273 class DoctypeParser:
2274 def doctype(self, name, pubid, system):
2275 nonlocal _doctype2
2276 _doctype2 = (name, pubid, system)
2277
2278 parser = MyParserWithDoctype(target=DoctypeParser())
2279 parser.feed(self.sample2)
2280 parser.close()
2281 self.assertIsNone(_doctype)
2282 self.assertEqual(_doctype2,
2283 ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
2284 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'))
2285
2286 def test_inherited_doctype(self):
2287 '''Ensure that ordinary usage is not deprecated (Issue 19176)'''
2288 with warnings.catch_warnings():
2289 warnings.simplefilter('error', DeprecationWarning)
2290 class MyParserWithoutDoctype(ET.XMLParser):
2291 pass
2292 parser = MyParserWithoutDoctype()
2293 parser.feed(self.sample2)
2294 parser.close()
2295
Serhiy Storchaka66d53fa2013-05-22 17:07:51 +03002296 def test_parse_string(self):
2297 parser = ET.XMLParser(target=ET.TreeBuilder())
2298 parser.feed(self.sample3)
2299 e = parser.close()
2300 self.assertEqual(e.tag, 'money')
2301 self.assertEqual(e.attrib['value'], '$\xa3\u20ac\U0001017b')
2302 self.assertEqual(e.text, '$\xa3\u20ac\U0001017b')
2303
Eli Bendersky52467b12012-06-01 07:13:08 +03002304
Eli Bendersky737b1732012-05-29 06:02:56 +03002305class NamespaceParseTest(unittest.TestCase):
2306 def test_find_with_namespace(self):
2307 nsmap = {'h': 'hello', 'f': 'foo'}
2308 doc = ET.fromstring(SAMPLE_XML_NS_ELEMS)
2309
2310 self.assertEqual(len(doc.findall('{hello}table', nsmap)), 1)
2311 self.assertEqual(len(doc.findall('.//{hello}td', nsmap)), 2)
2312 self.assertEqual(len(doc.findall('.//{foo}name', nsmap)), 1)
2313
2314
Eli Bendersky865756a2012-03-09 13:38:15 +02002315class ElementSlicingTest(unittest.TestCase):
2316 def _elem_tags(self, elemlist):
2317 return [e.tag for e in elemlist]
2318
2319 def _subelem_tags(self, elem):
2320 return self._elem_tags(list(elem))
2321
2322 def _make_elem_with_children(self, numchildren):
2323 """Create an Element with a tag 'a', with the given amount of children
2324 named 'a0', 'a1' ... and so on.
2325
2326 """
2327 e = ET.Element('a')
2328 for i in range(numchildren):
2329 ET.SubElement(e, 'a%s' % i)
2330 return e
2331
2332 def test_getslice_single_index(self):
2333 e = self._make_elem_with_children(10)
2334
2335 self.assertEqual(e[1].tag, 'a1')
2336 self.assertEqual(e[-2].tag, 'a8')
2337
2338 self.assertRaises(IndexError, lambda: e[12])
2339
2340 def test_getslice_range(self):
2341 e = self._make_elem_with_children(6)
2342
2343 self.assertEqual(self._elem_tags(e[3:]), ['a3', 'a4', 'a5'])
2344 self.assertEqual(self._elem_tags(e[3:6]), ['a3', 'a4', 'a5'])
2345 self.assertEqual(self._elem_tags(e[3:16]), ['a3', 'a4', 'a5'])
2346 self.assertEqual(self._elem_tags(e[3:5]), ['a3', 'a4'])
2347 self.assertEqual(self._elem_tags(e[3:-1]), ['a3', 'a4'])
2348 self.assertEqual(self._elem_tags(e[:2]), ['a0', 'a1'])
2349
2350 def test_getslice_steps(self):
2351 e = self._make_elem_with_children(10)
2352
2353 self.assertEqual(self._elem_tags(e[8:10:1]), ['a8', 'a9'])
2354 self.assertEqual(self._elem_tags(e[::3]), ['a0', 'a3', 'a6', 'a9'])
2355 self.assertEqual(self._elem_tags(e[::8]), ['a0', 'a8'])
2356 self.assertEqual(self._elem_tags(e[1::8]), ['a1', 'a9'])
2357
2358 def test_getslice_negative_steps(self):
2359 e = self._make_elem_with_children(4)
2360
2361 self.assertEqual(self._elem_tags(e[::-1]), ['a3', 'a2', 'a1', 'a0'])
2362 self.assertEqual(self._elem_tags(e[::-2]), ['a3', 'a1'])
2363
2364 def test_delslice(self):
2365 e = self._make_elem_with_children(4)
2366 del e[0:2]
2367 self.assertEqual(self._subelem_tags(e), ['a2', 'a3'])
2368
2369 e = self._make_elem_with_children(4)
2370 del e[0:]
2371 self.assertEqual(self._subelem_tags(e), [])
2372
2373 e = self._make_elem_with_children(4)
2374 del e[::-1]
2375 self.assertEqual(self._subelem_tags(e), [])
2376
2377 e = self._make_elem_with_children(4)
2378 del e[::-2]
2379 self.assertEqual(self._subelem_tags(e), ['a0', 'a2'])
2380
2381 e = self._make_elem_with_children(4)
2382 del e[1::2]
2383 self.assertEqual(self._subelem_tags(e), ['a0', 'a2'])
2384
2385 e = self._make_elem_with_children(2)
2386 del e[::2]
2387 self.assertEqual(self._subelem_tags(e), ['a1'])
2388
Eli Benderskyf996e772012-03-16 05:53:30 +02002389
Eli Bendersky00f402b2012-07-15 06:02:22 +03002390class IOTest(unittest.TestCase):
2391 def tearDown(self):
Eli Bendersky23687042013-02-26 05:53:23 -08002392 support.unlink(TESTFN)
Eli Bendersky00f402b2012-07-15 06:02:22 +03002393
2394 def test_encoding(self):
2395 # Test encoding issues.
2396 elem = ET.Element("tag")
2397 elem.text = "abc"
2398 self.assertEqual(serialize(elem), '<tag>abc</tag>')
2399 self.assertEqual(serialize(elem, encoding="utf-8"),
2400 b'<tag>abc</tag>')
2401 self.assertEqual(serialize(elem, encoding="us-ascii"),
2402 b'<tag>abc</tag>')
2403 for enc in ("iso-8859-1", "utf-16", "utf-32"):
2404 self.assertEqual(serialize(elem, encoding=enc),
2405 ("<?xml version='1.0' encoding='%s'?>\n"
2406 "<tag>abc</tag>" % enc).encode(enc))
2407
2408 elem = ET.Element("tag")
2409 elem.text = "<&\"\'>"
2410 self.assertEqual(serialize(elem), '<tag>&lt;&amp;"\'&gt;</tag>')
2411 self.assertEqual(serialize(elem, encoding="utf-8"),
2412 b'<tag>&lt;&amp;"\'&gt;</tag>')
2413 self.assertEqual(serialize(elem, encoding="us-ascii"),
2414 b'<tag>&lt;&amp;"\'&gt;</tag>')
2415 for enc in ("iso-8859-1", "utf-16", "utf-32"):
2416 self.assertEqual(serialize(elem, encoding=enc),
2417 ("<?xml version='1.0' encoding='%s'?>\n"
2418 "<tag>&lt;&amp;\"'&gt;</tag>" % enc).encode(enc))
2419
2420 elem = ET.Element("tag")
2421 elem.attrib["key"] = "<&\"\'>"
2422 self.assertEqual(serialize(elem), '<tag key="&lt;&amp;&quot;\'&gt;" />')
2423 self.assertEqual(serialize(elem, encoding="utf-8"),
2424 b'<tag key="&lt;&amp;&quot;\'&gt;" />')
2425 self.assertEqual(serialize(elem, encoding="us-ascii"),
2426 b'<tag key="&lt;&amp;&quot;\'&gt;" />')
2427 for enc in ("iso-8859-1", "utf-16", "utf-32"):
2428 self.assertEqual(serialize(elem, encoding=enc),
2429 ("<?xml version='1.0' encoding='%s'?>\n"
2430 "<tag key=\"&lt;&amp;&quot;'&gt;\" />" % enc).encode(enc))
2431
2432 elem = ET.Element("tag")
2433 elem.text = '\xe5\xf6\xf6<>'
2434 self.assertEqual(serialize(elem), '<tag>\xe5\xf6\xf6&lt;&gt;</tag>')
2435 self.assertEqual(serialize(elem, encoding="utf-8"),
2436 b'<tag>\xc3\xa5\xc3\xb6\xc3\xb6&lt;&gt;</tag>')
2437 self.assertEqual(serialize(elem, encoding="us-ascii"),
2438 b'<tag>&#229;&#246;&#246;&lt;&gt;</tag>')
2439 for enc in ("iso-8859-1", "utf-16", "utf-32"):
2440 self.assertEqual(serialize(elem, encoding=enc),
2441 ("<?xml version='1.0' encoding='%s'?>\n"
2442 "<tag>Ă¥Ă¶Ă¶&lt;&gt;</tag>" % enc).encode(enc))
2443
2444 elem = ET.Element("tag")
2445 elem.attrib["key"] = '\xe5\xf6\xf6<>'
2446 self.assertEqual(serialize(elem), '<tag key="\xe5\xf6\xf6&lt;&gt;" />')
2447 self.assertEqual(serialize(elem, encoding="utf-8"),
2448 b'<tag key="\xc3\xa5\xc3\xb6\xc3\xb6&lt;&gt;" />')
2449 self.assertEqual(serialize(elem, encoding="us-ascii"),
2450 b'<tag key="&#229;&#246;&#246;&lt;&gt;" />')
2451 for enc in ("iso-8859-1", "utf-16", "utf-16le", "utf-16be", "utf-32"):
2452 self.assertEqual(serialize(elem, encoding=enc),
2453 ("<?xml version='1.0' encoding='%s'?>\n"
2454 "<tag key=\"Ă¥Ă¶Ă¶&lt;&gt;\" />" % enc).encode(enc))
2455
2456 def test_write_to_filename(self):
2457 tree = ET.ElementTree(ET.XML('''<site />'''))
2458 tree.write(TESTFN)
2459 with open(TESTFN, 'rb') as f:
2460 self.assertEqual(f.read(), b'''<site />''')
2461
2462 def test_write_to_text_file(self):
2463 tree = ET.ElementTree(ET.XML('''<site />'''))
2464 with open(TESTFN, 'w', encoding='utf-8') as f:
2465 tree.write(f, encoding='unicode')
2466 self.assertFalse(f.closed)
2467 with open(TESTFN, 'rb') as f:
2468 self.assertEqual(f.read(), b'''<site />''')
2469
2470 def test_write_to_binary_file(self):
2471 tree = ET.ElementTree(ET.XML('''<site />'''))
2472 with open(TESTFN, 'wb') as f:
2473 tree.write(f)
2474 self.assertFalse(f.closed)
2475 with open(TESTFN, 'rb') as f:
2476 self.assertEqual(f.read(), b'''<site />''')
2477
2478 def test_write_to_binary_file_with_bom(self):
2479 tree = ET.ElementTree(ET.XML('''<site />'''))
2480 # test BOM writing to buffered file
2481 with open(TESTFN, 'wb') as f:
2482 tree.write(f, encoding='utf-16')
2483 self.assertFalse(f.closed)
2484 with open(TESTFN, 'rb') as f:
2485 self.assertEqual(f.read(),
2486 '''<?xml version='1.0' encoding='utf-16'?>\n'''
2487 '''<site />'''.encode("utf-16"))
2488 # test BOM writing to non-buffered file
2489 with open(TESTFN, 'wb', buffering=0) as f:
2490 tree.write(f, encoding='utf-16')
2491 self.assertFalse(f.closed)
2492 with open(TESTFN, 'rb') as f:
2493 self.assertEqual(f.read(),
2494 '''<?xml version='1.0' encoding='utf-16'?>\n'''
2495 '''<site />'''.encode("utf-16"))
2496
Eli Benderskyf996e772012-03-16 05:53:30 +02002497 def test_read_from_stringio(self):
2498 tree = ET.ElementTree()
Eli Bendersky00f402b2012-07-15 06:02:22 +03002499 stream = io.StringIO('''<?xml version="1.0"?><site></site>''')
Eli Benderskyf996e772012-03-16 05:53:30 +02002500 tree.parse(stream)
Eli Benderskyf996e772012-03-16 05:53:30 +02002501 self.assertEqual(tree.getroot().tag, 'site')
2502
Eli Bendersky00f402b2012-07-15 06:02:22 +03002503 def test_write_to_stringio(self):
2504 tree = ET.ElementTree(ET.XML('''<site />'''))
2505 stream = io.StringIO()
2506 tree.write(stream, encoding='unicode')
2507 self.assertEqual(stream.getvalue(), '''<site />''')
2508
2509 def test_read_from_bytesio(self):
2510 tree = ET.ElementTree()
2511 raw = io.BytesIO(b'''<?xml version="1.0"?><site></site>''')
2512 tree.parse(raw)
2513 self.assertEqual(tree.getroot().tag, 'site')
2514
2515 def test_write_to_bytesio(self):
2516 tree = ET.ElementTree(ET.XML('''<site />'''))
2517 raw = io.BytesIO()
2518 tree.write(raw)
2519 self.assertEqual(raw.getvalue(), b'''<site />''')
2520
2521 class dummy:
2522 pass
2523
2524 def test_read_from_user_text_reader(self):
2525 stream = io.StringIO('''<?xml version="1.0"?><site></site>''')
2526 reader = self.dummy()
2527 reader.read = stream.read
2528 tree = ET.ElementTree()
2529 tree.parse(reader)
2530 self.assertEqual(tree.getroot().tag, 'site')
2531
2532 def test_write_to_user_text_writer(self):
2533 tree = ET.ElementTree(ET.XML('''<site />'''))
2534 stream = io.StringIO()
2535 writer = self.dummy()
2536 writer.write = stream.write
2537 tree.write(writer, encoding='unicode')
2538 self.assertEqual(stream.getvalue(), '''<site />''')
2539
2540 def test_read_from_user_binary_reader(self):
2541 raw = io.BytesIO(b'''<?xml version="1.0"?><site></site>''')
2542 reader = self.dummy()
2543 reader.read = raw.read
2544 tree = ET.ElementTree()
2545 tree.parse(reader)
2546 self.assertEqual(tree.getroot().tag, 'site')
2547 tree = ET.ElementTree()
2548
2549 def test_write_to_user_binary_writer(self):
2550 tree = ET.ElementTree(ET.XML('''<site />'''))
2551 raw = io.BytesIO()
2552 writer = self.dummy()
2553 writer.write = raw.write
2554 tree.write(writer)
2555 self.assertEqual(raw.getvalue(), b'''<site />''')
2556
2557 def test_write_to_user_binary_writer_with_bom(self):
2558 tree = ET.ElementTree(ET.XML('''<site />'''))
2559 raw = io.BytesIO()
2560 writer = self.dummy()
2561 writer.write = raw.write
2562 writer.seekable = lambda: True
2563 writer.tell = raw.tell
2564 tree.write(writer, encoding="utf-16")
2565 self.assertEqual(raw.getvalue(),
2566 '''<?xml version='1.0' encoding='utf-16'?>\n'''
2567 '''<site />'''.encode("utf-16"))
2568
Eli Bendersky426e2482012-07-17 05:45:11 +03002569 def test_tostringlist_invariant(self):
2570 root = ET.fromstring('<tag>foo</tag>')
2571 self.assertEqual(
2572 ET.tostring(root, 'unicode'),
2573 ''.join(ET.tostringlist(root, 'unicode')))
2574 self.assertEqual(
2575 ET.tostring(root, 'utf-16'),
2576 b''.join(ET.tostringlist(root, 'utf-16')))
2577
Eli Benderskya9a2ef52013-01-13 06:04:43 -08002578 def test_short_empty_elements(self):
2579 root = ET.fromstring('<tag>a<x />b<y></y>c</tag>')
2580 self.assertEqual(
2581 ET.tostring(root, 'unicode'),
2582 '<tag>a<x />b<y />c</tag>')
2583 self.assertEqual(
2584 ET.tostring(root, 'unicode', short_empty_elements=True),
2585 '<tag>a<x />b<y />c</tag>')
2586 self.assertEqual(
2587 ET.tostring(root, 'unicode', short_empty_elements=False),
2588 '<tag>a<x></x>b<y></y>c</tag>')
2589
Eli Benderskyf996e772012-03-16 05:53:30 +02002590
Eli Bendersky5b77d812012-03-16 08:20:05 +02002591class ParseErrorTest(unittest.TestCase):
2592 def test_subclass(self):
2593 self.assertIsInstance(ET.ParseError(), SyntaxError)
2594
2595 def _get_error(self, s):
2596 try:
2597 ET.fromstring(s)
2598 except ET.ParseError as e:
2599 return e
2600
2601 def test_error_position(self):
2602 self.assertEqual(self._get_error('foo').position, (1, 0))
2603 self.assertEqual(self._get_error('<tag>&foo;</tag>').position, (1, 5))
2604 self.assertEqual(self._get_error('foobar<').position, (1, 6))
2605
2606 def test_error_code(self):
2607 import xml.parsers.expat.errors as ERRORS
2608 self.assertEqual(self._get_error('foo').code,
2609 ERRORS.codes[ERRORS.XML_ERROR_SYNTAX])
2610
2611
Eli Bendersky737b1732012-05-29 06:02:56 +03002612class KeywordArgsTest(unittest.TestCase):
2613 # Test various issues with keyword arguments passed to ET.Element
2614 # constructor and methods
2615 def test_issue14818(self):
2616 x = ET.XML("<a>foo</a>")
2617 self.assertEqual(x.find('a', None),
2618 x.find(path='a', namespaces=None))
2619 self.assertEqual(x.findtext('a', None, None),
2620 x.findtext(path='a', default=None, namespaces=None))
2621 self.assertEqual(x.findall('a', None),
2622 x.findall(path='a', namespaces=None))
2623 self.assertEqual(list(x.iterfind('a', None)),
2624 list(x.iterfind(path='a', namespaces=None)))
2625
2626 self.assertEqual(ET.Element('a').attrib, {})
2627 elements = [
2628 ET.Element('a', dict(href="#", id="foo")),
2629 ET.Element('a', attrib=dict(href="#", id="foo")),
2630 ET.Element('a', dict(href="#"), id="foo"),
2631 ET.Element('a', href="#", id="foo"),
2632 ET.Element('a', dict(href="#", id="foo"), href="#", id="foo"),
2633 ]
2634 for e in elements:
2635 self.assertEqual(e.tag, 'a')
2636 self.assertEqual(e.attrib, dict(href="#", id="foo"))
2637
2638 e2 = ET.SubElement(elements[0], 'foobar', attrib={'key1': 'value1'})
2639 self.assertEqual(e2.attrib['key1'], 'value1')
2640
2641 with self.assertRaisesRegex(TypeError, 'must be dict, not str'):
2642 ET.Element('a', "I'm not a dict")
2643 with self.assertRaisesRegex(TypeError, 'must be dict, not str'):
2644 ET.Element('a', attrib="I'm not a dict")
2645
Eli Bendersky64d11e62012-06-15 07:42:50 +03002646# --------------------------------------------------------------------
2647
Eli Bendersky64d11e62012-06-15 07:42:50 +03002648class NoAcceleratorTest(unittest.TestCase):
Eli Bendersky52280c42012-12-30 06:27:56 -08002649 def setUp(self):
2650 if not pyET:
Eli Bendersky698bdb22013-01-10 06:01:06 -08002651 raise unittest.SkipTest('only for the Python version')
Eli Bendersky52280c42012-12-30 06:27:56 -08002652
Eli Bendersky64d11e62012-06-15 07:42:50 +03002653 # Test that the C accelerator was not imported for pyET
2654 def test_correct_import_pyET(self):
Eli Benderskye26fa1b2013-05-19 17:49:54 -07002655 # The type of methods defined in Python code is types.FunctionType,
2656 # while the type of methods defined inside _elementtree is
2657 # <class 'wrapper_descriptor'>
2658 self.assertIsInstance(pyET.Element.__init__, types.FunctionType)
2659 self.assertIsInstance(pyET.XMLParser.__init__, types.FunctionType)
Eli Bendersky64d11e62012-06-15 07:42:50 +03002660
Florent Xicluna75b5e7e2012-03-05 10:42:19 +01002661# --------------------------------------------------------------------
2662
2663
2664class CleanContext(object):
2665 """Provide default namespace mapping and path cache."""
2666 checkwarnings = None
2667
2668 def __init__(self, quiet=False):
2669 if sys.flags.optimize >= 2:
2670 # under -OO, doctests cannot be run and therefore not all warnings
2671 # will be emitted
2672 quiet = True
2673 deprecations = (
2674 # Search behaviour is broken if search path starts with "/".
2675 ("This search is broken in 1.3 and earlier, and will be fixed "
2676 "in a future version. If you rely on the current behaviour, "
2677 "change it to '.+'", FutureWarning),
2678 # Element.getchildren() and Element.getiterator() are deprecated.
2679 ("This method will be removed in future versions. "
2680 "Use .+ instead.", DeprecationWarning),
2681 ("This method will be removed in future versions. "
2682 "Use .+ instead.", PendingDeprecationWarning))
2683 self.checkwarnings = support.check_warnings(*deprecations, quiet=quiet)
2684
2685 def __enter__(self):
2686 from xml.etree import ElementPath
2687 self._nsmap = ET.register_namespace._namespace_map
2688 # Copy the default namespace mapping
2689 self._nsmap_copy = self._nsmap.copy()
2690 # Copy the path cache (should be empty)
2691 self._path_cache = ElementPath._cache
2692 ElementPath._cache = self._path_cache.copy()
2693 self.checkwarnings.__enter__()
2694
2695 def __exit__(self, *args):
2696 from xml.etree import ElementPath
2697 # Restore mapping and path cache
2698 self._nsmap.clear()
2699 self._nsmap.update(self._nsmap_copy)
2700 ElementPath._cache = self._path_cache
2701 self.checkwarnings.__exit__(*args)
2702
2703
Eli Bendersky64d11e62012-06-15 07:42:50 +03002704def test_main(module=None):
2705 # When invoked without a module, runs the Python ET tests by loading pyET.
2706 # Otherwise, uses the given module as the ET.
Eli Bendersky698bdb22013-01-10 06:01:06 -08002707 global pyET
2708 pyET = import_fresh_module('xml.etree.ElementTree',
2709 blocked=['_elementtree'])
Eli Bendersky64d11e62012-06-15 07:42:50 +03002710 if module is None:
Eli Bendersky64d11e62012-06-15 07:42:50 +03002711 module = pyET
Florent Xicluna41fe6152010-04-02 18:52:12 +00002712
Eli Bendersky64d11e62012-06-15 07:42:50 +03002713 global ET
2714 ET = module
Florent Xiclunaf15351d2010-03-13 23:24:31 +00002715
Eli Bendersky865756a2012-03-09 13:38:15 +02002716 test_classes = [
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02002717 ModuleTest,
Eli Bendersky865756a2012-03-09 13:38:15 +02002718 ElementSlicingTest,
Eli Bendersky396e8fc2012-03-23 14:24:20 +02002719 BasicElementTest,
Serhiy Storchaka5bf31202015-05-18 18:29:33 +03002720 BadElementTest,
2721 BadElementPathTest,
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02002722 ElementTreeTest,
Eli Bendersky00f402b2012-07-15 06:02:22 +03002723 IOTest,
Eli Bendersky5b77d812012-03-16 08:20:05 +02002724 ParseErrorTest,
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02002725 XIncludeTest,
2726 ElementTreeTypeTest,
Eli Benderskyceab1a92013-01-12 07:42:46 -08002727 ElementFindTest,
Eli Bendersky64d11e62012-06-15 07:42:50 +03002728 ElementIterTest,
Eli Bendersky737b1732012-05-29 06:02:56 +03002729 TreeBuilderTest,
Serhiy Storchaka66d53fa2013-05-22 17:07:51 +03002730 XMLParserTest,
Eli Benderskyb5869342013-08-30 05:51:20 -07002731 XMLPullParserTest,
Serhiy Storchakaf8cf59e2013-02-25 17:20:59 +02002732 BugsTest,
Eli Bendersky64d11e62012-06-15 07:42:50 +03002733 ]
2734
2735 # These tests will only run for the pure-Python version that doesn't import
2736 # _elementtree. We can't use skipUnless here, because pyET is filled in only
2737 # after the module is loaded.
Eli Bendersky698bdb22013-01-10 06:01:06 -08002738 if pyET is not ET:
Eli Bendersky64d11e62012-06-15 07:42:50 +03002739 test_classes.extend([
2740 NoAcceleratorTest,
Eli Bendersky64d11e62012-06-15 07:42:50 +03002741 ])
Florent Xicluna75b5e7e2012-03-05 10:42:19 +01002742
Eli Bendersky6319e0f2012-06-16 06:47:44 +03002743 try:
Eli Bendersky6319e0f2012-06-16 06:47:44 +03002744 # XXX the C module should give the same warnings as the Python module
Eli Bendersky698bdb22013-01-10 06:01:06 -08002745 with CleanContext(quiet=(pyET is not ET)):
Eli Benderskyceab1a92013-01-12 07:42:46 -08002746 support.run_unittest(*test_classes)
Eli Bendersky6319e0f2012-06-16 06:47:44 +03002747 finally:
2748 # don't interfere with subsequent tests
2749 ET = pyET = None
2750
Florent Xiclunaf15351d2010-03-13 23:24:31 +00002751
Armin Rigo9ed73062005-12-14 18:10:45 +00002752if __name__ == '__main__':
2753 test_main()