test_xml_etree_c.py 2.93 KB
Newer Older
1
# xml.etree test for cElementTree
2
import sys, struct
3
from test import support
4
from test.support import import_fresh_module
5
import types
6
import unittest
Fredrik Lundh's avatar
Fredrik Lundh committed
7

Eli Bendersky's avatar
Eli Bendersky committed
8 9 10 11
cET = import_fresh_module('xml.etree.ElementTree',
                          fresh=['_elementtree'])
cET_alias = import_fresh_module('xml.etree.cElementTree',
                                fresh=['_elementtree', 'xml.etree'])
12

Fredrik Lundh's avatar
Fredrik Lundh committed
13

14 15
class MiscTests(unittest.TestCase):
    # Issue #8651.
16
    @support.bigmemtest(size=support._2G + 100, memuse=1, dry_run=False)
17 18 19 20 21 22 23 24
    def test_length_overflow(self, size):
        data = b'x' * size
        parser = cET.XMLParser()
        try:
            self.assertRaises(OverflowError, parser.feed, data)
        finally:
            data = None

25

26 27 28 29 30 31 32
@unittest.skipUnless(cET, 'requires _elementtree')
class TestAliasWorking(unittest.TestCase):
    # Test that the cET alias module is alive
    def test_alias_working(self):
        e = cET_alias.Element('foo')
        self.assertEqual(e.tag, 'foo')

33

34
@unittest.skipUnless(cET, 'requires _elementtree')
35
@support.cpython_only
36 37 38
class TestAcceleratorImported(unittest.TestCase):
    # Test that the C accelerator was imported, as expected
    def test_correct_import_cET(self):
39
        # SubElement is a function so it retains _elementtree as its module.
40
        self.assertEqual(cET.SubElement.__module__, '_elementtree')
41

42
    def test_correct_import_cET_alias(self):
43
        self.assertEqual(cET_alias.SubElement.__module__, '_elementtree')
44

45 46 47 48 49 50
    def test_parser_comes_from_C(self):
        # The type of methods defined in Python code is types.FunctionType,
        # while the type of methods defined inside _elementtree is
        # <class 'wrapper_descriptor'>
        self.assertNotIsInstance(cET.Element.__init__, types.FunctionType)

51

52
@unittest.skipUnless(cET, 'requires _elementtree')
53
@support.cpython_only
54 55
class SizeofTest(unittest.TestCase):
    def setUp(self):
56
        self.elementsize = support.calcobjsize('5P')
57 58 59
        # extra
        self.extra = struct.calcsize('PiiP4P')

60 61
    check_sizeof = support.check_sizeof

62 63
    def test_element(self):
        e = cET.Element('a')
64
        self.check_sizeof(e, self.elementsize)
65 66 67

    def test_element_with_attrib(self):
        e = cET.Element('a', href='about:')
68
        self.check_sizeof(e, self.elementsize + self.extra)
69 70 71 72 73 74

    def test_element_with_children(self):
        e = cET.Element('a')
        for i in range(5):
            cET.SubElement(e, 'span')
        # should have space for 8 children now
75 76
        self.check_sizeof(e, self.elementsize + self.extra +
                             struct.calcsize('8P'))
77

Fredrik Lundh's avatar
Fredrik Lundh committed
78
def test_main():
79 80 81
    from test import test_xml_etree, test_xml_etree_c

    # Run the tests specific to the C implementation
82 83 84
    support.run_unittest(
        MiscTests,
        TestAliasWorking,
85 86
        TestAcceleratorImported,
        SizeofTest,
87
        )
88

89 90
    # Run the same test suite as the Python module
    test_xml_etree.test_main(module=cET)
91

92

Fredrik Lundh's avatar
Fredrik Lundh committed
93 94
if __name__ == '__main__':
    test_main()