»Core Development>Code coverage>Lib/test/test_xml_etree.py

Python code coverage for Lib/test/test_xml_etree.py

#countcontent
1n/a# IMPORTANT: the same tests are run from "test_xml_etree_c" in order
2n/a# to ensure consistency between the C implementation and the Python
3n/a# implementation.
4n/a#
5n/a# For this purpose, the module-level "ET" symbol is temporarily
6n/a# monkey-patched when running the "test_xml_etree_c" test suite.
7n/a
8n/aimport copy
9n/aimport html
10n/aimport io
11n/aimport operator
12n/aimport pickle
13n/aimport sys
14n/aimport types
15n/aimport unittest
16n/aimport warnings
17n/aimport weakref
18n/a
19n/afrom itertools import product
20n/afrom test import support
21n/afrom test.support import TESTFN, findfile, import_fresh_module, gc_collect, swap_attr
22n/a
23n/a# pyET is the pure-Python implementation.
24n/a#
25n/a# ET is pyET in test_xml_etree and is the C accelerated version in
26n/a# test_xml_etree_c.
27n/apyET = None
28n/aET = None
29n/a
30n/aSIMPLE_XMLFILE = findfile("simple.xml", subdir="xmltestdata")
31n/atry:
32n/a SIMPLE_XMLFILE.encode("utf-8")
33n/aexcept UnicodeEncodeError:
34n/a raise unittest.SkipTest("filename is not encodable to utf8")
35n/aSIMPLE_NS_XMLFILE = findfile("simple-ns.xml", subdir="xmltestdata")
36n/a
37n/aSAMPLE_XML = """\
38n/a<body>
39n/a <tag class='a'>text</tag>
40n/a <tag class='b' />
41n/a <section>
42n/a <tag class='b' id='inner'>subtext</tag>
43n/a </section>
44n/a</body>
45n/a"""
46n/a
47n/aSAMPLE_SECTION = """\
48n/a<section>
49n/a <tag class='b' id='inner'>subtext</tag>
50n/a <nexttag />
51n/a <nextsection>
52n/a <tag />
53n/a </nextsection>
54n/a</section>
55n/a"""
56n/a
57n/aSAMPLE_XML_NS = """
58n/a<body xmlns="http://effbot.org/ns">
59n/a <tag>text</tag>
60n/a <tag />
61n/a <section>
62n/a <tag>subtext</tag>
63n/a </section>
64n/a</body>
65n/a"""
66n/a
67n/aSAMPLE_XML_NS_ELEMS = """
68n/a<root>
69n/a<h:table xmlns:h="hello">
70n/a <h:tr>
71n/a <h:td>Apples</h:td>
72n/a <h:td>Bananas</h:td>
73n/a </h:tr>
74n/a</h:table>
75n/a
76n/a<f:table xmlns:f="foo">
77n/a <f:name>African Coffee Table</f:name>
78n/a <f:width>80</f:width>
79n/a <f:length>120</f:length>
80n/a</f:table>
81n/a</root>
82n/a"""
83n/a
84n/aENTITY_XML = """\
85n/a<!DOCTYPE points [
86n/a<!ENTITY % user-entities SYSTEM 'user-entities.xml'>
87n/a%user-entities;
88n/a]>
89n/a<document>&entity;</document>
90n/a"""
91n/a
92n/a
93n/aclass ModuleTest(unittest.TestCase):
94n/a def test_sanity(self):
95n/a # Import sanity.
96n/a
97n/a from xml.etree import ElementTree
98n/a from xml.etree import ElementInclude
99n/a from xml.etree import ElementPath
100n/a
101n/a def test_all(self):
102n/a names = ("xml.etree.ElementTree", "_elementtree")
103n/a support.check__all__(self, ET, names, blacklist=("HTML_EMPTY",))
104n/a
105n/a
106n/adef serialize(elem, to_string=True, encoding='unicode', **options):
107n/a if encoding != 'unicode':
108n/a file = io.BytesIO()
109n/a else:
110n/a file = io.StringIO()
111n/a tree = ET.ElementTree(elem)
112n/a tree.write(file, encoding=encoding, **options)
113n/a if to_string:
114n/a return file.getvalue()
115n/a else:
116n/a file.seek(0)
117n/a return file
118n/a
119n/adef summarize_list(seq):
120n/a return [elem.tag for elem in seq]
121n/a
122n/a
123n/aclass ElementTestCase:
124n/a @classmethod
125n/a def setUpClass(cls):
126n/a cls.modules = {pyET, ET}
127n/a
128n/a def pickleRoundTrip(self, obj, name, dumper, loader, proto):
129n/a save_m = sys.modules[name]
130n/a try:
131n/a sys.modules[name] = dumper
132n/a temp = pickle.dumps(obj, proto)
133n/a sys.modules[name] = loader
134n/a result = pickle.loads(temp)
135n/a except pickle.PicklingError as pe:
136n/a # pyET must be second, because pyET may be (equal to) ET.
137n/a human = dict([(ET, "cET"), (pyET, "pyET")])
138n/a raise support.TestFailed("Failed to round-trip %r from %r to %r"
139n/a % (obj,
140n/a human.get(dumper, dumper),
141n/a human.get(loader, loader))) from pe
142n/a finally:
143n/a sys.modules[name] = save_m
144n/a return result
145n/a
146n/a def assertEqualElements(self, alice, bob):
147n/a self.assertIsInstance(alice, (ET.Element, pyET.Element))
148n/a self.assertIsInstance(bob, (ET.Element, pyET.Element))
149n/a self.assertEqual(len(list(alice)), len(list(bob)))
150n/a for x, y in zip(alice, bob):
151n/a self.assertEqualElements(x, y)
152n/a properties = operator.attrgetter('tag', 'tail', 'text', 'attrib')
153n/a self.assertEqual(properties(alice), properties(bob))
154n/a
155n/a# --------------------------------------------------------------------
156n/a# element tree tests
157n/a
158n/aclass ElementTreeTest(unittest.TestCase):
159n/a
160n/a def serialize_check(self, elem, expected):
161n/a self.assertEqual(serialize(elem), expected)
162n/a
163n/a def test_interface(self):
164n/a # Test element tree interface.
165n/a
166n/a def check_string(string):
167n/a len(string)
168n/a for char in string:
169n/a self.assertEqual(len(char), 1,
170n/a msg="expected one-character string, got %r" % char)
171n/a new_string = string + ""
172n/a new_string = string + " "
173n/a string[:0]
174n/a
175n/a def check_mapping(mapping):
176n/a len(mapping)
177n/a keys = mapping.keys()
178n/a items = mapping.items()
179n/a for key in keys:
180n/a item = mapping[key]
181n/a mapping["key"] = "value"
182n/a self.assertEqual(mapping["key"], "value",
183n/a msg="expected value string, got %r" % mapping["key"])
184n/a
185n/a def check_element(element):
186n/a self.assertTrue(ET.iselement(element), msg="not an element")
187n/a direlem = dir(element)
188n/a for attr in 'tag', 'attrib', 'text', 'tail':
189n/a self.assertTrue(hasattr(element, attr),
190n/a msg='no %s member' % attr)
191n/a self.assertIn(attr, direlem,
192n/a msg='no %s visible by dir' % attr)
193n/a
194n/a check_string(element.tag)
195n/a check_mapping(element.attrib)
196n/a if element.text is not None:
197n/a check_string(element.text)
198n/a if element.tail is not None:
199n/a check_string(element.tail)
200n/a for elem in element:
201n/a check_element(elem)
202n/a
203n/a element = ET.Element("tag")
204n/a check_element(element)
205n/a tree = ET.ElementTree(element)
206n/a check_element(tree.getroot())
207n/a element = ET.Element("t\xe4g", key="value")
208n/a tree = ET.ElementTree(element)
209n/a self.assertRegex(repr(element), r"^<Element 't\xe4g' at 0x.*>$")
210n/a element = ET.Element("tag", key="value")
211n/a
212n/a # Make sure all standard element methods exist.
213n/a
214n/a def check_method(method):
215n/a self.assertTrue(hasattr(method, '__call__'),
216n/a msg="%s not callable" % method)
217n/a
218n/a check_method(element.append)
219n/a check_method(element.extend)
220n/a check_method(element.insert)
221n/a check_method(element.remove)
222n/a check_method(element.getchildren)
223n/a check_method(element.find)
224n/a check_method(element.iterfind)
225n/a check_method(element.findall)
226n/a check_method(element.findtext)
227n/a check_method(element.clear)
228n/a check_method(element.get)
229n/a check_method(element.set)
230n/a check_method(element.keys)
231n/a check_method(element.items)
232n/a check_method(element.iter)
233n/a check_method(element.itertext)
234n/a check_method(element.getiterator)
235n/a
236n/a # These methods return an iterable. See bug 6472.
237n/a
238n/a def check_iter(it):
239n/a check_method(it.__next__)
240n/a
241n/a check_iter(element.iterfind("tag"))
242n/a check_iter(element.iterfind("*"))
243n/a check_iter(tree.iterfind("tag"))
244n/a check_iter(tree.iterfind("*"))
245n/a
246n/a # These aliases are provided:
247n/a
248n/a self.assertEqual(ET.XML, ET.fromstring)
249n/a self.assertEqual(ET.PI, ET.ProcessingInstruction)
250n/a
251n/a def test_set_attribute(self):
252n/a element = ET.Element('tag')
253n/a
254n/a self.assertEqual(element.tag, 'tag')
255n/a element.tag = 'Tag'
256n/a self.assertEqual(element.tag, 'Tag')
257n/a element.tag = 'TAG'
258n/a self.assertEqual(element.tag, 'TAG')
259n/a
260n/a self.assertIsNone(element.text)
261n/a element.text = 'Text'
262n/a self.assertEqual(element.text, 'Text')
263n/a element.text = 'TEXT'
264n/a self.assertEqual(element.text, 'TEXT')
265n/a
266n/a self.assertIsNone(element.tail)
267n/a element.tail = 'Tail'
268n/a self.assertEqual(element.tail, 'Tail')
269n/a element.tail = 'TAIL'
270n/a self.assertEqual(element.tail, 'TAIL')
271n/a
272n/a self.assertEqual(element.attrib, {})
273n/a element.attrib = {'a': 'b', 'c': 'd'}
274n/a self.assertEqual(element.attrib, {'a': 'b', 'c': 'd'})
275n/a element.attrib = {'A': 'B', 'C': 'D'}
276n/a self.assertEqual(element.attrib, {'A': 'B', 'C': 'D'})
277n/a
278n/a def test_simpleops(self):
279n/a # Basic method sanity checks.
280n/a
281n/a elem = ET.XML("<body><tag/></body>")
282n/a self.serialize_check(elem, '<body><tag /></body>')
283n/a e = ET.Element("tag2")
284n/a elem.append(e)
285n/a self.serialize_check(elem, '<body><tag /><tag2 /></body>')
286n/a elem.remove(e)
287n/a self.serialize_check(elem, '<body><tag /></body>')
288n/a elem.insert(0, e)
289n/a self.serialize_check(elem, '<body><tag2 /><tag /></body>')
290n/a elem.remove(e)
291n/a elem.extend([e])
292n/a self.serialize_check(elem, '<body><tag /><tag2 /></body>')
293n/a elem.remove(e)
294n/a
295n/a element = ET.Element("tag", key="value")
296n/a self.serialize_check(element, '<tag key="value" />') # 1
297n/a subelement = ET.Element("subtag")
298n/a element.append(subelement)
299n/a self.serialize_check(element, '<tag key="value"><subtag /></tag>') # 2
300n/a element.insert(0, subelement)
301n/a self.serialize_check(element,
302n/a '<tag key="value"><subtag /><subtag /></tag>') # 3
303n/a element.remove(subelement)
304n/a self.serialize_check(element, '<tag key="value"><subtag /></tag>') # 4
305n/a element.remove(subelement)
306n/a self.serialize_check(element, '<tag key="value" />') # 5
307n/a with self.assertRaises(ValueError) as cm:
308n/a element.remove(subelement)
309n/a self.assertEqual(str(cm.exception), 'list.remove(x): x not in list')
310n/a self.serialize_check(element, '<tag key="value" />') # 6
311n/a element[0:0] = [subelement, subelement, subelement]
312n/a self.serialize_check(element[1], '<subtag />')
313n/a self.assertEqual(element[1:9], [element[1], element[2]])
314n/a self.assertEqual(element[:9:2], [element[0], element[2]])
315n/a del element[1:2]
316n/a self.serialize_check(element,
317n/a '<tag key="value"><subtag /><subtag /></tag>')
318n/a
319n/a def test_cdata(self):
320n/a # Test CDATA handling (etc).
321n/a
322n/a self.serialize_check(ET.XML("<tag>hello</tag>"),
323n/a '<tag>hello</tag>')
324n/a self.serialize_check(ET.XML("<tag>&#104;&#101;&#108;&#108;&#111;</tag>"),
325n/a '<tag>hello</tag>')
326n/a self.serialize_check(ET.XML("<tag><![CDATA[hello]]></tag>"),
327n/a '<tag>hello</tag>')
328n/a
329n/a def test_file_init(self):
330n/a stringfile = io.BytesIO(SAMPLE_XML.encode("utf-8"))
331n/a tree = ET.ElementTree(file=stringfile)
332n/a self.assertEqual(tree.find("tag").tag, 'tag')
333n/a self.assertEqual(tree.find("section/tag").tag, 'tag')
334n/a
335n/a tree = ET.ElementTree(file=SIMPLE_XMLFILE)
336n/a self.assertEqual(tree.find("element").tag, 'element')
337n/a self.assertEqual(tree.find("element/../empty-element").tag,
338n/a 'empty-element')
339n/a
340n/a def test_path_cache(self):
341n/a # Check that the path cache behaves sanely.
342n/a
343n/a from xml.etree import ElementPath
344n/a
345n/a elem = ET.XML(SAMPLE_XML)
346n/a for i in range(10): ET.ElementTree(elem).find('./'+str(i))
347n/a cache_len_10 = len(ElementPath._cache)
348n/a for i in range(10): ET.ElementTree(elem).find('./'+str(i))
349n/a self.assertEqual(len(ElementPath._cache), cache_len_10)
350n/a for i in range(20): ET.ElementTree(elem).find('./'+str(i))
351n/a self.assertGreater(len(ElementPath._cache), cache_len_10)
352n/a for i in range(600): ET.ElementTree(elem).find('./'+str(i))
353n/a self.assertLess(len(ElementPath._cache), 500)
354n/a
355n/a def test_copy(self):
356n/a # Test copy handling (etc).
357n/a
358n/a import copy
359n/a e1 = ET.XML("<tag>hello<foo/></tag>")
360n/a e2 = copy.copy(e1)
361n/a e3 = copy.deepcopy(e1)
362n/a e1.find("foo").tag = "bar"
363n/a self.serialize_check(e1, '<tag>hello<bar /></tag>')
364n/a self.serialize_check(e2, '<tag>hello<bar /></tag>')
365n/a self.serialize_check(e3, '<tag>hello<foo /></tag>')
366n/a
367n/a def test_attrib(self):
368n/a # Test attribute handling.
369n/a
370n/a elem = ET.Element("tag")
371n/a elem.get("key") # 1.1
372n/a self.assertEqual(elem.get("key", "default"), 'default') # 1.2
373n/a
374n/a elem.set("key", "value")
375n/a self.assertEqual(elem.get("key"), 'value') # 1.3
376n/a
377n/a elem = ET.Element("tag", key="value")
378n/a self.assertEqual(elem.get("key"), 'value') # 2.1
379n/a self.assertEqual(elem.attrib, {'key': 'value'}) # 2.2
380n/a
381n/a attrib = {"key": "value"}
382n/a elem = ET.Element("tag", attrib)
383n/a attrib.clear() # check for aliasing issues
384n/a self.assertEqual(elem.get("key"), 'value') # 3.1
385n/a self.assertEqual(elem.attrib, {'key': 'value'}) # 3.2
386n/a
387n/a attrib = {"key": "value"}
388n/a elem = ET.Element("tag", **attrib)
389n/a attrib.clear() # check for aliasing issues
390n/a self.assertEqual(elem.get("key"), 'value') # 4.1
391n/a self.assertEqual(elem.attrib, {'key': 'value'}) # 4.2
392n/a
393n/a elem = ET.Element("tag", {"key": "other"}, key="value")
394n/a self.assertEqual(elem.get("key"), 'value') # 5.1
395n/a self.assertEqual(elem.attrib, {'key': 'value'}) # 5.2
396n/a
397n/a elem = ET.Element('test')
398n/a elem.text = "aa"
399n/a elem.set('testa', 'testval')
400n/a elem.set('testb', 'test2')
401n/a self.assertEqual(ET.tostring(elem),
402n/a b'<test testa="testval" testb="test2">aa</test>')
403n/a self.assertEqual(sorted(elem.keys()), ['testa', 'testb'])
404n/a self.assertEqual(sorted(elem.items()),
405n/a [('testa', 'testval'), ('testb', 'test2')])
406n/a self.assertEqual(elem.attrib['testb'], 'test2')
407n/a elem.attrib['testb'] = 'test1'
408n/a elem.attrib['testc'] = 'test2'
409n/a self.assertEqual(ET.tostring(elem),
410n/a b'<test testa="testval" testb="test1" testc="test2">aa</test>')
411n/a
412n/a elem = ET.Element('test')
413n/a elem.set('a', '\r')
414n/a elem.set('b', '\r\n')
415n/a elem.set('c', '\t\n\r ')
416n/a elem.set('d', '\n\n')
417n/a self.assertEqual(ET.tostring(elem),
418n/a b'<test a="&#10;" b="&#10;" c="&#09;&#10;&#10; " d="&#10;&#10;" />')
419n/a
420n/a def test_makeelement(self):
421n/a # Test makeelement handling.
422n/a
423n/a elem = ET.Element("tag")
424n/a attrib = {"key": "value"}
425n/a subelem = elem.makeelement("subtag", attrib)
426n/a self.assertIsNot(subelem.attrib, attrib, msg="attrib aliasing")
427n/a elem.append(subelem)
428n/a self.serialize_check(elem, '<tag><subtag key="value" /></tag>')
429n/a
430n/a elem.clear()
431n/a self.serialize_check(elem, '<tag />')
432n/a elem.append(subelem)
433n/a self.serialize_check(elem, '<tag><subtag key="value" /></tag>')
434n/a elem.extend([subelem, subelem])
435n/a self.serialize_check(elem,
436n/a '<tag><subtag key="value" /><subtag key="value" /><subtag key="value" /></tag>')
437n/a elem[:] = [subelem]
438n/a self.serialize_check(elem, '<tag><subtag key="value" /></tag>')
439n/a elem[:] = tuple([subelem])
440n/a self.serialize_check(elem, '<tag><subtag key="value" /></tag>')
441n/a
442n/a def test_parsefile(self):
443n/a # Test parsing from file.
444n/a
445n/a tree = ET.parse(SIMPLE_XMLFILE)
446n/a stream = io.StringIO()
447n/a tree.write(stream, encoding='unicode')
448n/a self.assertEqual(stream.getvalue(),
449n/a '<root>\n'
450n/a ' <element key="value">text</element>\n'
451n/a ' <element>text</element>tail\n'
452n/a ' <empty-element />\n'
453n/a '</root>')
454n/a tree = ET.parse(SIMPLE_NS_XMLFILE)
455n/a stream = io.StringIO()
456n/a tree.write(stream, encoding='unicode')
457n/a self.assertEqual(stream.getvalue(),
458n/a '<ns0:root xmlns:ns0="namespace">\n'
459n/a ' <ns0:element key="value">text</ns0:element>\n'
460n/a ' <ns0:element>text</ns0:element>tail\n'
461n/a ' <ns0:empty-element />\n'
462n/a '</ns0:root>')
463n/a
464n/a with open(SIMPLE_XMLFILE) as f:
465n/a data = f.read()
466n/a
467n/a parser = ET.XMLParser()
468n/a self.assertRegex(parser.version, r'^Expat ')
469n/a parser.feed(data)
470n/a self.serialize_check(parser.close(),
471n/a '<root>\n'
472n/a ' <element key="value">text</element>\n'
473n/a ' <element>text</element>tail\n'
474n/a ' <empty-element />\n'
475n/a '</root>')
476n/a
477n/a target = ET.TreeBuilder()
478n/a parser = ET.XMLParser(target=target)
479n/a parser.feed(data)
480n/a self.serialize_check(parser.close(),
481n/a '<root>\n'
482n/a ' <element key="value">text</element>\n'
483n/a ' <element>text</element>tail\n'
484n/a ' <empty-element />\n'
485n/a '</root>')
486n/a
487n/a def test_parseliteral(self):
488n/a element = ET.XML("<html><body>text</body></html>")
489n/a self.assertEqual(ET.tostring(element, encoding='unicode'),
490n/a '<html><body>text</body></html>')
491n/a element = ET.fromstring("<html><body>text</body></html>")
492n/a self.assertEqual(ET.tostring(element, encoding='unicode'),
493n/a '<html><body>text</body></html>')
494n/a sequence = ["<html><body>", "text</bo", "dy></html>"]
495n/a element = ET.fromstringlist(sequence)
496n/a self.assertEqual(ET.tostring(element),
497n/a b'<html><body>text</body></html>')
498n/a self.assertEqual(b"".join(ET.tostringlist(element)),
499n/a b'<html><body>text</body></html>')
500n/a self.assertEqual(ET.tostring(element, "ascii"),
501n/a b"<?xml version='1.0' encoding='ascii'?>\n"
502n/a b"<html><body>text</body></html>")
503n/a _, ids = ET.XMLID("<html><body>text</body></html>")
504n/a self.assertEqual(len(ids), 0)
505n/a _, ids = ET.XMLID("<html><body id='body'>text</body></html>")
506n/a self.assertEqual(len(ids), 1)
507n/a self.assertEqual(ids["body"].tag, 'body')
508n/a
509n/a def test_iterparse(self):
510n/a # Test iterparse interface.
511n/a
512n/a iterparse = ET.iterparse
513n/a
514n/a context = iterparse(SIMPLE_XMLFILE)
515n/a action, elem = next(context)
516n/a self.assertEqual((action, elem.tag), ('end', 'element'))
517n/a self.assertEqual([(action, elem.tag) for action, elem in context], [
518n/a ('end', 'element'),
519n/a ('end', 'empty-element'),
520n/a ('end', 'root'),
521n/a ])
522n/a self.assertEqual(context.root.tag, 'root')
523n/a
524n/a context = iterparse(SIMPLE_NS_XMLFILE)
525n/a self.assertEqual([(action, elem.tag) for action, elem in context], [
526n/a ('end', '{namespace}element'),
527n/a ('end', '{namespace}element'),
528n/a ('end', '{namespace}empty-element'),
529n/a ('end', '{namespace}root'),
530n/a ])
531n/a
532n/a events = ()
533n/a context = iterparse(SIMPLE_XMLFILE, events)
534n/a self.assertEqual([(action, elem.tag) for action, elem in context], [])
535n/a
536n/a events = ()
537n/a context = iterparse(SIMPLE_XMLFILE, events=events)
538n/a self.assertEqual([(action, elem.tag) for action, elem in context], [])
539n/a
540n/a events = ("start", "end")
541n/a context = iterparse(SIMPLE_XMLFILE, events)
542n/a self.assertEqual([(action, elem.tag) for action, elem in context], [
543n/a ('start', 'root'),
544n/a ('start', 'element'),
545n/a ('end', 'element'),
546n/a ('start', 'element'),
547n/a ('end', 'element'),
548n/a ('start', 'empty-element'),
549n/a ('end', 'empty-element'),
550n/a ('end', 'root'),
551n/a ])
552n/a
553n/a events = ("start", "end", "start-ns", "end-ns")
554n/a context = iterparse(SIMPLE_NS_XMLFILE, events)
555n/a self.assertEqual([(action, elem.tag) if action in ("start", "end")
556n/a else (action, elem)
557n/a for action, elem in context], [
558n/a ('start-ns', ('', 'namespace')),
559n/a ('start', '{namespace}root'),
560n/a ('start', '{namespace}element'),
561n/a ('end', '{namespace}element'),
562n/a ('start', '{namespace}element'),
563n/a ('end', '{namespace}element'),
564n/a ('start', '{namespace}empty-element'),
565n/a ('end', '{namespace}empty-element'),
566n/a ('end', '{namespace}root'),
567n/a ('end-ns', None),
568n/a ])
569n/a
570n/a events = ('start-ns', 'end-ns')
571n/a context = iterparse(io.StringIO(r"<root xmlns=''/>"), events)
572n/a res = [action for action, elem in context]
573n/a self.assertEqual(res, ['start-ns', 'end-ns'])
574n/a
575n/a events = ("start", "end", "bogus")
576n/a with open(SIMPLE_XMLFILE, "rb") as f:
577n/a with self.assertRaises(ValueError) as cm:
578n/a iterparse(f, events)
579n/a self.assertFalse(f.closed)
580n/a self.assertEqual(str(cm.exception), "unknown event 'bogus'")
581n/a
582n/a with support.check_no_resource_warning(self):
583n/a with self.assertRaises(ValueError) as cm:
584n/a iterparse(SIMPLE_XMLFILE, events)
585n/a self.assertEqual(str(cm.exception), "unknown event 'bogus'")
586n/a del cm
587n/a
588n/a source = io.BytesIO(
589n/a b"<?xml version='1.0' encoding='iso-8859-1'?>\n"
590n/a b"<body xmlns='http://&#233;ffbot.org/ns'\n"
591n/a b" xmlns:cl\xe9='http://effbot.org/ns'>text</body>\n")
592n/a events = ("start-ns",)
593n/a context = iterparse(source, events)
594n/a self.assertEqual([(action, elem) for action, elem in context], [
595n/a ('start-ns', ('', 'http://\xe9ffbot.org/ns')),
596n/a ('start-ns', ('cl\xe9', 'http://effbot.org/ns')),
597n/a ])
598n/a
599n/a source = io.StringIO("<document />junk")
600n/a it = iterparse(source)
601n/a action, elem = next(it)
602n/a self.assertEqual((action, elem.tag), ('end', 'document'))
603n/a with self.assertRaises(ET.ParseError) as cm:
604n/a next(it)
605n/a self.assertEqual(str(cm.exception),
606n/a 'junk after document element: line 1, column 12')
607n/a
608n/a with open(TESTFN, "wb") as f:
609n/a f.write(b"<document />junk")
610n/a it = iterparse(TESTFN)
611n/a action, elem = next(it)
612n/a self.assertEqual((action, elem.tag), ('end', 'document'))
613n/a with support.check_no_resource_warning(self):
614n/a with self.assertRaises(ET.ParseError) as cm:
615n/a next(it)
616n/a self.assertEqual(str(cm.exception),
617n/a 'junk after document element: line 1, column 12')
618n/a del cm, it
619n/a
620n/a def test_writefile(self):
621n/a elem = ET.Element("tag")
622n/a elem.text = "text"
623n/a self.serialize_check(elem, '<tag>text</tag>')
624n/a ET.SubElement(elem, "subtag").text = "subtext"
625n/a self.serialize_check(elem, '<tag>text<subtag>subtext</subtag></tag>')
626n/a
627n/a # Test tag suppression
628n/a elem.tag = None
629n/a self.serialize_check(elem, 'text<subtag>subtext</subtag>')
630n/a elem.insert(0, ET.Comment("comment"))
631n/a self.serialize_check(elem,
632n/a 'text<!--comment--><subtag>subtext</subtag>') # assumes 1.3
633n/a
634n/a elem[0] = ET.PI("key", "value")
635n/a self.serialize_check(elem, 'text<?key value?><subtag>subtext</subtag>')
636n/a
637n/a def test_custom_builder(self):
638n/a # Test parser w. custom builder.
639n/a
640n/a with open(SIMPLE_XMLFILE) as f:
641n/a data = f.read()
642n/a class Builder(list):
643n/a def start(self, tag, attrib):
644n/a self.append(("start", tag))
645n/a def end(self, tag):
646n/a self.append(("end", tag))
647n/a def data(self, text):
648n/a pass
649n/a builder = Builder()
650n/a parser = ET.XMLParser(target=builder)
651n/a parser.feed(data)
652n/a self.assertEqual(builder, [
653n/a ('start', 'root'),
654n/a ('start', 'element'),
655n/a ('end', 'element'),
656n/a ('start', 'element'),
657n/a ('end', 'element'),
658n/a ('start', 'empty-element'),
659n/a ('end', 'empty-element'),
660n/a ('end', 'root'),
661n/a ])
662n/a
663n/a with open(SIMPLE_NS_XMLFILE) as f:
664n/a data = f.read()
665n/a class Builder(list):
666n/a def start(self, tag, attrib):
667n/a self.append(("start", tag))
668n/a def end(self, tag):
669n/a self.append(("end", tag))
670n/a def data(self, text):
671n/a pass
672n/a def pi(self, target, data):
673n/a self.append(("pi", target, data))
674n/a def comment(self, data):
675n/a self.append(("comment", data))
676n/a builder = Builder()
677n/a parser = ET.XMLParser(target=builder)
678n/a parser.feed(data)
679n/a self.assertEqual(builder, [
680n/a ('pi', 'pi', 'data'),
681n/a ('comment', ' comment '),
682n/a ('start', '{namespace}root'),
683n/a ('start', '{namespace}element'),
684n/a ('end', '{namespace}element'),
685n/a ('start', '{namespace}element'),
686n/a ('end', '{namespace}element'),
687n/a ('start', '{namespace}empty-element'),
688n/a ('end', '{namespace}empty-element'),
689n/a ('end', '{namespace}root'),
690n/a ])
691n/a
692n/a
693n/a def test_getchildren(self):
694n/a # Test Element.getchildren()
695n/a
696n/a with open(SIMPLE_XMLFILE, "rb") as f:
697n/a tree = ET.parse(f)
698n/a self.assertEqual([summarize_list(elem.getchildren())
699n/a for elem in tree.getroot().iter()], [
700n/a ['element', 'element', 'empty-element'],
701n/a [],
702n/a [],
703n/a [],
704n/a ])
705n/a self.assertEqual([summarize_list(elem.getchildren())
706n/a for elem in tree.getiterator()], [
707n/a ['element', 'element', 'empty-element'],
708n/a [],
709n/a [],
710n/a [],
711n/a ])
712n/a
713n/a elem = ET.XML(SAMPLE_XML)
714n/a self.assertEqual(len(elem.getchildren()), 3)
715n/a self.assertEqual(len(elem[2].getchildren()), 1)
716n/a self.assertEqual(elem[:], elem.getchildren())
717n/a child1 = elem[0]
718n/a child2 = elem[2]
719n/a del elem[1:2]
720n/a self.assertEqual(len(elem.getchildren()), 2)
721n/a self.assertEqual(child1, elem[0])
722n/a self.assertEqual(child2, elem[1])
723n/a elem[0:2] = [child2, child1]
724n/a self.assertEqual(child2, elem[0])
725n/a self.assertEqual(child1, elem[1])
726n/a self.assertNotEqual(child1, elem[0])
727n/a elem.clear()
728n/a self.assertEqual(elem.getchildren(), [])
729n/a
730n/a def test_writestring(self):
731n/a elem = ET.XML("<html><body>text</body></html>")
732n/a self.assertEqual(ET.tostring(elem), b'<html><body>text</body></html>')
733n/a elem = ET.fromstring("<html><body>text</body></html>")
734n/a self.assertEqual(ET.tostring(elem), b'<html><body>text</body></html>')
735n/a
736n/a def test_encoding(self):
737n/a def check(encoding, body=''):
738n/a xml = ("<?xml version='1.0' encoding='%s'?><xml>%s</xml>" %
739n/a (encoding, body))
740n/a self.assertEqual(ET.XML(xml.encode(encoding)).text, body)
741n/a self.assertEqual(ET.XML(xml).text, body)
742n/a check("ascii", 'a')
743n/a check("us-ascii", 'a')
744n/a check("iso-8859-1", '\xbd')
745n/a check("iso-8859-15", '\u20ac')
746n/a check("cp437", '\u221a')
747n/a check("mac-roman", '\u02da')
748n/a
749n/a def xml(encoding):
750n/a return "<?xml version='1.0' encoding='%s'?><xml />" % encoding
751n/a def bxml(encoding):
752n/a return xml(encoding).encode(encoding)
753n/a supported_encodings = [
754n/a 'ascii', 'utf-8', 'utf-8-sig', 'utf-16', 'utf-16be', 'utf-16le',
755n/a 'iso8859-1', 'iso8859-2', 'iso8859-3', 'iso8859-4', 'iso8859-5',
756n/a 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10',
757n/a 'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16',
758n/a 'cp437', 'cp720', 'cp737', 'cp775', 'cp850', 'cp852',
759n/a 'cp855', 'cp856', 'cp857', 'cp858', 'cp860', 'cp861', 'cp862',
760n/a 'cp863', 'cp865', 'cp866', 'cp869', 'cp874', 'cp1006', 'cp1125',
761n/a 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255',
762n/a 'cp1256', 'cp1257', 'cp1258',
763n/a 'mac-cyrillic', 'mac-greek', 'mac-iceland', 'mac-latin2',
764n/a 'mac-roman', 'mac-turkish',
765n/a 'iso2022-jp', 'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004',
766n/a 'iso2022-jp-3', 'iso2022-jp-ext',
767n/a 'koi8-r', 'koi8-t', 'koi8-u', 'kz1048',
768n/a 'hz', 'ptcp154',
769n/a ]
770n/a for encoding in supported_encodings:
771n/a self.assertEqual(ET.tostring(ET.XML(bxml(encoding))), b'<xml />')
772n/a
773n/a unsupported_ascii_compatible_encodings = [
774n/a 'big5', 'big5hkscs',
775n/a 'cp932', 'cp949', 'cp950',
776n/a 'euc-jp', 'euc-jis-2004', 'euc-jisx0213', 'euc-kr',
777n/a 'gb2312', 'gbk', 'gb18030',
778n/a 'iso2022-kr', 'johab',
779n/a 'shift-jis', 'shift-jis-2004', 'shift-jisx0213',
780n/a 'utf-7',
781n/a ]
782n/a for encoding in unsupported_ascii_compatible_encodings:
783n/a self.assertRaises(ValueError, ET.XML, bxml(encoding))
784n/a
785n/a unsupported_ascii_incompatible_encodings = [
786n/a 'cp037', 'cp424', 'cp500', 'cp864', 'cp875', 'cp1026', 'cp1140',
787n/a 'utf_32', 'utf_32_be', 'utf_32_le',
788n/a ]
789n/a for encoding in unsupported_ascii_incompatible_encodings:
790n/a self.assertRaises(ET.ParseError, ET.XML, bxml(encoding))
791n/a
792n/a self.assertRaises(ValueError, ET.XML, xml('undefined').encode('ascii'))
793n/a self.assertRaises(LookupError, ET.XML, xml('xxx').encode('ascii'))
794n/a
795n/a def test_methods(self):
796n/a # Test serialization methods.
797n/a
798n/a e = ET.XML("<html><link/><script>1 &lt; 2</script></html>")
799n/a e.tail = "\n"
800n/a self.assertEqual(serialize(e),
801n/a '<html><link /><script>1 &lt; 2</script></html>\n')
802n/a self.assertEqual(serialize(e, method=None),
803n/a '<html><link /><script>1 &lt; 2</script></html>\n')
804n/a self.assertEqual(serialize(e, method="xml"),
805n/a '<html><link /><script>1 &lt; 2</script></html>\n')
806n/a self.assertEqual(serialize(e, method="html"),
807n/a '<html><link><script>1 < 2</script></html>\n')
808n/a self.assertEqual(serialize(e, method="text"), '1 < 2\n')
809n/a
810n/a def test_issue18347(self):
811n/a e = ET.XML('<html><CamelCase>text</CamelCase></html>')
812n/a self.assertEqual(serialize(e),
813n/a '<html><CamelCase>text</CamelCase></html>')
814n/a self.assertEqual(serialize(e, method="html"),
815n/a '<html><CamelCase>text</CamelCase></html>')
816n/a
817n/a def test_entity(self):
818n/a # Test entity handling.
819n/a
820n/a # 1) good entities
821n/a
822n/a e = ET.XML("<document title='&#x8230;'>test</document>")
823n/a self.assertEqual(serialize(e, encoding="us-ascii"),
824n/a b'<document title="&#33328;">test</document>')
825n/a self.serialize_check(e, '<document title="\u8230">test</document>')
826n/a
827n/a # 2) bad entities
828n/a
829n/a with self.assertRaises(ET.ParseError) as cm:
830n/a ET.XML("<document>&entity;</document>")
831n/a self.assertEqual(str(cm.exception),
832n/a 'undefined entity: line 1, column 10')
833n/a
834n/a with self.assertRaises(ET.ParseError) as cm:
835n/a ET.XML(ENTITY_XML)
836n/a self.assertEqual(str(cm.exception),
837n/a 'undefined entity &entity;: line 5, column 10')
838n/a
839n/a # 3) custom entity
840n/a
841n/a parser = ET.XMLParser()
842n/a parser.entity["entity"] = "text"
843n/a parser.feed(ENTITY_XML)
844n/a root = parser.close()
845n/a self.serialize_check(root, '<document>text</document>')
846n/a
847n/a def test_namespace(self):
848n/a # Test namespace issues.
849n/a
850n/a # 1) xml namespace
851n/a
852n/a elem = ET.XML("<tag xml:lang='en' />")
853n/a self.serialize_check(elem, '<tag xml:lang="en" />') # 1.1
854n/a
855n/a # 2) other "well-known" namespaces
856n/a
857n/a elem = ET.XML("<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' />")
858n/a self.serialize_check(elem,
859n/a '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" />') # 2.1
860n/a
861n/a elem = ET.XML("<html:html xmlns:html='http://www.w3.org/1999/xhtml' />")
862n/a self.serialize_check(elem,
863n/a '<html:html xmlns:html="http://www.w3.org/1999/xhtml" />') # 2.2
864n/a
865n/a elem = ET.XML("<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope' />")
866n/a self.serialize_check(elem,
867n/a '<ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope" />') # 2.3
868n/a
869n/a # 3) unknown namespaces
870n/a elem = ET.XML(SAMPLE_XML_NS)
871n/a self.serialize_check(elem,
872n/a '<ns0:body xmlns:ns0="http://effbot.org/ns">\n'
873n/a ' <ns0:tag>text</ns0:tag>\n'
874n/a ' <ns0:tag />\n'
875n/a ' <ns0:section>\n'
876n/a ' <ns0:tag>subtext</ns0:tag>\n'
877n/a ' </ns0:section>\n'
878n/a '</ns0:body>')
879n/a
880n/a def test_qname(self):
881n/a # Test QName handling.
882n/a
883n/a # 1) decorated tags
884n/a
885n/a elem = ET.Element("{uri}tag")
886n/a self.serialize_check(elem, '<ns0:tag xmlns:ns0="uri" />') # 1.1
887n/a elem = ET.Element(ET.QName("{uri}tag"))
888n/a self.serialize_check(elem, '<ns0:tag xmlns:ns0="uri" />') # 1.2
889n/a elem = ET.Element(ET.QName("uri", "tag"))
890n/a self.serialize_check(elem, '<ns0:tag xmlns:ns0="uri" />') # 1.3
891n/a elem = ET.Element(ET.QName("uri", "tag"))
892n/a subelem = ET.SubElement(elem, ET.QName("uri", "tag1"))
893n/a subelem = ET.SubElement(elem, ET.QName("uri", "tag2"))
894n/a self.serialize_check(elem,
895n/a '<ns0:tag xmlns:ns0="uri"><ns0:tag1 /><ns0:tag2 /></ns0:tag>') # 1.4
896n/a
897n/a # 2) decorated attributes
898n/a
899n/a elem.clear()
900n/a elem.attrib["{uri}key"] = "value"
901n/a self.serialize_check(elem,
902n/a '<ns0:tag xmlns:ns0="uri" ns0:key="value" />') # 2.1
903n/a
904n/a elem.clear()
905n/a elem.attrib[ET.QName("{uri}key")] = "value"
906n/a self.serialize_check(elem,
907n/a '<ns0:tag xmlns:ns0="uri" ns0:key="value" />') # 2.2
908n/a
909n/a # 3) decorated values are not converted by default, but the
910n/a # QName wrapper can be used for values
911n/a
912n/a elem.clear()
913n/a elem.attrib["{uri}key"] = "{uri}value"
914n/a self.serialize_check(elem,
915n/a '<ns0:tag xmlns:ns0="uri" ns0:key="{uri}value" />') # 3.1
916n/a
917n/a elem.clear()
918n/a elem.attrib["{uri}key"] = ET.QName("{uri}value")
919n/a self.serialize_check(elem,
920n/a '<ns0:tag xmlns:ns0="uri" ns0:key="ns0:value" />') # 3.2
921n/a
922n/a elem.clear()
923n/a subelem = ET.Element("tag")
924n/a subelem.attrib["{uri1}key"] = ET.QName("{uri2}value")
925n/a elem.append(subelem)
926n/a elem.append(subelem)
927n/a self.serialize_check(elem,
928n/a '<ns0:tag xmlns:ns0="uri" xmlns:ns1="uri1" xmlns:ns2="uri2">'
929n/a '<tag ns1:key="ns2:value" />'
930n/a '<tag ns1:key="ns2:value" />'
931n/a '</ns0:tag>') # 3.3
932n/a
933n/a # 4) Direct QName tests
934n/a
935n/a self.assertEqual(str(ET.QName('ns', 'tag')), '{ns}tag')
936n/a self.assertEqual(str(ET.QName('{ns}tag')), '{ns}tag')
937n/a q1 = ET.QName('ns', 'tag')
938n/a q2 = ET.QName('ns', 'tag')
939n/a self.assertEqual(q1, q2)
940n/a q2 = ET.QName('ns', 'other-tag')
941n/a self.assertNotEqual(q1, q2)
942n/a self.assertNotEqual(q1, 'ns:tag')
943n/a self.assertEqual(q1, '{ns}tag')
944n/a
945n/a def test_doctype_public(self):
946n/a # Test PUBLIC doctype.
947n/a
948n/a elem = ET.XML('<!DOCTYPE html PUBLIC'
949n/a ' "-//W3C//DTD XHTML 1.0 Transitional//EN"'
950n/a ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
951n/a '<html>text</html>')
952n/a
953n/a def test_xpath_tokenizer(self):
954n/a # Test the XPath tokenizer.
955n/a from xml.etree import ElementPath
956n/a def check(p, expected):
957n/a self.assertEqual([op or tag
958n/a for op, tag in ElementPath.xpath_tokenizer(p)],
959n/a expected)
960n/a
961n/a # tests from the xml specification
962n/a check("*", ['*'])
963n/a check("text()", ['text', '()'])
964n/a check("@name", ['@', 'name'])
965n/a check("@*", ['@', '*'])
966n/a check("para[1]", ['para', '[', '1', ']'])
967n/a check("para[last()]", ['para', '[', 'last', '()', ']'])
968n/a check("*/para", ['*', '/', 'para'])
969n/a check("/doc/chapter[5]/section[2]",
970n/a ['/', 'doc', '/', 'chapter', '[', '5', ']',
971n/a '/', 'section', '[', '2', ']'])
972n/a check("chapter//para", ['chapter', '//', 'para'])
973n/a check("//para", ['//', 'para'])
974n/a check("//olist/item", ['//', 'olist', '/', 'item'])
975n/a check(".", ['.'])
976n/a check(".//para", ['.', '//', 'para'])
977n/a check("..", ['..'])
978n/a check("../@lang", ['..', '/', '@', 'lang'])
979n/a check("chapter[title]", ['chapter', '[', 'title', ']'])
980n/a check("employee[@secretary and @assistant]", ['employee',
981n/a '[', '@', 'secretary', '', 'and', '', '@', 'assistant', ']'])
982n/a
983n/a # additional tests
984n/a check("{http://spam}egg", ['{http://spam}egg'])
985n/a check("./spam.egg", ['.', '/', 'spam.egg'])
986n/a check(".//{http://spam}egg", ['.', '//', '{http://spam}egg'])
987n/a
988n/a def test_processinginstruction(self):
989n/a # Test ProcessingInstruction directly
990n/a
991n/a self.assertEqual(ET.tostring(ET.ProcessingInstruction('test', 'instruction')),
992n/a b'<?test instruction?>')
993n/a self.assertEqual(ET.tostring(ET.PI('test', 'instruction')),
994n/a b'<?test instruction?>')
995n/a
996n/a # Issue #2746
997n/a
998n/a self.assertEqual(ET.tostring(ET.PI('test', '<testing&>')),
999n/a b'<?test <testing&>?>')
1000n/a self.assertEqual(ET.tostring(ET.PI('test', '<testing&>\xe3'), 'latin-1'),
1001n/a b"<?xml version='1.0' encoding='latin-1'?>\n"
1002n/a b"<?test <testing&>\xe3?>")
1003n/a
1004n/a def test_html_empty_elems_serialization(self):
1005n/a # issue 15970
1006n/a # from http://www.w3.org/TR/html401/index/elements.html
1007n/a for element in ['AREA', 'BASE', 'BASEFONT', 'BR', 'COL', 'FRAME', 'HR',
1008n/a 'IMG', 'INPUT', 'ISINDEX', 'LINK', 'META', 'PARAM']:
1009n/a for elem in [element, element.lower()]:
1010n/a expected = '<%s>' % elem
1011n/a serialized = serialize(ET.XML('<%s />' % elem), method='html')
1012n/a self.assertEqual(serialized, expected)
1013n/a serialized = serialize(ET.XML('<%s></%s>' % (elem,elem)),
1014n/a method='html')
1015n/a self.assertEqual(serialized, expected)
1016n/a
1017n/a
1018n/aclass XMLPullParserTest(unittest.TestCase):
1019n/a
1020n/a def _feed(self, parser, data, chunk_size=None):
1021n/a if chunk_size is None:
1022n/a parser.feed(data)
1023n/a else:
1024n/a for i in range(0, len(data), chunk_size):
1025n/a parser.feed(data[i:i+chunk_size])
1026n/a
1027n/a def assert_event_tags(self, parser, expected):
1028n/a events = parser.read_events()
1029n/a self.assertEqual([(action, elem.tag) for action, elem in events],
1030n/a expected)
1031n/a
1032n/a def test_simple_xml(self):
1033n/a for chunk_size in (None, 1, 5):
1034n/a with self.subTest(chunk_size=chunk_size):
1035n/a parser = ET.XMLPullParser()
1036n/a self.assert_event_tags(parser, [])
1037n/a self._feed(parser, "<!-- comment -->\n", chunk_size)
1038n/a self.assert_event_tags(parser, [])
1039n/a self._feed(parser,
1040n/a "<root>\n <element key='value'>text</element",
1041n/a chunk_size)
1042n/a self.assert_event_tags(parser, [])
1043n/a self._feed(parser, ">\n", chunk_size)
1044n/a self.assert_event_tags(parser, [('end', 'element')])
1045n/a self._feed(parser, "<element>text</element>tail\n", chunk_size)
1046n/a self._feed(parser, "<empty-element/>\n", chunk_size)
1047n/a self.assert_event_tags(parser, [
1048n/a ('end', 'element'),
1049n/a ('end', 'empty-element'),
1050n/a ])
1051n/a self._feed(parser, "</root>\n", chunk_size)
1052n/a self.assert_event_tags(parser, [('end', 'root')])
1053n/a self.assertIsNone(parser.close())
1054n/a
1055n/a def test_feed_while_iterating(self):
1056n/a parser = ET.XMLPullParser()
1057n/a it = parser.read_events()
1058n/a self._feed(parser, "<root>\n <element key='value'>text</element>\n")
1059n/a action, elem = next(it)
1060n/a self.assertEqual((action, elem.tag), ('end', 'element'))
1061n/a self._feed(parser, "</root>\n")
1062n/a action, elem = next(it)
1063n/a self.assertEqual((action, elem.tag), ('end', 'root'))
1064n/a with self.assertRaises(StopIteration):
1065n/a next(it)
1066n/a
1067n/a def test_simple_xml_with_ns(self):
1068n/a parser = ET.XMLPullParser()
1069n/a self.assert_event_tags(parser, [])
1070n/a self._feed(parser, "<!-- comment -->\n")
1071n/a self.assert_event_tags(parser, [])
1072n/a self._feed(parser, "<root xmlns='namespace'>\n")
1073n/a self.assert_event_tags(parser, [])
1074n/a self._feed(parser, "<element key='value'>text</element")
1075n/a self.assert_event_tags(parser, [])
1076n/a self._feed(parser, ">\n")
1077n/a self.assert_event_tags(parser, [('end', '{namespace}element')])
1078n/a self._feed(parser, "<element>text</element>tail\n")
1079n/a self._feed(parser, "<empty-element/>\n")
1080n/a self.assert_event_tags(parser, [
1081n/a ('end', '{namespace}element'),
1082n/a ('end', '{namespace}empty-element'),
1083n/a ])
1084n/a self._feed(parser, "</root>\n")
1085n/a self.assert_event_tags(parser, [('end', '{namespace}root')])
1086n/a self.assertIsNone(parser.close())
1087n/a
1088n/a def test_ns_events(self):
1089n/a parser = ET.XMLPullParser(events=('start-ns', 'end-ns'))
1090n/a self._feed(parser, "<!-- comment -->\n")
1091n/a self._feed(parser, "<root xmlns='namespace'>\n")
1092n/a self.assertEqual(
1093n/a list(parser.read_events()),
1094n/a [('start-ns', ('', 'namespace'))])
1095n/a self._feed(parser, "<element key='value'>text</element")
1096n/a self._feed(parser, ">\n")
1097n/a self._feed(parser, "<element>text</element>tail\n")
1098n/a self._feed(parser, "<empty-element/>\n")
1099n/a self._feed(parser, "</root>\n")
1100n/a self.assertEqual(list(parser.read_events()), [('end-ns', None)])
1101n/a self.assertIsNone(parser.close())
1102n/a
1103n/a def test_events(self):
1104n/a parser = ET.XMLPullParser(events=())
1105n/a self._feed(parser, "<root/>\n")
1106n/a self.assert_event_tags(parser, [])
1107n/a
1108n/a parser = ET.XMLPullParser(events=('start', 'end'))
1109n/a self._feed(parser, "<!-- comment -->\n")
1110n/a self.assert_event_tags(parser, [])
1111n/a self._feed(parser, "<root>\n")
1112n/a self.assert_event_tags(parser, [('start', 'root')])
1113n/a self._feed(parser, "<element key='value'>text</element")
1114n/a self.assert_event_tags(parser, [('start', 'element')])
1115n/a self._feed(parser, ">\n")
1116n/a self.assert_event_tags(parser, [('end', 'element')])
1117n/a self._feed(parser,
1118n/a "<element xmlns='foo'>text<empty-element/></element>tail\n")
1119n/a self.assert_event_tags(parser, [
1120n/a ('start', '{foo}element'),
1121n/a ('start', '{foo}empty-element'),
1122n/a ('end', '{foo}empty-element'),
1123n/a ('end', '{foo}element'),
1124n/a ])
1125n/a self._feed(parser, "</root>")
1126n/a self.assertIsNone(parser.close())
1127n/a self.assert_event_tags(parser, [('end', 'root')])
1128n/a
1129n/a parser = ET.XMLPullParser(events=('start',))
1130n/a self._feed(parser, "<!-- comment -->\n")
1131n/a self.assert_event_tags(parser, [])
1132n/a self._feed(parser, "<root>\n")
1133n/a self.assert_event_tags(parser, [('start', 'root')])
1134n/a self._feed(parser, "<element key='value'>text</element")
1135n/a self.assert_event_tags(parser, [('start', 'element')])
1136n/a self._feed(parser, ">\n")
1137n/a self.assert_event_tags(parser, [])
1138n/a self._feed(parser,
1139n/a "<element xmlns='foo'>text<empty-element/></element>tail\n")
1140n/a self.assert_event_tags(parser, [
1141n/a ('start', '{foo}element'),
1142n/a ('start', '{foo}empty-element'),
1143n/a ])
1144n/a self._feed(parser, "</root>")
1145n/a self.assertIsNone(parser.close())
1146n/a
1147n/a def test_events_sequence(self):
1148n/a # Test that events can be some sequence that's not just a tuple or list
1149n/a eventset = {'end', 'start'}
1150n/a parser = ET.XMLPullParser(events=eventset)
1151n/a self._feed(parser, "<foo>bar</foo>")
1152n/a self.assert_event_tags(parser, [('start', 'foo'), ('end', 'foo')])
1153n/a
1154n/a class DummyIter:
1155n/a def __init__(self):
1156n/a self.events = iter(['start', 'end', 'start-ns'])
1157n/a def __iter__(self):
1158n/a return self
1159n/a def __next__(self):
1160n/a return next(self.events)
1161n/a
1162n/a parser = ET.XMLPullParser(events=DummyIter())
1163n/a self._feed(parser, "<foo>bar</foo>")
1164n/a self.assert_event_tags(parser, [('start', 'foo'), ('end', 'foo')])
1165n/a
1166n/a
1167n/a def test_unknown_event(self):
1168n/a with self.assertRaises(ValueError):
1169n/a ET.XMLPullParser(events=('start', 'end', 'bogus'))
1170n/a
1171n/a
1172n/a#
1173n/a# xinclude tests (samples from appendix C of the xinclude specification)
1174n/a
1175n/aXINCLUDE = {}
1176n/a
1177n/aXINCLUDE["C1.xml"] = """\
1178n/a<?xml version='1.0'?>
1179n/a<document xmlns:xi="http://www.w3.org/2001/XInclude">
1180n/a <p>120 Mz is adequate for an average home user.</p>
1181n/a <xi:include href="disclaimer.xml"/>
1182n/a</document>
1183n/a"""
1184n/a
1185n/aXINCLUDE["disclaimer.xml"] = """\
1186n/a<?xml version='1.0'?>
1187n/a<disclaimer>
1188n/a <p>The opinions represented herein represent those of the individual
1189n/a and should not be interpreted as official policy endorsed by this
1190n/a organization.</p>
1191n/a</disclaimer>
1192n/a"""
1193n/a
1194n/aXINCLUDE["C2.xml"] = """\
1195n/a<?xml version='1.0'?>
1196n/a<document xmlns:xi="http://www.w3.org/2001/XInclude">
1197n/a <p>This document has been accessed
1198n/a <xi:include href="count.txt" parse="text"/> times.</p>
1199n/a</document>
1200n/a"""
1201n/a
1202n/aXINCLUDE["count.txt"] = "324387"
1203n/a
1204n/aXINCLUDE["C2b.xml"] = """\
1205n/a<?xml version='1.0'?>
1206n/a<document xmlns:xi="http://www.w3.org/2001/XInclude">
1207n/a <p>This document has been <em>accessed</em>
1208n/a <xi:include href="count.txt" parse="text"/> times.</p>
1209n/a</document>
1210n/a"""
1211n/a
1212n/aXINCLUDE["C3.xml"] = """\
1213n/a<?xml version='1.0'?>
1214n/a<document xmlns:xi="http://www.w3.org/2001/XInclude">
1215n/a <p>The following is the source of the "data.xml" resource:</p>
1216n/a <example><xi:include href="data.xml" parse="text"/></example>
1217n/a</document>
1218n/a"""
1219n/a
1220n/aXINCLUDE["data.xml"] = """\
1221n/a<?xml version='1.0'?>
1222n/a<data>
1223n/a <item><![CDATA[Brooks & Shields]]></item>
1224n/a</data>
1225n/a"""
1226n/a
1227n/aXINCLUDE["C5.xml"] = """\
1228n/a<?xml version='1.0'?>
1229n/a<div xmlns:xi="http://www.w3.org/2001/XInclude">
1230n/a <xi:include href="example.txt" parse="text">
1231n/a <xi:fallback>
1232n/a <xi:include href="fallback-example.txt" parse="text">
1233n/a <xi:fallback><a href="mailto:bob@example.org">Report error</a></xi:fallback>
1234n/a </xi:include>
1235n/a </xi:fallback>
1236n/a </xi:include>
1237n/a</div>
1238n/a"""
1239n/a
1240n/aXINCLUDE["default.xml"] = """\
1241n/a<?xml version='1.0'?>
1242n/a<document xmlns:xi="http://www.w3.org/2001/XInclude">
1243n/a <p>Example.</p>
1244n/a <xi:include href="{}"/>
1245n/a</document>
1246n/a""".format(html.escape(SIMPLE_XMLFILE, True))
1247n/a
1248n/a#
1249n/a# badly formatted xi:include tags
1250n/a
1251n/aXINCLUDE_BAD = {}
1252n/a
1253n/aXINCLUDE_BAD["B1.xml"] = """\
1254n/a<?xml version='1.0'?>
1255n/a<document xmlns:xi="http://www.w3.org/2001/XInclude">
1256n/a <p>120 Mz is adequate for an average home user.</p>
1257n/a <xi:include href="disclaimer.xml" parse="BAD_TYPE"/>
1258n/a</document>
1259n/a"""
1260n/a
1261n/aXINCLUDE_BAD["B2.xml"] = """\
1262n/a<?xml version='1.0'?>
1263n/a<div xmlns:xi="http://www.w3.org/2001/XInclude">
1264n/a <xi:fallback></xi:fallback>
1265n/a</div>
1266n/a"""
1267n/a
1268n/aclass XIncludeTest(unittest.TestCase):
1269n/a
1270n/a def xinclude_loader(self, href, parse="xml", encoding=None):
1271n/a try:
1272n/a data = XINCLUDE[href]
1273n/a except KeyError:
1274n/a raise OSError("resource not found")
1275n/a if parse == "xml":
1276n/a data = ET.XML(data)
1277n/a return data
1278n/a
1279n/a def none_loader(self, href, parser, encoding=None):
1280n/a return None
1281n/a
1282n/a def _my_loader(self, href, parse):
1283n/a # Used to avoid a test-dependency problem where the default loader
1284n/a # of ElementInclude uses the pyET parser for cET tests.
1285n/a if parse == 'xml':
1286n/a with open(href, 'rb') as f:
1287n/a return ET.parse(f).getroot()
1288n/a else:
1289n/a return None
1290n/a
1291n/a def test_xinclude_default(self):
1292n/a from xml.etree import ElementInclude
1293n/a doc = self.xinclude_loader('default.xml')
1294n/a ElementInclude.include(doc, self._my_loader)
1295n/a self.assertEqual(serialize(doc),
1296n/a '<document>\n'
1297n/a ' <p>Example.</p>\n'
1298n/a ' <root>\n'
1299n/a ' <element key="value">text</element>\n'
1300n/a ' <element>text</element>tail\n'
1301n/a ' <empty-element />\n'
1302n/a '</root>\n'
1303n/a '</document>')
1304n/a
1305n/a def test_xinclude(self):
1306n/a from xml.etree import ElementInclude
1307n/a
1308n/a # Basic inclusion example (XInclude C.1)
1309n/a document = self.xinclude_loader("C1.xml")
1310n/a ElementInclude.include(document, self.xinclude_loader)
1311n/a self.assertEqual(serialize(document),
1312n/a '<document>\n'
1313n/a ' <p>120 Mz is adequate for an average home user.</p>\n'
1314n/a ' <disclaimer>\n'
1315n/a ' <p>The opinions represented herein represent those of the individual\n'
1316n/a ' and should not be interpreted as official policy endorsed by this\n'
1317n/a ' organization.</p>\n'
1318n/a '</disclaimer>\n'
1319n/a '</document>') # C1
1320n/a
1321n/a # Textual inclusion example (XInclude C.2)
1322n/a document = self.xinclude_loader("C2.xml")
1323n/a ElementInclude.include(document, self.xinclude_loader)
1324n/a self.assertEqual(serialize(document),
1325n/a '<document>\n'
1326n/a ' <p>This document has been accessed\n'
1327n/a ' 324387 times.</p>\n'
1328n/a '</document>') # C2
1329n/a
1330n/a # Textual inclusion after sibling element (based on modified XInclude C.2)
1331n/a document = self.xinclude_loader("C2b.xml")
1332n/a ElementInclude.include(document, self.xinclude_loader)
1333n/a self.assertEqual(serialize(document),
1334n/a '<document>\n'
1335n/a ' <p>This document has been <em>accessed</em>\n'
1336n/a ' 324387 times.</p>\n'
1337n/a '</document>') # C2b
1338n/a
1339n/a # Textual inclusion of XML example (XInclude C.3)
1340n/a document = self.xinclude_loader("C3.xml")
1341n/a ElementInclude.include(document, self.xinclude_loader)
1342n/a self.assertEqual(serialize(document),
1343n/a '<document>\n'
1344n/a ' <p>The following is the source of the "data.xml" resource:</p>\n'
1345n/a " <example>&lt;?xml version='1.0'?&gt;\n"
1346n/a '&lt;data&gt;\n'
1347n/a ' &lt;item&gt;&lt;![CDATA[Brooks &amp; Shields]]&gt;&lt;/item&gt;\n'
1348n/a '&lt;/data&gt;\n'
1349n/a '</example>\n'
1350n/a '</document>') # C3
1351n/a
1352n/a # Fallback example (XInclude C.5)
1353n/a # Note! Fallback support is not yet implemented
1354n/a document = self.xinclude_loader("C5.xml")
1355n/a with self.assertRaises(OSError) as cm:
1356n/a ElementInclude.include(document, self.xinclude_loader)
1357n/a self.assertEqual(str(cm.exception), 'resource not found')
1358n/a self.assertEqual(serialize(document),
1359n/a '<div xmlns:ns0="http://www.w3.org/2001/XInclude">\n'
1360n/a ' <ns0:include href="example.txt" parse="text">\n'
1361n/a ' <ns0:fallback>\n'
1362n/a ' <ns0:include href="fallback-example.txt" parse="text">\n'
1363n/a ' <ns0:fallback><a href="mailto:bob@example.org">Report error</a></ns0:fallback>\n'
1364n/a ' </ns0:include>\n'
1365n/a ' </ns0:fallback>\n'
1366n/a ' </ns0:include>\n'
1367n/a '</div>') # C5
1368n/a
1369n/a def test_xinclude_failures(self):
1370n/a from xml.etree import ElementInclude
1371n/a
1372n/a # Test failure to locate included XML file.
1373n/a document = ET.XML(XINCLUDE["C1.xml"])
1374n/a with self.assertRaises(ElementInclude.FatalIncludeError) as cm:
1375n/a ElementInclude.include(document, loader=self.none_loader)
1376n/a self.assertEqual(str(cm.exception),
1377n/a "cannot load 'disclaimer.xml' as 'xml'")
1378n/a
1379n/a # Test failure to locate included text file.
1380n/a document = ET.XML(XINCLUDE["C2.xml"])
1381n/a with self.assertRaises(ElementInclude.FatalIncludeError) as cm:
1382n/a ElementInclude.include(document, loader=self.none_loader)
1383n/a self.assertEqual(str(cm.exception),
1384n/a "cannot load 'count.txt' as 'text'")
1385n/a
1386n/a # Test bad parse type.
1387n/a document = ET.XML(XINCLUDE_BAD["B1.xml"])
1388n/a with self.assertRaises(ElementInclude.FatalIncludeError) as cm:
1389n/a ElementInclude.include(document, loader=self.none_loader)
1390n/a self.assertEqual(str(cm.exception),
1391n/a "unknown parse type in xi:include tag ('BAD_TYPE')")
1392n/a
1393n/a # Test xi:fallback outside xi:include.
1394n/a document = ET.XML(XINCLUDE_BAD["B2.xml"])
1395n/a with self.assertRaises(ElementInclude.FatalIncludeError) as cm:
1396n/a ElementInclude.include(document, loader=self.none_loader)
1397n/a self.assertEqual(str(cm.exception),
1398n/a "xi:fallback tag must be child of xi:include "
1399n/a "('{http://www.w3.org/2001/XInclude}fallback')")
1400n/a
1401n/a# --------------------------------------------------------------------
1402n/a# reported bugs
1403n/a
1404n/aclass BugsTest(unittest.TestCase):
1405n/a
1406n/a def test_bug_xmltoolkit21(self):
1407n/a # marshaller gives obscure errors for non-string values
1408n/a
1409n/a def check(elem):
1410n/a with self.assertRaises(TypeError) as cm:
1411n/a serialize(elem)
1412n/a self.assertEqual(str(cm.exception),
1413n/a 'cannot serialize 123 (type int)')
1414n/a
1415n/a elem = ET.Element(123)
1416n/a check(elem) # tag
1417n/a
1418n/a elem = ET.Element("elem")
1419n/a elem.text = 123
1420n/a check(elem) # text
1421n/a
1422n/a elem = ET.Element("elem")
1423n/a elem.tail = 123
1424n/a check(elem) # tail
1425n/a
1426n/a elem = ET.Element("elem")
1427n/a elem.set(123, "123")
1428n/a check(elem) # attribute key
1429n/a
1430n/a elem = ET.Element("elem")
1431n/a elem.set("123", 123)
1432n/a check(elem) # attribute value
1433n/a
1434n/a def test_bug_xmltoolkit25(self):
1435n/a # typo in ElementTree.findtext
1436n/a
1437n/a elem = ET.XML(SAMPLE_XML)
1438n/a tree = ET.ElementTree(elem)
1439n/a self.assertEqual(tree.findtext("tag"), 'text')
1440n/a self.assertEqual(tree.findtext("section/tag"), 'subtext')
1441n/a
1442n/a def test_bug_xmltoolkit28(self):
1443n/a # .//tag causes exceptions
1444n/a
1445n/a tree = ET.XML("<doc><table><tbody/></table></doc>")
1446n/a self.assertEqual(summarize_list(tree.findall(".//thead")), [])
1447n/a self.assertEqual(summarize_list(tree.findall(".//tbody")), ['tbody'])
1448n/a
1449n/a def test_bug_xmltoolkitX1(self):
1450n/a # dump() doesn't flush the output buffer
1451n/a
1452n/a tree = ET.XML("<doc><table><tbody/></table></doc>")
1453n/a with support.captured_stdout() as stdout:
1454n/a ET.dump(tree)
1455n/a self.assertEqual(stdout.getvalue(), '<doc><table><tbody /></table></doc>\n')
1456n/a
1457n/a def test_bug_xmltoolkit39(self):
1458n/a # non-ascii element and attribute names doesn't work
1459n/a
1460n/a tree = ET.XML(b"<?xml version='1.0' encoding='iso-8859-1'?><t\xe4g />")
1461n/a self.assertEqual(ET.tostring(tree, "utf-8"), b'<t\xc3\xa4g />')
1462n/a
1463n/a tree = ET.XML(b"<?xml version='1.0' encoding='iso-8859-1'?>"
1464n/a b"<tag \xe4ttr='v&#228;lue' />")
1465n/a self.assertEqual(tree.attrib, {'\xe4ttr': 'v\xe4lue'})
1466n/a self.assertEqual(ET.tostring(tree, "utf-8"),
1467n/a b'<tag \xc3\xa4ttr="v\xc3\xa4lue" />')
1468n/a
1469n/a tree = ET.XML(b"<?xml version='1.0' encoding='iso-8859-1'?>"
1470n/a b'<t\xe4g>text</t\xe4g>')
1471n/a self.assertEqual(ET.tostring(tree, "utf-8"),
1472n/a b'<t\xc3\xa4g>text</t\xc3\xa4g>')
1473n/a
1474n/a tree = ET.Element("t\u00e4g")
1475n/a self.assertEqual(ET.tostring(tree, "utf-8"), b'<t\xc3\xa4g />')
1476n/a
1477n/a tree = ET.Element("tag")
1478n/a tree.set("\u00e4ttr", "v\u00e4lue")
1479n/a self.assertEqual(ET.tostring(tree, "utf-8"),
1480n/a b'<tag \xc3\xa4ttr="v\xc3\xa4lue" />')
1481n/a
1482n/a def test_bug_xmltoolkit54(self):
1483n/a # problems handling internally defined entities
1484n/a
1485n/a e = ET.XML("<!DOCTYPE doc [<!ENTITY ldots '&#x8230;'>]>"
1486n/a '<doc>&ldots;</doc>')
1487n/a self.assertEqual(serialize(e, encoding="us-ascii"),
1488n/a b'<doc>&#33328;</doc>')
1489n/a self.assertEqual(serialize(e), '<doc>\u8230</doc>')
1490n/a
1491n/a def test_bug_xmltoolkit55(self):
1492n/a # make sure we're reporting the first error, not the last
1493n/a
1494n/a with self.assertRaises(ET.ParseError) as cm:
1495n/a ET.XML(b"<!DOCTYPE doc SYSTEM 'doc.dtd'>"
1496n/a b'<doc>&ldots;&ndots;&rdots;</doc>')
1497n/a self.assertEqual(str(cm.exception),
1498n/a 'undefined entity &ldots;: line 1, column 36')
1499n/a
1500n/a def test_bug_xmltoolkit60(self):
1501n/a # Handle crash in stream source.
1502n/a
1503n/a class ExceptionFile:
1504n/a def read(self, x):
1505n/a raise OSError
1506n/a
1507n/a self.assertRaises(OSError, ET.parse, ExceptionFile())
1508n/a
1509n/a def test_bug_xmltoolkit62(self):
1510n/a # Don't crash when using custom entities.
1511n/a
1512n/a ENTITIES = {'rsquo': '\u2019', 'lsquo': '\u2018'}
1513n/a parser = ET.XMLParser()
1514n/a parser.entity.update(ENTITIES)
1515n/a parser.feed("""<?xml version="1.0" encoding="UTF-8"?>
1516n/a<!DOCTYPE patent-application-publication SYSTEM "pap-v15-2001-01-31.dtd" []>
1517n/a<patent-application-publication>
1518n/a<subdoc-abstract>
1519n/a<paragraph id="A-0001" lvl="0">A new cultivar of Begonia plant named &lsquo;BCT9801BEG&rsquo;.</paragraph>
1520n/a</subdoc-abstract>
1521n/a</patent-application-publication>""")
1522n/a t = parser.close()
1523n/a self.assertEqual(t.find('.//paragraph').text,
1524n/a 'A new cultivar of Begonia plant named \u2018BCT9801BEG\u2019.')
1525n/a
1526n/a def test_bug_xmltoolkit63(self):
1527n/a # Check reference leak.
1528n/a def xmltoolkit63():
1529n/a tree = ET.TreeBuilder()
1530n/a tree.start("tag", {})
1531n/a tree.data("text")
1532n/a tree.end("tag")
1533n/a
1534n/a xmltoolkit63()
1535n/a count = sys.getrefcount(None)
1536n/a for i in range(1000):
1537n/a xmltoolkit63()
1538n/a self.assertEqual(sys.getrefcount(None), count)
1539n/a
1540n/a def test_bug_200708_newline(self):
1541n/a # Preserve newlines in attributes.
1542n/a
1543n/a e = ET.Element('SomeTag', text="def _f():\n return 3\n")
1544n/a self.assertEqual(ET.tostring(e),
1545n/a b'<SomeTag text="def _f():&#10; return 3&#10;" />')
1546n/a self.assertEqual(ET.XML(ET.tostring(e)).get("text"),
1547n/a 'def _f():\n return 3\n')
1548n/a self.assertEqual(ET.tostring(ET.XML(ET.tostring(e))),
1549n/a b'<SomeTag text="def _f():&#10; return 3&#10;" />')
1550n/a
1551n/a def test_bug_200708_close(self):
1552n/a # Test default builder.
1553n/a parser = ET.XMLParser() # default
1554n/a parser.feed("<element>some text</element>")
1555n/a self.assertEqual(parser.close().tag, 'element')
1556n/a
1557n/a # Test custom builder.
1558n/a class EchoTarget:
1559n/a def close(self):
1560n/a return ET.Element("element") # simulate root
1561n/a parser = ET.XMLParser(EchoTarget())
1562n/a parser.feed("<element>some text</element>")
1563n/a self.assertEqual(parser.close().tag, 'element')
1564n/a
1565n/a def test_bug_200709_default_namespace(self):
1566n/a e = ET.Element("{default}elem")
1567n/a s = ET.SubElement(e, "{default}elem")
1568n/a self.assertEqual(serialize(e, default_namespace="default"), # 1
1569n/a '<elem xmlns="default"><elem /></elem>')
1570n/a
1571n/a e = ET.Element("{default}elem")
1572n/a s = ET.SubElement(e, "{default}elem")
1573n/a s = ET.SubElement(e, "{not-default}elem")
1574n/a self.assertEqual(serialize(e, default_namespace="default"), # 2
1575n/a '<elem xmlns="default" xmlns:ns1="not-default">'
1576n/a '<elem />'
1577n/a '<ns1:elem />'
1578n/a '</elem>')
1579n/a
1580n/a e = ET.Element("{default}elem")
1581n/a s = ET.SubElement(e, "{default}elem")
1582n/a s = ET.SubElement(e, "elem") # unprefixed name
1583n/a with self.assertRaises(ValueError) as cm:
1584n/a serialize(e, default_namespace="default") # 3
1585n/a self.assertEqual(str(cm.exception),
1586n/a 'cannot use non-qualified names with default_namespace option')
1587n/a
1588n/a def test_bug_200709_register_namespace(self):
1589n/a e = ET.Element("{http://namespace.invalid/does/not/exist/}title")
1590n/a self.assertEqual(ET.tostring(e),
1591n/a b'<ns0:title xmlns:ns0="http://namespace.invalid/does/not/exist/" />')
1592n/a ET.register_namespace("foo", "http://namespace.invalid/does/not/exist/")
1593n/a e = ET.Element("{http://namespace.invalid/does/not/exist/}title")
1594n/a self.assertEqual(ET.tostring(e),
1595n/a b'<foo:title xmlns:foo="http://namespace.invalid/does/not/exist/" />')
1596n/a
1597n/a # And the Dublin Core namespace is in the default list:
1598n/a
1599n/a e = ET.Element("{http://purl.org/dc/elements/1.1/}title")
1600n/a self.assertEqual(ET.tostring(e),
1601n/a b'<dc:title xmlns:dc="http://purl.org/dc/elements/1.1/" />')
1602n/a
1603n/a def test_bug_200709_element_comment(self):
1604n/a # Not sure if this can be fixed, really (since the serializer needs
1605n/a # ET.Comment, not cET.comment).
1606n/a
1607n/a a = ET.Element('a')
1608n/a a.append(ET.Comment('foo'))
1609n/a self.assertEqual(a[0].tag, ET.Comment)
1610n/a
1611n/a a = ET.Element('a')
1612n/a a.append(ET.PI('foo'))
1613n/a self.assertEqual(a[0].tag, ET.PI)
1614n/a
1615n/a def test_bug_200709_element_insert(self):
1616n/a a = ET.Element('a')
1617n/a b = ET.SubElement(a, 'b')
1618n/a c = ET.SubElement(a, 'c')
1619n/a d = ET.Element('d')
1620n/a a.insert(0, d)
1621n/a self.assertEqual(summarize_list(a), ['d', 'b', 'c'])
1622n/a a.insert(-1, d)
1623n/a self.assertEqual(summarize_list(a), ['d', 'b', 'd', 'c'])
1624n/a
1625n/a def test_bug_200709_iter_comment(self):
1626n/a a = ET.Element('a')
1627n/a b = ET.SubElement(a, 'b')
1628n/a comment_b = ET.Comment("TEST-b")
1629n/a b.append(comment_b)
1630n/a self.assertEqual(summarize_list(a.iter(ET.Comment)), [ET.Comment])
1631n/a
1632n/a # --------------------------------------------------------------------
1633n/a # reported on bugs.python.org
1634n/a
1635n/a def test_bug_1534630(self):
1636n/a bob = ET.TreeBuilder()
1637n/a e = bob.data("data")
1638n/a e = bob.start("tag", {})
1639n/a e = bob.end("tag")
1640n/a e = bob.close()
1641n/a self.assertEqual(serialize(e), '<tag />')
1642n/a
1643n/a def test_issue6233(self):
1644n/a e = ET.XML(b"<?xml version='1.0' encoding='utf-8'?>"
1645n/a b'<body>t\xc3\xa3g</body>')
1646n/a self.assertEqual(ET.tostring(e, 'ascii'),
1647n/a b"<?xml version='1.0' encoding='ascii'?>\n"
1648n/a b'<body>t&#227;g</body>')
1649n/a e = ET.XML(b"<?xml version='1.0' encoding='iso-8859-1'?>"
1650n/a b'<body>t\xe3g</body>')
1651n/a self.assertEqual(ET.tostring(e, 'ascii'),
1652n/a b"<?xml version='1.0' encoding='ascii'?>\n"
1653n/a b'<body>t&#227;g</body>')
1654n/a
1655n/a def test_issue3151(self):
1656n/a e = ET.XML('<prefix:localname xmlns:prefix="${stuff}"/>')
1657n/a self.assertEqual(e.tag, '{${stuff}}localname')
1658n/a t = ET.ElementTree(e)
1659n/a self.assertEqual(ET.tostring(e), b'<ns0:localname xmlns:ns0="${stuff}" />')
1660n/a
1661n/a def test_issue6565(self):
1662n/a elem = ET.XML("<body><tag/></body>")
1663n/a self.assertEqual(summarize_list(elem), ['tag'])
1664n/a newelem = ET.XML(SAMPLE_XML)
1665n/a elem[:] = newelem[:]
1666n/a self.assertEqual(summarize_list(elem), ['tag', 'tag', 'section'])
1667n/a
1668n/a def test_issue10777(self):
1669n/a # Registering a namespace twice caused a "dictionary changed size during
1670n/a # iteration" bug.
1671n/a
1672n/a ET.register_namespace('test10777', 'http://myuri/')
1673n/a ET.register_namespace('test10777', 'http://myuri/')
1674n/a
1675n/a def test_lost_text(self):
1676n/a # Issue #25902: Borrowed text can disappear
1677n/a class Text:
1678n/a def __bool__(self):
1679n/a e.text = 'changed'
1680n/a return True
1681n/a
1682n/a e = ET.Element('tag')
1683n/a e.text = Text()
1684n/a i = e.itertext()
1685n/a t = next(i)
1686n/a self.assertIsInstance(t, Text)
1687n/a self.assertIsInstance(e.text, str)
1688n/a self.assertEqual(e.text, 'changed')
1689n/a
1690n/a def test_lost_tail(self):
1691n/a # Issue #25902: Borrowed tail can disappear
1692n/a class Text:
1693n/a def __bool__(self):
1694n/a e[0].tail = 'changed'
1695n/a return True
1696n/a
1697n/a e = ET.Element('root')
1698n/a e.append(ET.Element('tag'))
1699n/a e[0].tail = Text()
1700n/a i = e.itertext()
1701n/a t = next(i)
1702n/a self.assertIsInstance(t, Text)
1703n/a self.assertIsInstance(e[0].tail, str)
1704n/a self.assertEqual(e[0].tail, 'changed')
1705n/a
1706n/a def test_lost_elem(self):
1707n/a # Issue #25902: Borrowed element can disappear
1708n/a class Tag:
1709n/a def __eq__(self, other):
1710n/a e[0] = ET.Element('changed')
1711n/a next(i)
1712n/a return True
1713n/a
1714n/a e = ET.Element('root')
1715n/a e.append(ET.Element(Tag()))
1716n/a e.append(ET.Element('tag'))
1717n/a i = e.iter('tag')
1718n/a try:
1719n/a t = next(i)
1720n/a except ValueError:
1721n/a self.skipTest('generators are not reentrant')
1722n/a self.assertIsInstance(t.tag, Tag)
1723n/a self.assertIsInstance(e[0].tag, str)
1724n/a self.assertEqual(e[0].tag, 'changed')
1725n/a
1726n/a
1727n/a# --------------------------------------------------------------------
1728n/a
1729n/a
1730n/aclass BasicElementTest(ElementTestCase, unittest.TestCase):
1731n/a def test_augmentation_type_errors(self):
1732n/a e = ET.Element('joe')
1733n/a self.assertRaises(TypeError, e.append, 'b')
1734n/a self.assertRaises(TypeError, e.extend, [ET.Element('bar'), 'foo'])
1735n/a self.assertRaises(TypeError, e.insert, 0, 'foo')
1736n/a
1737n/a def test_cyclic_gc(self):
1738n/a class Dummy:
1739n/a pass
1740n/a
1741n/a # Test the shortest cycle: d->element->d
1742n/a d = Dummy()
1743n/a d.dummyref = ET.Element('joe', attr=d)
1744n/a wref = weakref.ref(d)
1745n/a del d
1746n/a gc_collect()
1747n/a self.assertIsNone(wref())
1748n/a
1749n/a # A longer cycle: d->e->e2->d
1750n/a e = ET.Element('joe')
1751n/a d = Dummy()
1752n/a d.dummyref = e
1753n/a wref = weakref.ref(d)
1754n/a e2 = ET.SubElement(e, 'foo', attr=d)
1755n/a del d, e, e2
1756n/a gc_collect()
1757n/a self.assertIsNone(wref())
1758n/a
1759n/a # A cycle between Element objects as children of one another
1760n/a # e1->e2->e3->e1
1761n/a e1 = ET.Element('e1')
1762n/a e2 = ET.Element('e2')
1763n/a e3 = ET.Element('e3')
1764n/a e1.append(e2)
1765n/a e2.append(e2)
1766n/a e3.append(e1)
1767n/a wref = weakref.ref(e1)
1768n/a del e1, e2, e3
1769n/a gc_collect()
1770n/a self.assertIsNone(wref())
1771n/a
1772n/a def test_weakref(self):
1773n/a flag = False
1774n/a def wref_cb(w):
1775n/a nonlocal flag
1776n/a flag = True
1777n/a e = ET.Element('e')
1778n/a wref = weakref.ref(e, wref_cb)
1779n/a self.assertEqual(wref().tag, 'e')
1780n/a del e
1781n/a self.assertEqual(flag, True)
1782n/a self.assertEqual(wref(), None)
1783n/a
1784n/a def test_get_keyword_args(self):
1785n/a e1 = ET.Element('foo' , x=1, y=2, z=3)
1786n/a self.assertEqual(e1.get('x', default=7), 1)
1787n/a self.assertEqual(e1.get('w', default=7), 7)
1788n/a
1789n/a def test_pickle(self):
1790n/a # issue #16076: the C implementation wasn't pickleable.
1791n/a for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
1792n/a for dumper, loader in product(self.modules, repeat=2):
1793n/a e = dumper.Element('foo', bar=42)
1794n/a e.text = "text goes here"
1795n/a e.tail = "opposite of head"
1796n/a dumper.SubElement(e, 'child').append(dumper.Element('grandchild'))
1797n/a e.append(dumper.Element('child'))
1798n/a e.findall('.//grandchild')[0].set('attr', 'other value')
1799n/a
1800n/a e2 = self.pickleRoundTrip(e, 'xml.etree.ElementTree',
1801n/a dumper, loader, proto)
1802n/a
1803n/a self.assertEqual(e2.tag, 'foo')
1804n/a self.assertEqual(e2.attrib['bar'], 42)
1805n/a self.assertEqual(len(e2), 2)
1806n/a self.assertEqualElements(e, e2)
1807n/a
1808n/a def test_pickle_issue18997(self):
1809n/a for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
1810n/a for dumper, loader in product(self.modules, repeat=2):
1811n/a XMLTEXT = """<?xml version="1.0"?>
1812n/a <group><dogs>4</dogs>
1813n/a </group>"""
1814n/a e1 = dumper.fromstring(XMLTEXT)
1815n/a if hasattr(e1, '__getstate__'):
1816n/a self.assertEqual(e1.__getstate__()['tag'], 'group')
1817n/a e2 = self.pickleRoundTrip(e1, 'xml.etree.ElementTree',
1818n/a dumper, loader, proto)
1819n/a self.assertEqual(e2.tag, 'group')
1820n/a self.assertEqual(e2[0].tag, 'dogs')
1821n/a
1822n/a
1823n/aclass BadElementTest(ElementTestCase, unittest.TestCase):
1824n/a def test_extend_mutable_list(self):
1825n/a class X:
1826n/a @property
1827n/a def __class__(self):
1828n/a L[:] = [ET.Element('baz')]
1829n/a return ET.Element
1830n/a L = [X()]
1831n/a e = ET.Element('foo')
1832n/a try:
1833n/a e.extend(L)
1834n/a except TypeError:
1835n/a pass
1836n/a
1837n/a class Y(X, ET.Element):
1838n/a pass
1839n/a L = [Y('x')]
1840n/a e = ET.Element('foo')
1841n/a e.extend(L)
1842n/a
1843n/a def test_extend_mutable_list2(self):
1844n/a class X:
1845n/a @property
1846n/a def __class__(self):
1847n/a del L[:]
1848n/a return ET.Element
1849n/a L = [X(), ET.Element('baz')]
1850n/a e = ET.Element('foo')
1851n/a try:
1852n/a e.extend(L)
1853n/a except TypeError:
1854n/a pass
1855n/a
1856n/a class Y(X, ET.Element):
1857n/a pass
1858n/a L = [Y('bar'), ET.Element('baz')]
1859n/a e = ET.Element('foo')
1860n/a e.extend(L)
1861n/a
1862n/a def test_remove_with_mutating(self):
1863n/a class X(ET.Element):
1864n/a def __eq__(self, o):
1865n/a del e[:]
1866n/a return False
1867n/a e = ET.Element('foo')
1868n/a e.extend([X('bar')])
1869n/a self.assertRaises(ValueError, e.remove, ET.Element('baz'))
1870n/a
1871n/a e = ET.Element('foo')
1872n/a e.extend([ET.Element('bar')])
1873n/a self.assertRaises(ValueError, e.remove, X('baz'))
1874n/a
1875n/a def test_recursive_repr(self):
1876n/a # Issue #25455
1877n/a e = ET.Element('foo')
1878n/a with swap_attr(e, 'tag', e):
1879n/a with self.assertRaises(RuntimeError):
1880n/a repr(e) # Should not crash
1881n/a
1882n/aclass MutatingElementPath(str):
1883n/a def __new__(cls, elem, *args):
1884n/a self = str.__new__(cls, *args)
1885n/a self.elem = elem
1886n/a return self
1887n/a def __eq__(self, o):
1888n/a del self.elem[:]
1889n/a return True
1890n/aMutatingElementPath.__hash__ = str.__hash__
1891n/a
1892n/aclass BadElementPath(str):
1893n/a def __eq__(self, o):
1894n/a raise 1/0
1895n/aBadElementPath.__hash__ = str.__hash__
1896n/a
1897n/aclass BadElementPathTest(ElementTestCase, unittest.TestCase):
1898n/a def setUp(self):
1899n/a super().setUp()
1900n/a from xml.etree import ElementPath
1901n/a self.path_cache = ElementPath._cache
1902n/a ElementPath._cache = {}
1903n/a
1904n/a def tearDown(self):
1905n/a from xml.etree import ElementPath
1906n/a ElementPath._cache = self.path_cache
1907n/a super().tearDown()
1908n/a
1909n/a def test_find_with_mutating(self):
1910n/a e = ET.Element('foo')
1911n/a e.extend([ET.Element('bar')])
1912n/a e.find(MutatingElementPath(e, 'x'))
1913n/a
1914n/a def test_find_with_error(self):
1915n/a e = ET.Element('foo')
1916n/a e.extend([ET.Element('bar')])
1917n/a try:
1918n/a e.find(BadElementPath('x'))
1919n/a except ZeroDivisionError:
1920n/a pass
1921n/a
1922n/a def test_findtext_with_mutating(self):
1923n/a e = ET.Element('foo')
1924n/a e.extend([ET.Element('bar')])
1925n/a e.findtext(MutatingElementPath(e, 'x'))
1926n/a
1927n/a def test_findtext_with_error(self):
1928n/a e = ET.Element('foo')
1929n/a e.extend([ET.Element('bar')])
1930n/a try:
1931n/a e.findtext(BadElementPath('x'))
1932n/a except ZeroDivisionError:
1933n/a pass
1934n/a
1935n/a def test_findall_with_mutating(self):
1936n/a e = ET.Element('foo')
1937n/a e.extend([ET.Element('bar')])
1938n/a e.findall(MutatingElementPath(e, 'x'))
1939n/a
1940n/a def test_findall_with_error(self):
1941n/a e = ET.Element('foo')
1942n/a e.extend([ET.Element('bar')])
1943n/a try:
1944n/a e.findall(BadElementPath('x'))
1945n/a except ZeroDivisionError:
1946n/a pass
1947n/a
1948n/a
1949n/aclass ElementTreeTypeTest(unittest.TestCase):
1950n/a def test_istype(self):
1951n/a self.assertIsInstance(ET.ParseError, type)
1952n/a self.assertIsInstance(ET.QName, type)
1953n/a self.assertIsInstance(ET.ElementTree, type)
1954n/a self.assertIsInstance(ET.Element, type)
1955n/a self.assertIsInstance(ET.TreeBuilder, type)
1956n/a self.assertIsInstance(ET.XMLParser, type)
1957n/a
1958n/a def test_Element_subclass_trivial(self):
1959n/a class MyElement(ET.Element):
1960n/a pass
1961n/a
1962n/a mye = MyElement('foo')
1963n/a self.assertIsInstance(mye, ET.Element)
1964n/a self.assertIsInstance(mye, MyElement)
1965n/a self.assertEqual(mye.tag, 'foo')
1966n/a
1967n/a # test that attribute assignment works (issue 14849)
1968n/a mye.text = "joe"
1969n/a self.assertEqual(mye.text, "joe")
1970n/a
1971n/a def test_Element_subclass_constructor(self):
1972n/a class MyElement(ET.Element):
1973n/a def __init__(self, tag, attrib={}, **extra):
1974n/a super(MyElement, self).__init__(tag + '__', attrib, **extra)
1975n/a
1976n/a mye = MyElement('foo', {'a': 1, 'b': 2}, c=3, d=4)
1977n/a self.assertEqual(mye.tag, 'foo__')
1978n/a self.assertEqual(sorted(mye.items()),
1979n/a [('a', 1), ('b', 2), ('c', 3), ('d', 4)])
1980n/a
1981n/a def test_Element_subclass_new_method(self):
1982n/a class MyElement(ET.Element):
1983n/a def newmethod(self):
1984n/a return self.tag
1985n/a
1986n/a mye = MyElement('joe')
1987n/a self.assertEqual(mye.newmethod(), 'joe')
1988n/a
1989n/a
1990n/aclass ElementFindTest(unittest.TestCase):
1991n/a def test_find_simple(self):
1992n/a e = ET.XML(SAMPLE_XML)
1993n/a self.assertEqual(e.find('tag').tag, 'tag')
1994n/a self.assertEqual(e.find('section/tag').tag, 'tag')
1995n/a self.assertEqual(e.find('./tag').tag, 'tag')
1996n/a
1997n/a e[2] = ET.XML(SAMPLE_SECTION)
1998n/a self.assertEqual(e.find('section/nexttag').tag, 'nexttag')
1999n/a
2000n/a self.assertEqual(e.findtext('./tag'), 'text')
2001n/a self.assertEqual(e.findtext('section/tag'), 'subtext')
2002n/a
2003n/a # section/nexttag is found but has no text
2004n/a self.assertEqual(e.findtext('section/nexttag'), '')
2005n/a self.assertEqual(e.findtext('section/nexttag', 'default'), '')
2006n/a
2007n/a # tog doesn't exist and 'default' kicks in
2008n/a self.assertIsNone(e.findtext('tog'))
2009n/a self.assertEqual(e.findtext('tog', 'default'), 'default')
2010n/a
2011n/a # Issue #16922
2012n/a self.assertEqual(ET.XML('<tag><empty /></tag>').findtext('empty'), '')
2013n/a
2014n/a def test_find_xpath(self):
2015n/a LINEAR_XML = '''
2016n/a <body>
2017n/a <tag class='a'/>
2018n/a <tag class='b'/>
2019n/a <tag class='c'/>
2020n/a <tag class='d'/>
2021n/a </body>'''
2022n/a e = ET.XML(LINEAR_XML)
2023n/a
2024n/a # Test for numeric indexing and last()
2025n/a self.assertEqual(e.find('./tag[1]').attrib['class'], 'a')
2026n/a self.assertEqual(e.find('./tag[2]').attrib['class'], 'b')
2027n/a self.assertEqual(e.find('./tag[last()]').attrib['class'], 'd')
2028n/a self.assertEqual(e.find('./tag[last()-1]').attrib['class'], 'c')
2029n/a self.assertEqual(e.find('./tag[last()-2]').attrib['class'], 'b')
2030n/a
2031n/a self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[0]')
2032n/a self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[-1]')
2033n/a self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[last()-0]')
2034n/a self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[last()+1]')
2035n/a
2036n/a def test_findall(self):
2037n/a e = ET.XML(SAMPLE_XML)
2038n/a e[2] = ET.XML(SAMPLE_SECTION)
2039n/a self.assertEqual(summarize_list(e.findall('.')), ['body'])
2040n/a self.assertEqual(summarize_list(e.findall('tag')), ['tag', 'tag'])
2041n/a self.assertEqual(summarize_list(e.findall('tog')), [])
2042n/a self.assertEqual(summarize_list(e.findall('tog/foo')), [])
2043n/a self.assertEqual(summarize_list(e.findall('*')),
2044n/a ['tag', 'tag', 'section'])
2045n/a self.assertEqual(summarize_list(e.findall('.//tag')),
2046n/a ['tag'] * 4)
2047n/a self.assertEqual(summarize_list(e.findall('section/tag')), ['tag'])
2048n/a self.assertEqual(summarize_list(e.findall('section//tag')), ['tag'] * 2)
2049n/a self.assertEqual(summarize_list(e.findall('section/*')),
2050n/a ['tag', 'nexttag', 'nextsection'])
2051n/a self.assertEqual(summarize_list(e.findall('section//*')),
2052n/a ['tag', 'nexttag', 'nextsection', 'tag'])
2053n/a self.assertEqual(summarize_list(e.findall('section/.//*')),
2054n/a ['tag', 'nexttag', 'nextsection', 'tag'])
2055n/a self.assertEqual(summarize_list(e.findall('*/*')),
2056n/a ['tag', 'nexttag', 'nextsection'])
2057n/a self.assertEqual(summarize_list(e.findall('*//*')),
2058n/a ['tag', 'nexttag', 'nextsection', 'tag'])
2059n/a self.assertEqual(summarize_list(e.findall('*/tag')), ['tag'])
2060n/a self.assertEqual(summarize_list(e.findall('*/./tag')), ['tag'])
2061n/a self.assertEqual(summarize_list(e.findall('./tag')), ['tag'] * 2)
2062n/a self.assertEqual(summarize_list(e.findall('././tag')), ['tag'] * 2)
2063n/a
2064n/a self.assertEqual(summarize_list(e.findall('.//tag[@class]')),
2065n/a ['tag'] * 3)
2066n/a self.assertEqual(summarize_list(e.findall('.//tag[@class="a"]')),
2067n/a ['tag'])
2068n/a self.assertEqual(summarize_list(e.findall('.//tag[@class="b"]')),
2069n/a ['tag'] * 2)
2070n/a self.assertEqual(summarize_list(e.findall('.//tag[@id]')),
2071n/a ['tag'])
2072n/a self.assertEqual(summarize_list(e.findall('.//section[tag]')),
2073n/a ['section'])
2074n/a self.assertEqual(summarize_list(e.findall('.//section[element]')), [])
2075n/a self.assertEqual(summarize_list(e.findall('../tag')), [])
2076n/a self.assertEqual(summarize_list(e.findall('section/../tag')),
2077n/a ['tag'] * 2)
2078n/a self.assertEqual(e.findall('section//'), e.findall('section//*'))
2079n/a
2080n/a def test_test_find_with_ns(self):
2081n/a e = ET.XML(SAMPLE_XML_NS)
2082n/a self.assertEqual(summarize_list(e.findall('tag')), [])
2083n/a self.assertEqual(
2084n/a summarize_list(e.findall("{http://effbot.org/ns}tag")),
2085n/a ['{http://effbot.org/ns}tag'] * 2)
2086n/a self.assertEqual(
2087n/a summarize_list(e.findall(".//{http://effbot.org/ns}tag")),
2088n/a ['{http://effbot.org/ns}tag'] * 3)
2089n/a
2090n/a def test_findall_different_nsmaps(self):
2091n/a root = ET.XML('''
2092n/a <a xmlns:x="X" xmlns:y="Y">
2093n/a <x:b><c/></x:b>
2094n/a <b/>
2095n/a <c><x:b/><b/></c><y:b/>
2096n/a </a>''')
2097n/a nsmap = {'xx': 'X'}
2098n/a self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 2)
2099n/a self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 2)
2100n/a nsmap = {'xx': 'Y'}
2101n/a self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 1)
2102n/a self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 2)
2103n/a
2104n/a def test_bad_find(self):
2105n/a e = ET.XML(SAMPLE_XML)
2106n/a with self.assertRaisesRegex(SyntaxError, 'cannot use absolute path'):
2107n/a e.findall('/tag')
2108n/a
2109n/a def test_find_through_ElementTree(self):
2110n/a e = ET.XML(SAMPLE_XML)
2111n/a self.assertEqual(ET.ElementTree(e).find('tag').tag, 'tag')
2112n/a self.assertEqual(ET.ElementTree(e).findtext('tag'), 'text')
2113n/a self.assertEqual(summarize_list(ET.ElementTree(e).findall('tag')),
2114n/a ['tag'] * 2)
2115n/a # this produces a warning
2116n/a self.assertEqual(summarize_list(ET.ElementTree(e).findall('//tag')),
2117n/a ['tag'] * 3)
2118n/a
2119n/a
2120n/aclass ElementIterTest(unittest.TestCase):
2121n/a def _ilist(self, elem, tag=None):
2122n/a return summarize_list(elem.iter(tag))
2123n/a
2124n/a def test_basic(self):
2125n/a doc = ET.XML("<html><body>this is a <i>paragraph</i>.</body>..</html>")
2126n/a self.assertEqual(self._ilist(doc), ['html', 'body', 'i'])
2127n/a self.assertEqual(self._ilist(doc.find('body')), ['body', 'i'])
2128n/a self.assertEqual(next(doc.iter()).tag, 'html')
2129n/a self.assertEqual(''.join(doc.itertext()), 'this is a paragraph...')
2130n/a self.assertEqual(''.join(doc.find('body').itertext()),
2131n/a 'this is a paragraph.')
2132n/a self.assertEqual(next(doc.itertext()), 'this is a ')
2133n/a
2134n/a # iterparse should return an iterator
2135n/a sourcefile = serialize(doc, to_string=False)
2136n/a self.assertEqual(next(ET.iterparse(sourcefile))[0], 'end')
2137n/a
2138n/a # With an explitit parser too (issue #9708)
2139n/a sourcefile = serialize(doc, to_string=False)
2140n/a parser = ET.XMLParser(target=ET.TreeBuilder())
2141n/a self.assertEqual(next(ET.iterparse(sourcefile, parser=parser))[0],
2142n/a 'end')
2143n/a
2144n/a tree = ET.ElementTree(None)
2145n/a self.assertRaises(AttributeError, tree.iter)
2146n/a
2147n/a # Issue #16913
2148n/a doc = ET.XML("<root>a&amp;<sub>b&amp;</sub>c&amp;</root>")
2149n/a self.assertEqual(''.join(doc.itertext()), 'a&b&c&')
2150n/a
2151n/a def test_corners(self):
2152n/a # single root, no subelements
2153n/a a = ET.Element('a')
2154n/a self.assertEqual(self._ilist(a), ['a'])
2155n/a
2156n/a # one child
2157n/a b = ET.SubElement(a, 'b')
2158n/a self.assertEqual(self._ilist(a), ['a', 'b'])
2159n/a
2160n/a # one child and one grandchild
2161n/a c = ET.SubElement(b, 'c')
2162n/a self.assertEqual(self._ilist(a), ['a', 'b', 'c'])
2163n/a
2164n/a # two children, only first with grandchild
2165n/a d = ET.SubElement(a, 'd')
2166n/a self.assertEqual(self._ilist(a), ['a', 'b', 'c', 'd'])
2167n/a
2168n/a # replace first child by second
2169n/a a[0] = a[1]
2170n/a del a[1]
2171n/a self.assertEqual(self._ilist(a), ['a', 'd'])
2172n/a
2173n/a def test_iter_by_tag(self):
2174n/a doc = ET.XML('''
2175n/a <document>
2176n/a <house>
2177n/a <room>bedroom1</room>
2178n/a <room>bedroom2</room>
2179n/a </house>
2180n/a <shed>nothing here
2181n/a </shed>
2182n/a <house>
2183n/a <room>bedroom8</room>
2184n/a </house>
2185n/a </document>''')
2186n/a
2187n/a self.assertEqual(self._ilist(doc, 'room'), ['room'] * 3)
2188n/a self.assertEqual(self._ilist(doc, 'house'), ['house'] * 2)
2189n/a
2190n/a # test that iter also accepts 'tag' as a keyword arg
2191n/a self.assertEqual(
2192n/a summarize_list(doc.iter(tag='room')),
2193n/a ['room'] * 3)
2194n/a
2195n/a # make sure both tag=None and tag='*' return all tags
2196n/a all_tags = ['document', 'house', 'room', 'room',
2197n/a 'shed', 'house', 'room']
2198n/a self.assertEqual(summarize_list(doc.iter()), all_tags)
2199n/a self.assertEqual(self._ilist(doc), all_tags)
2200n/a self.assertEqual(self._ilist(doc, '*'), all_tags)
2201n/a
2202n/a def test_getiterator(self):
2203n/a doc = ET.XML('''
2204n/a <document>
2205n/a <house>
2206n/a <room>bedroom1</room>
2207n/a <room>bedroom2</room>
2208n/a </house>
2209n/a <shed>nothing here
2210n/a </shed>
2211n/a <house>
2212n/a <room>bedroom8</room>
2213n/a </house>
2214n/a </document>''')
2215n/a
2216n/a self.assertEqual(summarize_list(doc.getiterator('room')),
2217n/a ['room'] * 3)
2218n/a self.assertEqual(summarize_list(doc.getiterator('house')),
2219n/a ['house'] * 2)
2220n/a
2221n/a # test that getiterator also accepts 'tag' as a keyword arg
2222n/a self.assertEqual(
2223n/a summarize_list(doc.getiterator(tag='room')),
2224n/a ['room'] * 3)
2225n/a
2226n/a # make sure both tag=None and tag='*' return all tags
2227n/a all_tags = ['document', 'house', 'room', 'room',
2228n/a 'shed', 'house', 'room']
2229n/a self.assertEqual(summarize_list(doc.getiterator()), all_tags)
2230n/a self.assertEqual(summarize_list(doc.getiterator(None)), all_tags)
2231n/a self.assertEqual(summarize_list(doc.getiterator('*')), all_tags)
2232n/a
2233n/a def test_copy(self):
2234n/a a = ET.Element('a')
2235n/a it = a.iter()
2236n/a with self.assertRaises(TypeError):
2237n/a copy.copy(it)
2238n/a
2239n/a def test_pickle(self):
2240n/a a = ET.Element('a')
2241n/a it = a.iter()
2242n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
2243n/a with self.assertRaises((TypeError, pickle.PicklingError)):
2244n/a pickle.dumps(it, proto)
2245n/a
2246n/a
2247n/aclass TreeBuilderTest(unittest.TestCase):
2248n/a sample1 = ('<!DOCTYPE html PUBLIC'
2249n/a ' "-//W3C//DTD XHTML 1.0 Transitional//EN"'
2250n/a ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
2251n/a '<html>text<div>subtext</div>tail</html>')
2252n/a
2253n/a sample2 = '''<toplevel>sometext</toplevel>'''
2254n/a
2255n/a def _check_sample1_element(self, e):
2256n/a self.assertEqual(e.tag, 'html')
2257n/a self.assertEqual(e.text, 'text')
2258n/a self.assertEqual(e.tail, None)
2259n/a self.assertEqual(e.attrib, {})
2260n/a children = list(e)
2261n/a self.assertEqual(len(children), 1)
2262n/a child = children[0]
2263n/a self.assertEqual(child.tag, 'div')
2264n/a self.assertEqual(child.text, 'subtext')
2265n/a self.assertEqual(child.tail, 'tail')
2266n/a self.assertEqual(child.attrib, {})
2267n/a
2268n/a def test_dummy_builder(self):
2269n/a class BaseDummyBuilder:
2270n/a def close(self):
2271n/a return 42
2272n/a
2273n/a class DummyBuilder(BaseDummyBuilder):
2274n/a data = start = end = lambda *a: None
2275n/a
2276n/a parser = ET.XMLParser(target=DummyBuilder())
2277n/a parser.feed(self.sample1)
2278n/a self.assertEqual(parser.close(), 42)
2279n/a
2280n/a parser = ET.XMLParser(target=BaseDummyBuilder())
2281n/a parser.feed(self.sample1)
2282n/a self.assertEqual(parser.close(), 42)
2283n/a
2284n/a parser = ET.XMLParser(target=object())
2285n/a parser.feed(self.sample1)
2286n/a self.assertIsNone(parser.close())
2287n/a
2288n/a def test_treebuilder_elementfactory_none(self):
2289n/a parser = ET.XMLParser(target=ET.TreeBuilder(element_factory=None))
2290n/a parser.feed(self.sample1)
2291n/a e = parser.close()
2292n/a self._check_sample1_element(e)
2293n/a
2294n/a def test_subclass(self):
2295n/a class MyTreeBuilder(ET.TreeBuilder):
2296n/a def foobar(self, x):
2297n/a return x * 2
2298n/a
2299n/a tb = MyTreeBuilder()
2300n/a self.assertEqual(tb.foobar(10), 20)
2301n/a
2302n/a parser = ET.XMLParser(target=tb)
2303n/a parser.feed(self.sample1)
2304n/a
2305n/a e = parser.close()
2306n/a self._check_sample1_element(e)
2307n/a
2308n/a def test_element_factory(self):
2309n/a lst = []
2310n/a def myfactory(tag, attrib):
2311n/a nonlocal lst
2312n/a lst.append(tag)
2313n/a return ET.Element(tag, attrib)
2314n/a
2315n/a tb = ET.TreeBuilder(element_factory=myfactory)
2316n/a parser = ET.XMLParser(target=tb)
2317n/a parser.feed(self.sample2)
2318n/a parser.close()
2319n/a
2320n/a self.assertEqual(lst, ['toplevel'])
2321n/a
2322n/a def _check_element_factory_class(self, cls):
2323n/a tb = ET.TreeBuilder(element_factory=cls)
2324n/a
2325n/a parser = ET.XMLParser(target=tb)
2326n/a parser.feed(self.sample1)
2327n/a e = parser.close()
2328n/a self.assertIsInstance(e, cls)
2329n/a self._check_sample1_element(e)
2330n/a
2331n/a def test_element_factory_subclass(self):
2332n/a class MyElement(ET.Element):
2333n/a pass
2334n/a self._check_element_factory_class(MyElement)
2335n/a
2336n/a def test_element_factory_pure_python_subclass(self):
2337n/a # Mimick SimpleTAL's behaviour (issue #16089): both versions of
2338n/a # TreeBuilder should be able to cope with a subclass of the
2339n/a # pure Python Element class.
2340n/a base = ET._Element_Py
2341n/a # Not from a C extension
2342n/a self.assertEqual(base.__module__, 'xml.etree.ElementTree')
2343n/a # Force some multiple inheritance with a C class to make things
2344n/a # more interesting.
2345n/a class MyElement(base, ValueError):
2346n/a pass
2347n/a self._check_element_factory_class(MyElement)
2348n/a
2349n/a def test_doctype(self):
2350n/a class DoctypeParser:
2351n/a _doctype = None
2352n/a
2353n/a def doctype(self, name, pubid, system):
2354n/a self._doctype = (name, pubid, system)
2355n/a
2356n/a def close(self):
2357n/a return self._doctype
2358n/a
2359n/a parser = ET.XMLParser(target=DoctypeParser())
2360n/a parser.feed(self.sample1)
2361n/a
2362n/a self.assertEqual(parser.close(),
2363n/a ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
2364n/a 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'))
2365n/a
2366n/a
2367n/aclass XMLParserTest(unittest.TestCase):
2368n/a sample1 = b'<file><line>22</line></file>'
2369n/a sample2 = (b'<!DOCTYPE html PUBLIC'
2370n/a b' "-//W3C//DTD XHTML 1.0 Transitional//EN"'
2371n/a b' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
2372n/a b'<html>text</html>')
2373n/a sample3 = ('<?xml version="1.0" encoding="iso-8859-1"?>\n'
2374n/a '<money value="$\xa3\u20ac\U0001017b">$\xa3\u20ac\U0001017b</money>')
2375n/a
2376n/a def _check_sample_element(self, e):
2377n/a self.assertEqual(e.tag, 'file')
2378n/a self.assertEqual(e[0].tag, 'line')
2379n/a self.assertEqual(e[0].text, '22')
2380n/a
2381n/a def test_constructor_args(self):
2382n/a # Positional args. The first (html) is not supported, but should be
2383n/a # nevertheless correctly accepted.
2384n/a parser = ET.XMLParser(None, ET.TreeBuilder(), 'utf-8')
2385n/a parser.feed(self.sample1)
2386n/a self._check_sample_element(parser.close())
2387n/a
2388n/a # Now as keyword args.
2389n/a parser2 = ET.XMLParser(encoding='utf-8',
2390n/a html=[{}],
2391n/a target=ET.TreeBuilder())
2392n/a parser2.feed(self.sample1)
2393n/a self._check_sample_element(parser2.close())
2394n/a
2395n/a def test_subclass(self):
2396n/a class MyParser(ET.XMLParser):
2397n/a pass
2398n/a parser = MyParser()
2399n/a parser.feed(self.sample1)
2400n/a self._check_sample_element(parser.close())
2401n/a
2402n/a def test_doctype_warning(self):
2403n/a parser = ET.XMLParser()
2404n/a with self.assertWarns(DeprecationWarning):
2405n/a parser.doctype('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
2406n/a 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')
2407n/a parser.feed('<html/>')
2408n/a parser.close()
2409n/a
2410n/a with warnings.catch_warnings():
2411n/a warnings.simplefilter('error', DeprecationWarning)
2412n/a parser = ET.XMLParser()
2413n/a parser.feed(self.sample2)
2414n/a parser.close()
2415n/a
2416n/a def test_subclass_doctype(self):
2417n/a _doctype = None
2418n/a class MyParserWithDoctype(ET.XMLParser):
2419n/a def doctype(self, name, pubid, system):
2420n/a nonlocal _doctype
2421n/a _doctype = (name, pubid, system)
2422n/a
2423n/a parser = MyParserWithDoctype()
2424n/a with self.assertWarns(DeprecationWarning):
2425n/a parser.feed(self.sample2)
2426n/a parser.close()
2427n/a self.assertEqual(_doctype,
2428n/a ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
2429n/a 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'))
2430n/a
2431n/a _doctype = _doctype2 = None
2432n/a with warnings.catch_warnings():
2433n/a warnings.simplefilter('error', DeprecationWarning)
2434n/a class DoctypeParser:
2435n/a def doctype(self, name, pubid, system):
2436n/a nonlocal _doctype2
2437n/a _doctype2 = (name, pubid, system)
2438n/a
2439n/a parser = MyParserWithDoctype(target=DoctypeParser())
2440n/a parser.feed(self.sample2)
2441n/a parser.close()
2442n/a self.assertIsNone(_doctype)
2443n/a self.assertEqual(_doctype2,
2444n/a ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
2445n/a 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'))
2446n/a
2447n/a def test_inherited_doctype(self):
2448n/a '''Ensure that ordinary usage is not deprecated (Issue 19176)'''
2449n/a with warnings.catch_warnings():
2450n/a warnings.simplefilter('error', DeprecationWarning)
2451n/a class MyParserWithoutDoctype(ET.XMLParser):
2452n/a pass
2453n/a parser = MyParserWithoutDoctype()
2454n/a parser.feed(self.sample2)
2455n/a parser.close()
2456n/a
2457n/a def test_parse_string(self):
2458n/a parser = ET.XMLParser(target=ET.TreeBuilder())
2459n/a parser.feed(self.sample3)
2460n/a e = parser.close()
2461n/a self.assertEqual(e.tag, 'money')
2462n/a self.assertEqual(e.attrib['value'], '$\xa3\u20ac\U0001017b')
2463n/a self.assertEqual(e.text, '$\xa3\u20ac\U0001017b')
2464n/a
2465n/a
2466n/aclass NamespaceParseTest(unittest.TestCase):
2467n/a def test_find_with_namespace(self):
2468n/a nsmap = {'h': 'hello', 'f': 'foo'}
2469n/a doc = ET.fromstring(SAMPLE_XML_NS_ELEMS)
2470n/a
2471n/a self.assertEqual(len(doc.findall('{hello}table', nsmap)), 1)
2472n/a self.assertEqual(len(doc.findall('.//{hello}td', nsmap)), 2)
2473n/a self.assertEqual(len(doc.findall('.//{foo}name', nsmap)), 1)
2474n/a
2475n/a
2476n/aclass ElementSlicingTest(unittest.TestCase):
2477n/a def _elem_tags(self, elemlist):
2478n/a return [e.tag for e in elemlist]
2479n/a
2480n/a def _subelem_tags(self, elem):
2481n/a return self._elem_tags(list(elem))
2482n/a
2483n/a def _make_elem_with_children(self, numchildren):
2484n/a """Create an Element with a tag 'a', with the given amount of children
2485n/a named 'a0', 'a1' ... and so on.
2486n/a
2487n/a """
2488n/a e = ET.Element('a')
2489n/a for i in range(numchildren):
2490n/a ET.SubElement(e, 'a%s' % i)
2491n/a return e
2492n/a
2493n/a def test_getslice_single_index(self):
2494n/a e = self._make_elem_with_children(10)
2495n/a
2496n/a self.assertEqual(e[1].tag, 'a1')
2497n/a self.assertEqual(e[-2].tag, 'a8')
2498n/a
2499n/a self.assertRaises(IndexError, lambda: e[12])
2500n/a self.assertRaises(IndexError, lambda: e[-12])
2501n/a
2502n/a def test_getslice_range(self):
2503n/a e = self._make_elem_with_children(6)
2504n/a
2505n/a self.assertEqual(self._elem_tags(e[3:]), ['a3', 'a4', 'a5'])
2506n/a self.assertEqual(self._elem_tags(e[3:6]), ['a3', 'a4', 'a5'])
2507n/a self.assertEqual(self._elem_tags(e[3:16]), ['a3', 'a4', 'a5'])
2508n/a self.assertEqual(self._elem_tags(e[3:5]), ['a3', 'a4'])
2509n/a self.assertEqual(self._elem_tags(e[3:-1]), ['a3', 'a4'])
2510n/a self.assertEqual(self._elem_tags(e[:2]), ['a0', 'a1'])
2511n/a
2512n/a def test_getslice_steps(self):
2513n/a e = self._make_elem_with_children(10)
2514n/a
2515n/a self.assertEqual(self._elem_tags(e[8:10:1]), ['a8', 'a9'])
2516n/a self.assertEqual(self._elem_tags(e[::3]), ['a0', 'a3', 'a6', 'a9'])
2517n/a self.assertEqual(self._elem_tags(e[::8]), ['a0', 'a8'])
2518n/a self.assertEqual(self._elem_tags(e[1::8]), ['a1', 'a9'])
2519n/a self.assertEqual(self._elem_tags(e[3::sys.maxsize]), ['a3'])
2520n/a self.assertEqual(self._elem_tags(e[3::sys.maxsize<<64]), ['a3'])
2521n/a
2522n/a def test_getslice_negative_steps(self):
2523n/a e = self._make_elem_with_children(4)
2524n/a
2525n/a self.assertEqual(self._elem_tags(e[::-1]), ['a3', 'a2', 'a1', 'a0'])
2526n/a self.assertEqual(self._elem_tags(e[::-2]), ['a3', 'a1'])
2527n/a self.assertEqual(self._elem_tags(e[3::-sys.maxsize]), ['a3'])
2528n/a self.assertEqual(self._elem_tags(e[3::-sys.maxsize-1]), ['a3'])
2529n/a self.assertEqual(self._elem_tags(e[3::-sys.maxsize<<64]), ['a3'])
2530n/a
2531n/a def test_delslice(self):
2532n/a e = self._make_elem_with_children(4)
2533n/a del e[0:2]
2534n/a self.assertEqual(self._subelem_tags(e), ['a2', 'a3'])
2535n/a
2536n/a e = self._make_elem_with_children(4)
2537n/a del e[0:]
2538n/a self.assertEqual(self._subelem_tags(e), [])
2539n/a
2540n/a e = self._make_elem_with_children(4)
2541n/a del e[::-1]
2542n/a self.assertEqual(self._subelem_tags(e), [])
2543n/a
2544n/a e = self._make_elem_with_children(4)
2545n/a del e[::-2]
2546n/a self.assertEqual(self._subelem_tags(e), ['a0', 'a2'])
2547n/a
2548n/a e = self._make_elem_with_children(4)
2549n/a del e[1::2]
2550n/a self.assertEqual(self._subelem_tags(e), ['a0', 'a2'])
2551n/a
2552n/a e = self._make_elem_with_children(2)
2553n/a del e[::2]
2554n/a self.assertEqual(self._subelem_tags(e), ['a1'])
2555n/a
2556n/a def test_setslice_single_index(self):
2557n/a e = self._make_elem_with_children(4)
2558n/a e[1] = ET.Element('b')
2559n/a self.assertEqual(self._subelem_tags(e), ['a0', 'b', 'a2', 'a3'])
2560n/a
2561n/a e[-2] = ET.Element('c')
2562n/a self.assertEqual(self._subelem_tags(e), ['a0', 'b', 'c', 'a3'])
2563n/a
2564n/a with self.assertRaises(IndexError):
2565n/a e[5] = ET.Element('d')
2566n/a with self.assertRaises(IndexError):
2567n/a e[-5] = ET.Element('d')
2568n/a self.assertEqual(self._subelem_tags(e), ['a0', 'b', 'c', 'a3'])
2569n/a
2570n/a def test_setslice_range(self):
2571n/a e = self._make_elem_with_children(4)
2572n/a e[1:3] = [ET.Element('b%s' % i) for i in range(2)]
2573n/a self.assertEqual(self._subelem_tags(e), ['a0', 'b0', 'b1', 'a3'])
2574n/a
2575n/a e = self._make_elem_with_children(4)
2576n/a e[1:3] = [ET.Element('b')]
2577n/a self.assertEqual(self._subelem_tags(e), ['a0', 'b', 'a3'])
2578n/a
2579n/a e = self._make_elem_with_children(4)
2580n/a e[1:3] = [ET.Element('b%s' % i) for i in range(3)]
2581n/a self.assertEqual(self._subelem_tags(e), ['a0', 'b0', 'b1', 'b2', 'a3'])
2582n/a
2583n/a def test_setslice_steps(self):
2584n/a e = self._make_elem_with_children(6)
2585n/a e[1:5:2] = [ET.Element('b%s' % i) for i in range(2)]
2586n/a self.assertEqual(self._subelem_tags(e), ['a0', 'b0', 'a2', 'b1', 'a4', 'a5'])
2587n/a
2588n/a e = self._make_elem_with_children(6)
2589n/a with self.assertRaises(ValueError):
2590n/a e[1:5:2] = [ET.Element('b')]
2591n/a with self.assertRaises(ValueError):
2592n/a e[1:5:2] = [ET.Element('b%s' % i) for i in range(3)]
2593n/a with self.assertRaises(ValueError):
2594n/a e[1:5:2] = []
2595n/a self.assertEqual(self._subelem_tags(e), ['a0', 'a1', 'a2', 'a3', 'a4', 'a5'])
2596n/a
2597n/a e = self._make_elem_with_children(4)
2598n/a e[1::sys.maxsize] = [ET.Element('b')]
2599n/a self.assertEqual(self._subelem_tags(e), ['a0', 'b', 'a2', 'a3'])
2600n/a e[1::sys.maxsize<<64] = [ET.Element('c')]
2601n/a self.assertEqual(self._subelem_tags(e), ['a0', 'c', 'a2', 'a3'])
2602n/a
2603n/a def test_setslice_negative_steps(self):
2604n/a e = self._make_elem_with_children(4)
2605n/a e[2:0:-1] = [ET.Element('b%s' % i) for i in range(2)]
2606n/a self.assertEqual(self._subelem_tags(e), ['a0', 'b1', 'b0', 'a3'])
2607n/a
2608n/a e = self._make_elem_with_children(4)
2609n/a with self.assertRaises(ValueError):
2610n/a e[2:0:-1] = [ET.Element('b')]
2611n/a with self.assertRaises(ValueError):
2612n/a e[2:0:-1] = [ET.Element('b%s' % i) for i in range(3)]
2613n/a with self.assertRaises(ValueError):
2614n/a e[2:0:-1] = []
2615n/a self.assertEqual(self._subelem_tags(e), ['a0', 'a1', 'a2', 'a3'])
2616n/a
2617n/a e = self._make_elem_with_children(4)
2618n/a e[1::-sys.maxsize] = [ET.Element('b')]
2619n/a self.assertEqual(self._subelem_tags(e), ['a0', 'b', 'a2', 'a3'])
2620n/a e[1::-sys.maxsize-1] = [ET.Element('c')]
2621n/a self.assertEqual(self._subelem_tags(e), ['a0', 'c', 'a2', 'a3'])
2622n/a e[1::-sys.maxsize<<64] = [ET.Element('d')]
2623n/a self.assertEqual(self._subelem_tags(e), ['a0', 'd', 'a2', 'a3'])
2624n/a
2625n/a
2626n/aclass IOTest(unittest.TestCase):
2627n/a def tearDown(self):
2628n/a support.unlink(TESTFN)
2629n/a
2630n/a def test_encoding(self):
2631n/a # Test encoding issues.
2632n/a elem = ET.Element("tag")
2633n/a elem.text = "abc"
2634n/a self.assertEqual(serialize(elem), '<tag>abc</tag>')
2635n/a for enc in ("utf-8", "us-ascii"):
2636n/a with self.subTest(enc):
2637n/a self.assertEqual(serialize(elem, encoding=enc),
2638n/a b'<tag>abc</tag>')
2639n/a self.assertEqual(serialize(elem, encoding=enc.upper()),
2640n/a b'<tag>abc</tag>')
2641n/a for enc in ("iso-8859-1", "utf-16", "utf-32"):
2642n/a with self.subTest(enc):
2643n/a self.assertEqual(serialize(elem, encoding=enc),
2644n/a ("<?xml version='1.0' encoding='%s'?>\n"
2645n/a "<tag>abc</tag>" % enc).encode(enc))
2646n/a upper = enc.upper()
2647n/a self.assertEqual(serialize(elem, encoding=upper),
2648n/a ("<?xml version='1.0' encoding='%s'?>\n"
2649n/a "<tag>abc</tag>" % upper).encode(enc))
2650n/a
2651n/a elem = ET.Element("tag")
2652n/a elem.text = "<&\"\'>"
2653n/a self.assertEqual(serialize(elem), '<tag>&lt;&amp;"\'&gt;</tag>')
2654n/a self.assertEqual(serialize(elem, encoding="utf-8"),
2655n/a b'<tag>&lt;&amp;"\'&gt;</tag>')
2656n/a self.assertEqual(serialize(elem, encoding="us-ascii"),
2657n/a b'<tag>&lt;&amp;"\'&gt;</tag>')
2658n/a for enc in ("iso-8859-1", "utf-16", "utf-32"):
2659n/a self.assertEqual(serialize(elem, encoding=enc),
2660n/a ("<?xml version='1.0' encoding='%s'?>\n"
2661n/a "<tag>&lt;&amp;\"'&gt;</tag>" % enc).encode(enc))
2662n/a
2663n/a elem = ET.Element("tag")
2664n/a elem.attrib["key"] = "<&\"\'>"
2665n/a self.assertEqual(serialize(elem), '<tag key="&lt;&amp;&quot;\'&gt;" />')
2666n/a self.assertEqual(serialize(elem, encoding="utf-8"),
2667n/a b'<tag key="&lt;&amp;&quot;\'&gt;" />')
2668n/a self.assertEqual(serialize(elem, encoding="us-ascii"),
2669n/a b'<tag key="&lt;&amp;&quot;\'&gt;" />')
2670n/a for enc in ("iso-8859-1", "utf-16", "utf-32"):
2671n/a self.assertEqual(serialize(elem, encoding=enc),
2672n/a ("<?xml version='1.0' encoding='%s'?>\n"
2673n/a "<tag key=\"&lt;&amp;&quot;'&gt;\" />" % enc).encode(enc))
2674n/a
2675n/a elem = ET.Element("tag")
2676n/a elem.text = '\xe5\xf6\xf6<>'
2677n/a self.assertEqual(serialize(elem), '<tag>\xe5\xf6\xf6&lt;&gt;</tag>')
2678n/a self.assertEqual(serialize(elem, encoding="utf-8"),
2679n/a b'<tag>\xc3\xa5\xc3\xb6\xc3\xb6&lt;&gt;</tag>')
2680n/a self.assertEqual(serialize(elem, encoding="us-ascii"),
2681n/a b'<tag>&#229;&#246;&#246;&lt;&gt;</tag>')
2682n/a for enc in ("iso-8859-1", "utf-16", "utf-32"):
2683n/a self.assertEqual(serialize(elem, encoding=enc),
2684n/a ("<?xml version='1.0' encoding='%s'?>\n"
2685n/a "<tag>åöö&lt;&gt;</tag>" % enc).encode(enc))
2686n/a
2687n/a elem = ET.Element("tag")
2688n/a elem.attrib["key"] = '\xe5\xf6\xf6<>'
2689n/a self.assertEqual(serialize(elem), '<tag key="\xe5\xf6\xf6&lt;&gt;" />')
2690n/a self.assertEqual(serialize(elem, encoding="utf-8"),
2691n/a b'<tag key="\xc3\xa5\xc3\xb6\xc3\xb6&lt;&gt;" />')
2692n/a self.assertEqual(serialize(elem, encoding="us-ascii"),
2693n/a b'<tag key="&#229;&#246;&#246;&lt;&gt;" />')
2694n/a for enc in ("iso-8859-1", "utf-16", "utf-16le", "utf-16be", "utf-32"):
2695n/a self.assertEqual(serialize(elem, encoding=enc),
2696n/a ("<?xml version='1.0' encoding='%s'?>\n"
2697n/a "<tag key=\"åöö&lt;&gt;\" />" % enc).encode(enc))
2698n/a
2699n/a def test_write_to_filename(self):
2700n/a tree = ET.ElementTree(ET.XML('''<site />'''))
2701n/a tree.write(TESTFN)
2702n/a with open(TESTFN, 'rb') as f:
2703n/a self.assertEqual(f.read(), b'''<site />''')
2704n/a
2705n/a def test_write_to_text_file(self):
2706n/a tree = ET.ElementTree(ET.XML('''<site />'''))
2707n/a with open(TESTFN, 'w', encoding='utf-8') as f:
2708n/a tree.write(f, encoding='unicode')
2709n/a self.assertFalse(f.closed)
2710n/a with open(TESTFN, 'rb') as f:
2711n/a self.assertEqual(f.read(), b'''<site />''')
2712n/a
2713n/a def test_write_to_binary_file(self):
2714n/a tree = ET.ElementTree(ET.XML('''<site />'''))
2715n/a with open(TESTFN, 'wb') as f:
2716n/a tree.write(f)
2717n/a self.assertFalse(f.closed)
2718n/a with open(TESTFN, 'rb') as f:
2719n/a self.assertEqual(f.read(), b'''<site />''')
2720n/a
2721n/a def test_write_to_binary_file_with_bom(self):
2722n/a tree = ET.ElementTree(ET.XML('''<site />'''))
2723n/a # test BOM writing to buffered file
2724n/a with open(TESTFN, 'wb') as f:
2725n/a tree.write(f, encoding='utf-16')
2726n/a self.assertFalse(f.closed)
2727n/a with open(TESTFN, 'rb') as f:
2728n/a self.assertEqual(f.read(),
2729n/a '''<?xml version='1.0' encoding='utf-16'?>\n'''
2730n/a '''<site />'''.encode("utf-16"))
2731n/a # test BOM writing to non-buffered file
2732n/a with open(TESTFN, 'wb', buffering=0) as f:
2733n/a tree.write(f, encoding='utf-16')
2734n/a self.assertFalse(f.closed)
2735n/a with open(TESTFN, 'rb') as f:
2736n/a self.assertEqual(f.read(),
2737n/a '''<?xml version='1.0' encoding='utf-16'?>\n'''
2738n/a '''<site />'''.encode("utf-16"))
2739n/a
2740n/a def test_read_from_stringio(self):
2741n/a tree = ET.ElementTree()
2742n/a stream = io.StringIO('''<?xml version="1.0"?><site></site>''')
2743n/a tree.parse(stream)
2744n/a self.assertEqual(tree.getroot().tag, 'site')
2745n/a
2746n/a def test_write_to_stringio(self):
2747n/a tree = ET.ElementTree(ET.XML('''<site />'''))
2748n/a stream = io.StringIO()
2749n/a tree.write(stream, encoding='unicode')
2750n/a self.assertEqual(stream.getvalue(), '''<site />''')
2751n/a
2752n/a def test_read_from_bytesio(self):
2753n/a tree = ET.ElementTree()
2754n/a raw = io.BytesIO(b'''<?xml version="1.0"?><site></site>''')
2755n/a tree.parse(raw)
2756n/a self.assertEqual(tree.getroot().tag, 'site')
2757n/a
2758n/a def test_write_to_bytesio(self):
2759n/a tree = ET.ElementTree(ET.XML('''<site />'''))
2760n/a raw = io.BytesIO()
2761n/a tree.write(raw)
2762n/a self.assertEqual(raw.getvalue(), b'''<site />''')
2763n/a
2764n/a class dummy:
2765n/a pass
2766n/a
2767n/a def test_read_from_user_text_reader(self):
2768n/a stream = io.StringIO('''<?xml version="1.0"?><site></site>''')
2769n/a reader = self.dummy()
2770n/a reader.read = stream.read
2771n/a tree = ET.ElementTree()
2772n/a tree.parse(reader)
2773n/a self.assertEqual(tree.getroot().tag, 'site')
2774n/a
2775n/a def test_write_to_user_text_writer(self):
2776n/a tree = ET.ElementTree(ET.XML('''<site />'''))
2777n/a stream = io.StringIO()
2778n/a writer = self.dummy()
2779n/a writer.write = stream.write
2780n/a tree.write(writer, encoding='unicode')
2781n/a self.assertEqual(stream.getvalue(), '''<site />''')
2782n/a
2783n/a def test_read_from_user_binary_reader(self):
2784n/a raw = io.BytesIO(b'''<?xml version="1.0"?><site></site>''')
2785n/a reader = self.dummy()
2786n/a reader.read = raw.read
2787n/a tree = ET.ElementTree()
2788n/a tree.parse(reader)
2789n/a self.assertEqual(tree.getroot().tag, 'site')
2790n/a tree = ET.ElementTree()
2791n/a
2792n/a def test_write_to_user_binary_writer(self):
2793n/a tree = ET.ElementTree(ET.XML('''<site />'''))
2794n/a raw = io.BytesIO()
2795n/a writer = self.dummy()
2796n/a writer.write = raw.write
2797n/a tree.write(writer)
2798n/a self.assertEqual(raw.getvalue(), b'''<site />''')
2799n/a
2800n/a def test_write_to_user_binary_writer_with_bom(self):
2801n/a tree = ET.ElementTree(ET.XML('''<site />'''))
2802n/a raw = io.BytesIO()
2803n/a writer = self.dummy()
2804n/a writer.write = raw.write
2805n/a writer.seekable = lambda: True
2806n/a writer.tell = raw.tell
2807n/a tree.write(writer, encoding="utf-16")
2808n/a self.assertEqual(raw.getvalue(),
2809n/a '''<?xml version='1.0' encoding='utf-16'?>\n'''
2810n/a '''<site />'''.encode("utf-16"))
2811n/a
2812n/a def test_tostringlist_invariant(self):
2813n/a root = ET.fromstring('<tag>foo</tag>')
2814n/a self.assertEqual(
2815n/a ET.tostring(root, 'unicode'),
2816n/a ''.join(ET.tostringlist(root, 'unicode')))
2817n/a self.assertEqual(
2818n/a ET.tostring(root, 'utf-16'),
2819n/a b''.join(ET.tostringlist(root, 'utf-16')))
2820n/a
2821n/a def test_short_empty_elements(self):
2822n/a root = ET.fromstring('<tag>a<x />b<y></y>c</tag>')
2823n/a self.assertEqual(
2824n/a ET.tostring(root, 'unicode'),
2825n/a '<tag>a<x />b<y />c</tag>')
2826n/a self.assertEqual(
2827n/a ET.tostring(root, 'unicode', short_empty_elements=True),
2828n/a '<tag>a<x />b<y />c</tag>')
2829n/a self.assertEqual(
2830n/a ET.tostring(root, 'unicode', short_empty_elements=False),
2831n/a '<tag>a<x></x>b<y></y>c</tag>')
2832n/a
2833n/a
2834n/aclass ParseErrorTest(unittest.TestCase):
2835n/a def test_subclass(self):
2836n/a self.assertIsInstance(ET.ParseError(), SyntaxError)
2837n/a
2838n/a def _get_error(self, s):
2839n/a try:
2840n/a ET.fromstring(s)
2841n/a except ET.ParseError as e:
2842n/a return e
2843n/a
2844n/a def test_error_position(self):
2845n/a self.assertEqual(self._get_error('foo').position, (1, 0))
2846n/a self.assertEqual(self._get_error('<tag>&foo;</tag>').position, (1, 5))
2847n/a self.assertEqual(self._get_error('foobar<').position, (1, 6))
2848n/a
2849n/a def test_error_code(self):
2850n/a import xml.parsers.expat.errors as ERRORS
2851n/a self.assertEqual(self._get_error('foo').code,
2852n/a ERRORS.codes[ERRORS.XML_ERROR_SYNTAX])
2853n/a
2854n/a
2855n/aclass KeywordArgsTest(unittest.TestCase):
2856n/a # Test various issues with keyword arguments passed to ET.Element
2857n/a # constructor and methods
2858n/a def test_issue14818(self):
2859n/a x = ET.XML("<a>foo</a>")
2860n/a self.assertEqual(x.find('a', None),
2861n/a x.find(path='a', namespaces=None))
2862n/a self.assertEqual(x.findtext('a', None, None),
2863n/a x.findtext(path='a', default=None, namespaces=None))
2864n/a self.assertEqual(x.findall('a', None),
2865n/a x.findall(path='a', namespaces=None))
2866n/a self.assertEqual(list(x.iterfind('a', None)),
2867n/a list(x.iterfind(path='a', namespaces=None)))
2868n/a
2869n/a self.assertEqual(ET.Element('a').attrib, {})
2870n/a elements = [
2871n/a ET.Element('a', dict(href="#", id="foo")),
2872n/a ET.Element('a', attrib=dict(href="#", id="foo")),
2873n/a ET.Element('a', dict(href="#"), id="foo"),
2874n/a ET.Element('a', href="#", id="foo"),
2875n/a ET.Element('a', dict(href="#", id="foo"), href="#", id="foo"),
2876n/a ]
2877n/a for e in elements:
2878n/a self.assertEqual(e.tag, 'a')
2879n/a self.assertEqual(e.attrib, dict(href="#", id="foo"))
2880n/a
2881n/a e2 = ET.SubElement(elements[0], 'foobar', attrib={'key1': 'value1'})
2882n/a self.assertEqual(e2.attrib['key1'], 'value1')
2883n/a
2884n/a with self.assertRaisesRegex(TypeError, 'must be dict, not str'):
2885n/a ET.Element('a', "I'm not a dict")
2886n/a with self.assertRaisesRegex(TypeError, 'must be dict, not str'):
2887n/a ET.Element('a', attrib="I'm not a dict")
2888n/a
2889n/a# --------------------------------------------------------------------
2890n/a
2891n/aclass NoAcceleratorTest(unittest.TestCase):
2892n/a def setUp(self):
2893n/a if not pyET:
2894n/a raise unittest.SkipTest('only for the Python version')
2895n/a
2896n/a # Test that the C accelerator was not imported for pyET
2897n/a def test_correct_import_pyET(self):
2898n/a # The type of methods defined in Python code is types.FunctionType,
2899n/a # while the type of methods defined inside _elementtree is
2900n/a # <class 'wrapper_descriptor'>
2901n/a self.assertIsInstance(pyET.Element.__init__, types.FunctionType)
2902n/a self.assertIsInstance(pyET.XMLParser.__init__, types.FunctionType)
2903n/a
2904n/a# --------------------------------------------------------------------
2905n/a
2906n/a
2907n/aclass CleanContext(object):
2908n/a """Provide default namespace mapping and path cache."""
2909n/a checkwarnings = None
2910n/a
2911n/a def __init__(self, quiet=False):
2912n/a if sys.flags.optimize >= 2:
2913n/a # under -OO, doctests cannot be run and therefore not all warnings
2914n/a # will be emitted
2915n/a quiet = True
2916n/a deprecations = (
2917n/a # Search behaviour is broken if search path starts with "/".
2918n/a ("This search is broken in 1.3 and earlier, and will be fixed "
2919n/a "in a future version. If you rely on the current behaviour, "
2920n/a "change it to '.+'", FutureWarning),
2921n/a # Element.getchildren() and Element.getiterator() are deprecated.
2922n/a ("This method will be removed in future versions. "
2923n/a "Use .+ instead.", DeprecationWarning),
2924n/a ("This method will be removed in future versions. "
2925n/a "Use .+ instead.", PendingDeprecationWarning))
2926n/a self.checkwarnings = support.check_warnings(*deprecations, quiet=quiet)
2927n/a
2928n/a def __enter__(self):
2929n/a from xml.etree import ElementPath
2930n/a self._nsmap = ET.register_namespace._namespace_map
2931n/a # Copy the default namespace mapping
2932n/a self._nsmap_copy = self._nsmap.copy()
2933n/a # Copy the path cache (should be empty)
2934n/a self._path_cache = ElementPath._cache
2935n/a ElementPath._cache = self._path_cache.copy()
2936n/a self.checkwarnings.__enter__()
2937n/a
2938n/a def __exit__(self, *args):
2939n/a from xml.etree import ElementPath
2940n/a # Restore mapping and path cache
2941n/a self._nsmap.clear()
2942n/a self._nsmap.update(self._nsmap_copy)
2943n/a ElementPath._cache = self._path_cache
2944n/a self.checkwarnings.__exit__(*args)
2945n/a
2946n/a
2947n/adef test_main(module=None):
2948n/a # When invoked without a module, runs the Python ET tests by loading pyET.
2949n/a # Otherwise, uses the given module as the ET.
2950n/a global pyET
2951n/a pyET = import_fresh_module('xml.etree.ElementTree',
2952n/a blocked=['_elementtree'])
2953n/a if module is None:
2954n/a module = pyET
2955n/a
2956n/a global ET
2957n/a ET = module
2958n/a
2959n/a test_classes = [
2960n/a ModuleTest,
2961n/a ElementSlicingTest,
2962n/a BasicElementTest,
2963n/a BadElementTest,
2964n/a BadElementPathTest,
2965n/a ElementTreeTest,
2966n/a IOTest,
2967n/a ParseErrorTest,
2968n/a XIncludeTest,
2969n/a ElementTreeTypeTest,
2970n/a ElementFindTest,
2971n/a ElementIterTest,
2972n/a TreeBuilderTest,
2973n/a XMLParserTest,
2974n/a XMLPullParserTest,
2975n/a BugsTest,
2976n/a ]
2977n/a
2978n/a # These tests will only run for the pure-Python version that doesn't import
2979n/a # _elementtree. We can't use skipUnless here, because pyET is filled in only
2980n/a # after the module is loaded.
2981n/a if pyET is not ET:
2982n/a test_classes.extend([
2983n/a NoAcceleratorTest,
2984n/a ])
2985n/a
2986n/a try:
2987n/a # XXX the C module should give the same warnings as the Python module
2988n/a with CleanContext(quiet=(pyET is not ET)):
2989n/a support.run_unittest(*test_classes)
2990n/a finally:
2991n/a # don't interfere with subsequent tests
2992n/a ET = pyET = None
2993n/a
2994n/a
2995n/aif __name__ == '__main__':
2996n/a test_main()