Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1 | # xml.etree test for cElementTree |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 2 | import sys, struct |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Florent Xicluna | 6c3da6e | 2012-02-13 12:14:52 +0100 | [diff] [blame] | 4 | from test.support import import_fresh_module |
Victor Stinner | b3c9e07 | 2011-01-04 02:07:34 +0000 | [diff] [blame] | 5 | import unittest |
Fredrik Lundh | 9235ea4 | 2005-12-15 18:41:22 +0000 | [diff] [blame] | 6 | |
Eli Bendersky | 2368704 | 2013-02-26 05:53:23 -0800 | [diff] [blame] | 7 | cET = import_fresh_module('xml.etree.ElementTree', |
| 8 | fresh=['_elementtree']) |
| 9 | cET_alias = import_fresh_module('xml.etree.cElementTree', |
| 10 | fresh=['_elementtree', 'xml.etree']) |
Fredrik Lundh | 8911ca3d | 2005-12-16 22:07:17 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 9235ea4 | 2005-12-15 18:41:22 +0000 | [diff] [blame] | 12 | |
Victor Stinner | b3c9e07 | 2011-01-04 02:07:34 +0000 | [diff] [blame] | 13 | class MiscTests(unittest.TestCase): |
| 14 | # Issue #8651. |
Antoine Pitrou | 94190bb | 2011-10-04 10:22:36 +0200 | [diff] [blame] | 15 | @support.bigmemtest(size=support._2G + 100, memuse=1) |
Victor Stinner | b3c9e07 | 2011-01-04 02:07:34 +0000 | [diff] [blame] | 16 | def test_length_overflow(self, size): |
| 17 | if size < support._2G + 100: |
| 18 | self.skipTest("not enough free memory, need at least 2 GB") |
| 19 | data = b'x' * size |
| 20 | parser = cET.XMLParser() |
| 21 | try: |
| 22 | self.assertRaises(OverflowError, parser.feed, data) |
| 23 | finally: |
| 24 | data = None |
| 25 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 26 | |
Eli Bendersky | 292f9a8 | 2012-02-16 19:55:29 +0200 | [diff] [blame] | 27 | @unittest.skipUnless(cET, 'requires _elementtree') |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 28 | class TestAliasWorking(unittest.TestCase): |
| 29 | # Test that the cET alias module is alive |
| 30 | def test_alias_working(self): |
| 31 | e = cET_alias.Element('foo') |
| 32 | self.assertEqual(e.tag, 'foo') |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 33 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 34 | |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 35 | @unittest.skipUnless(cET, 'requires _elementtree') |
Eli Bendersky | da57819 | 2012-02-16 06:52:39 +0200 | [diff] [blame] | 36 | class TestAcceleratorImported(unittest.TestCase): |
| 37 | # Test that the C accelerator was imported, as expected |
| 38 | def test_correct_import_cET(self): |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 39 | self.assertEqual(cET.SubElement.__module__, '_elementtree') |
Eli Bendersky | da57819 | 2012-02-16 06:52:39 +0200 | [diff] [blame] | 40 | |
Florent Xicluna | e59a306 | 2012-02-16 23:17:31 +0100 | [diff] [blame] | 41 | def test_correct_import_cET_alias(self): |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 42 | self.assertEqual(cET_alias.SubElement.__module__, '_elementtree') |
Florent Xicluna | e59a306 | 2012-02-16 23:17:31 +0100 | [diff] [blame] | 43 | |
Victor Stinner | b3c9e07 | 2011-01-04 02:07:34 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 45 | @unittest.skipUnless(cET, 'requires _elementtree') |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 46 | @support.cpython_only |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 47 | class SizeofTest(unittest.TestCase): |
| 48 | def setUp(self): |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 49 | self.elementsize = support.calcobjsize('5P') |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 50 | # extra |
| 51 | self.extra = struct.calcsize('PiiP4P') |
| 52 | |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 53 | check_sizeof = support.check_sizeof |
| 54 | |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 55 | def test_element(self): |
| 56 | e = cET.Element('a') |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 57 | self.check_sizeof(e, self.elementsize) |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 58 | |
| 59 | def test_element_with_attrib(self): |
| 60 | e = cET.Element('a', href='about:') |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 61 | self.check_sizeof(e, self.elementsize + self.extra) |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 62 | |
| 63 | def test_element_with_children(self): |
| 64 | e = cET.Element('a') |
| 65 | for i in range(5): |
| 66 | cET.SubElement(e, 'span') |
| 67 | # should have space for 8 children now |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 68 | self.check_sizeof(e, self.elementsize + self.extra + |
| 69 | struct.calcsize('8P')) |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 70 | |
Fredrik Lundh | 9235ea4 | 2005-12-15 18:41:22 +0000 | [diff] [blame] | 71 | def test_main(): |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 72 | from test import test_xml_etree, test_xml_etree_c |
| 73 | |
| 74 | # Run the tests specific to the C implementation |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 75 | support.run_unittest( |
| 76 | MiscTests, |
| 77 | TestAliasWorking, |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 78 | TestAcceleratorImported, |
| 79 | SizeofTest, |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 80 | ) |
Victor Stinner | b3c9e07 | 2011-01-04 02:07:34 +0000 | [diff] [blame] | 81 | |
Florent Xicluna | a72a98f | 2012-02-13 11:03:30 +0100 | [diff] [blame] | 82 | # Run the same test suite as the Python module |
| 83 | test_xml_etree.test_main(module=cET) |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 84 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 85 | |
Fredrik Lundh | 9235ea4 | 2005-12-15 18:41:22 +0000 | [diff] [blame] | 86 | if __name__ == '__main__': |
| 87 | test_main() |