diff options
Diffstat (limited to 'Lib/test/test_xml_etree_c.py')
| -rw-r--r-- | Lib/test/test_xml_etree_c.py | 112 | 
1 files changed, 66 insertions, 46 deletions
diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py index 2ff118fae8..eaeeb56083 100644 --- a/Lib/test/test_xml_etree_c.py +++ b/Lib/test/test_xml_etree_c.py @@ -1,36 +1,11 @@  # xml.etree test for cElementTree - +import sys, struct  from test import support -from test.support import bigmemtest, _2G +from test.support import import_fresh_module  import unittest -cET = support.import_module('xml.etree.cElementTree') - - -# cElementTree specific tests - -def sanity(): -    r""" -    Import sanity. - -    >>> from xml.etree import cElementTree - -    Issue #6697. - -    >>> e = cElementTree.Element('a') -    >>> getattr(e, '\uD800')           # doctest: +ELLIPSIS -    Traceback (most recent call last): -      ... -    UnicodeEncodeError: ... - -    >>> p = cElementTree.XMLParser() -    >>> p.version.split()[0] -    'Expat' -    >>> getattr(p, '\uD800') -    Traceback (most recent call last): -     ... -    AttributeError: 'XMLParser' object has no attribute '\ud800' -    """ +cET = import_fresh_module('xml.etree.ElementTree', fresh=['_elementtree']) +cET_alias = import_fresh_module('xml.etree.cElementTree', fresh=['_elementtree', 'xml.etree'])  class MiscTests(unittest.TestCase): @@ -47,27 +22,72 @@ class MiscTests(unittest.TestCase):              data = None +@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') + + +@unittest.skipUnless(cET, 'requires _elementtree') +class TestAcceleratorImported(unittest.TestCase): +    # Test that the C accelerator was imported, as expected +    def test_correct_import_cET(self): +        self.assertEqual(cET.SubElement.__module__, '_elementtree') + +    def test_correct_import_cET_alias(self): +        self.assertEqual(cET_alias.SubElement.__module__, '_elementtree') + + +@unittest.skipUnless(cET, 'requires _elementtree') +class SizeofTest(unittest.TestCase): +    def setUp(self): +        import _testcapi +        gc_headsize = _testcapi.SIZEOF_PYGC_HEAD +        # object header +        header = 'PP' +        if hasattr(sys, "gettotalrefcount"): +            # debug header +            header = 'PP' + header +        # fields +        element = header + '5P' +        self.elementsize = gc_headsize + struct.calcsize(element) +        # extra +        self.extra = struct.calcsize('PiiP4P') + +    def test_element(self): +        e = cET.Element('a') +        self.assertEqual(sys.getsizeof(e), self.elementsize) + +    def test_element_with_attrib(self): +        e = cET.Element('a', href='about:') +        self.assertEqual(sys.getsizeof(e), +                         self.elementsize + self.extra) + +    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 +        self.assertEqual(sys.getsizeof(e), +                         self.elementsize + self.extra + +                         struct.calcsize('8P')) +  def test_main():      from test import test_xml_etree, test_xml_etree_c      # Run the tests specific to the C implementation -    support.run_doctest(test_xml_etree_c, verbosity=True) - -    support.run_unittest(MiscTests) - -    # Assign the C implementation before running the doctests -    # Patch the __name__, to prevent confusion with the pure Python test -    pyET = test_xml_etree.ET -    py__name__ = test_xml_etree.__name__ -    test_xml_etree.ET = cET -    if __name__ != '__main__': -        test_xml_etree.__name__ = __name__ -    try: -        # Run the same test suite as xml.etree.ElementTree -        test_xml_etree.test_main(module_name='xml.etree.cElementTree') -    finally: -        test_xml_etree.ET = pyET -        test_xml_etree.__name__ = py__name__ +    support.run_unittest( +        MiscTests, +        TestAliasWorking, +        TestAcceleratorImported, +        SizeofTest, +        ) + +    # Run the same test suite as the Python module +    test_xml_etree.test_main(module=cET) +  if __name__ == '__main__':      test_main()  | 
