1# xml.etree test for cElementTree 2 3from test import test_support 4from test.test_support import precisionbigmemtest, _2G 5import unittest 6 7cET = test_support.import_module('xml.etree.cElementTree') 8 9 10@unittest.skipUnless(cET, 'requires _elementtree') 11class MiscTests(unittest.TestCase): 12 # Issue #8651. 13 @precisionbigmemtest(size=_2G + 100, memuse=1) 14 def test_length_overflow(self, size): 15 if size < _2G + 100: 16 self.skipTest("not enough free memory, need at least 2 GB") 17 data = b'x' * size 18 parser = cET.XMLParser() 19 try: 20 self.assertRaises(OverflowError, parser.feed, data) 21 finally: 22 data = None 23 24 def test_del_attribute(self): 25 element = cET.Element('tag') 26 27 element.tag = 'TAG' 28 with self.assertRaises(AttributeError): 29 del element.tag 30 self.assertEqual(element.tag, 'TAG') 31 32 with self.assertRaises(AttributeError): 33 del element.text 34 self.assertIsNone(element.text) 35 element.text = 'TEXT' 36 with self.assertRaises(AttributeError): 37 del element.text 38 self.assertEqual(element.text, 'TEXT') 39 40 with self.assertRaises(AttributeError): 41 del element.tail 42 self.assertIsNone(element.tail) 43 element.tail = 'TAIL' 44 with self.assertRaises(AttributeError): 45 del element.tail 46 self.assertEqual(element.tail, 'TAIL') 47 48 with self.assertRaises(AttributeError): 49 del element.attrib 50 self.assertEqual(element.attrib, {}) 51 element.attrib = {'A': 'B', 'C': 'D'} 52 with self.assertRaises(AttributeError): 53 del element.attrib 54 self.assertEqual(element.attrib, {'A': 'B', 'C': 'D'}) 55 56 def test_bpo_31728(self): 57 # A crash shouldn't happen in case garbage collection triggers a call 58 # to clear() or a reading of text or tail, while a setter or clear() 59 # is already running. 60 elem = cET.Element('elem') 61 class X: 62 def __del__(self): 63 elem.text 64 elem.tail 65 elem.clear() 66 67 elem.text = X() 68 elem.clear() # shouldn't crash 69 70 elem.tail = X() 71 elem.clear() # shouldn't crash 72 73 elem.text = X() 74 elem.text = X() # shouldn't crash 75 elem.clear() 76 77 elem.tail = X() 78 elem.tail = X() # shouldn't crash 79 elem.clear() 80 81 82def test_main(): 83 from test import test_xml_etree, test_xml_etree_c 84 85 # Run the tests specific to the C implementation 86 test_support.run_unittest(MiscTests) 87 88 # Run the same test suite as the Python module 89 test_xml_etree.test_main(module=cET) 90 91 92if __name__ == '__main__': 93 test_main() 94