1 | n/a | # xml.etree test for cElementTree |
---|
2 | n/a | import struct |
---|
3 | n/a | from test import support |
---|
4 | n/a | from test.support import import_fresh_module |
---|
5 | n/a | import types |
---|
6 | n/a | import unittest |
---|
7 | n/a | |
---|
8 | n/a | cET = import_fresh_module('xml.etree.ElementTree', |
---|
9 | n/a | fresh=['_elementtree']) |
---|
10 | n/a | cET_alias = import_fresh_module('xml.etree.cElementTree', |
---|
11 | n/a | fresh=['_elementtree', 'xml.etree']) |
---|
12 | n/a | |
---|
13 | n/a | |
---|
14 | n/a | @unittest.skipUnless(cET, 'requires _elementtree') |
---|
15 | n/a | class MiscTests(unittest.TestCase): |
---|
16 | n/a | # Issue #8651. |
---|
17 | n/a | @support.bigmemtest(size=support._2G + 100, memuse=1, dry_run=False) |
---|
18 | n/a | def test_length_overflow(self, size): |
---|
19 | n/a | data = b'x' * size |
---|
20 | n/a | parser = cET.XMLParser() |
---|
21 | n/a | try: |
---|
22 | n/a | self.assertRaises(OverflowError, parser.feed, data) |
---|
23 | n/a | finally: |
---|
24 | n/a | data = None |
---|
25 | n/a | |
---|
26 | n/a | def test_del_attribute(self): |
---|
27 | n/a | element = cET.Element('tag') |
---|
28 | n/a | |
---|
29 | n/a | element.tag = 'TAG' |
---|
30 | n/a | with self.assertRaises(AttributeError): |
---|
31 | n/a | del element.tag |
---|
32 | n/a | self.assertEqual(element.tag, 'TAG') |
---|
33 | n/a | |
---|
34 | n/a | with self.assertRaises(AttributeError): |
---|
35 | n/a | del element.text |
---|
36 | n/a | self.assertIsNone(element.text) |
---|
37 | n/a | element.text = 'TEXT' |
---|
38 | n/a | with self.assertRaises(AttributeError): |
---|
39 | n/a | del element.text |
---|
40 | n/a | self.assertEqual(element.text, 'TEXT') |
---|
41 | n/a | |
---|
42 | n/a | with self.assertRaises(AttributeError): |
---|
43 | n/a | del element.tail |
---|
44 | n/a | self.assertIsNone(element.tail) |
---|
45 | n/a | element.tail = 'TAIL' |
---|
46 | n/a | with self.assertRaises(AttributeError): |
---|
47 | n/a | del element.tail |
---|
48 | n/a | self.assertEqual(element.tail, 'TAIL') |
---|
49 | n/a | |
---|
50 | n/a | with self.assertRaises(AttributeError): |
---|
51 | n/a | del element.attrib |
---|
52 | n/a | self.assertEqual(element.attrib, {}) |
---|
53 | n/a | element.attrib = {'A': 'B', 'C': 'D'} |
---|
54 | n/a | with self.assertRaises(AttributeError): |
---|
55 | n/a | del element.attrib |
---|
56 | n/a | self.assertEqual(element.attrib, {'A': 'B', 'C': 'D'}) |
---|
57 | n/a | |
---|
58 | n/a | def test_trashcan(self): |
---|
59 | n/a | # If this test fails, it will most likely die via segfault. |
---|
60 | n/a | e = root = cET.Element('root') |
---|
61 | n/a | for i in range(200000): |
---|
62 | n/a | e = cET.SubElement(e, 'x') |
---|
63 | n/a | del e |
---|
64 | n/a | del root |
---|
65 | n/a | support.gc_collect() |
---|
66 | n/a | |
---|
67 | n/a | |
---|
68 | n/a | @unittest.skipUnless(cET, 'requires _elementtree') |
---|
69 | n/a | class TestAliasWorking(unittest.TestCase): |
---|
70 | n/a | # Test that the cET alias module is alive |
---|
71 | n/a | def test_alias_working(self): |
---|
72 | n/a | e = cET_alias.Element('foo') |
---|
73 | n/a | self.assertEqual(e.tag, 'foo') |
---|
74 | n/a | |
---|
75 | n/a | |
---|
76 | n/a | @unittest.skipUnless(cET, 'requires _elementtree') |
---|
77 | n/a | @support.cpython_only |
---|
78 | n/a | class TestAcceleratorImported(unittest.TestCase): |
---|
79 | n/a | # Test that the C accelerator was imported, as expected |
---|
80 | n/a | def test_correct_import_cET(self): |
---|
81 | n/a | # SubElement is a function so it retains _elementtree as its module. |
---|
82 | n/a | self.assertEqual(cET.SubElement.__module__, '_elementtree') |
---|
83 | n/a | |
---|
84 | n/a | def test_correct_import_cET_alias(self): |
---|
85 | n/a | self.assertEqual(cET_alias.SubElement.__module__, '_elementtree') |
---|
86 | n/a | |
---|
87 | n/a | def test_parser_comes_from_C(self): |
---|
88 | n/a | # The type of methods defined in Python code is types.FunctionType, |
---|
89 | n/a | # while the type of methods defined inside _elementtree is |
---|
90 | n/a | # <class 'wrapper_descriptor'> |
---|
91 | n/a | self.assertNotIsInstance(cET.Element.__init__, types.FunctionType) |
---|
92 | n/a | |
---|
93 | n/a | |
---|
94 | n/a | @unittest.skipUnless(cET, 'requires _elementtree') |
---|
95 | n/a | @support.cpython_only |
---|
96 | n/a | class SizeofTest(unittest.TestCase): |
---|
97 | n/a | def setUp(self): |
---|
98 | n/a | self.elementsize = support.calcobjsize('5P') |
---|
99 | n/a | # extra |
---|
100 | n/a | self.extra = struct.calcsize('PnnP4P') |
---|
101 | n/a | |
---|
102 | n/a | check_sizeof = support.check_sizeof |
---|
103 | n/a | |
---|
104 | n/a | def test_element(self): |
---|
105 | n/a | e = cET.Element('a') |
---|
106 | n/a | self.check_sizeof(e, self.elementsize) |
---|
107 | n/a | |
---|
108 | n/a | def test_element_with_attrib(self): |
---|
109 | n/a | e = cET.Element('a', href='about:') |
---|
110 | n/a | self.check_sizeof(e, self.elementsize + self.extra) |
---|
111 | n/a | |
---|
112 | n/a | def test_element_with_children(self): |
---|
113 | n/a | e = cET.Element('a') |
---|
114 | n/a | for i in range(5): |
---|
115 | n/a | cET.SubElement(e, 'span') |
---|
116 | n/a | # should have space for 8 children now |
---|
117 | n/a | self.check_sizeof(e, self.elementsize + self.extra + |
---|
118 | n/a | struct.calcsize('8P')) |
---|
119 | n/a | |
---|
120 | n/a | def test_main(): |
---|
121 | n/a | from test import test_xml_etree |
---|
122 | n/a | |
---|
123 | n/a | # Run the tests specific to the C implementation |
---|
124 | n/a | support.run_unittest( |
---|
125 | n/a | MiscTests, |
---|
126 | n/a | TestAliasWorking, |
---|
127 | n/a | TestAcceleratorImported, |
---|
128 | n/a | SizeofTest, |
---|
129 | n/a | ) |
---|
130 | n/a | |
---|
131 | n/a | # Run the same test suite as the Python module |
---|
132 | n/a | test_xml_etree.test_main(module=cET) |
---|
133 | n/a | |
---|
134 | n/a | |
---|
135 | n/a | if __name__ == '__main__': |
---|
136 | n/a | test_main() |
---|