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