Commit 092af1fc authored by Eli Bendersky's avatar Eli Bendersky

Issue #14128: Exposing Element as an actual type from _elementtree, rather than a factory function.

This makes the C implementation more aligned with the Python implementation.
Also added some tests to ensure that Element is now a type and that it can
be subclassed.
parent c9590ad7
......@@ -1901,16 +1901,51 @@ class CleanContext(object):
class TestAcceleratorNotImported(unittest.TestCase):
# Test that the C accelerator was not imported for pyET
def test_correct_import_pyET(self):
self.assertEqual(pyET.Element.__module__, 'xml.etree.ElementTree')
self.assertEqual(pyET.SubElement.__module__, 'xml.etree.ElementTree')
class TestElementClass(unittest.TestCase):
def test_Element_is_a_type(self):
self.assertIsInstance(ET.Element, type)
def test_Element_subclass_trivial(self):
class MyElement(ET.Element):
pass
mye = MyElement('foo')
self.assertIsInstance(mye, ET.Element)
self.assertIsInstance(mye, MyElement)
self.assertEqual(mye.tag, 'foo')
def test_Element_subclass_constructor(self):
class MyElement(ET.Element):
def __init__(self, tag, attrib={}, **extra):
super(MyElement, self).__init__(tag + '__', attrib, **extra)
mye = MyElement('foo', {'a': 1, 'b': 2}, c=3, d=4)
self.assertEqual(mye.tag, 'foo__')
self.assertEqual(sorted(mye.items()),
[('a', 1), ('b', 2), ('c', 3), ('d', 4)])
def test_Element_subclass_new_method(self):
class MyElement(ET.Element):
def newmethod(self):
return self.tag
mye = MyElement('joe')
self.assertEqual(mye.newmethod(), 'joe')
def test_main(module=pyET):
from test import test_xml_etree
# Run the tests specific to the Python implementation
support.run_unittest(TestAcceleratorNotImported)
# The same doctests are used for both the Python and the C implementations
test_xml_etree.ET = module
support.run_unittest(TestAcceleratorNotImported)
support.run_unittest(TestElementClass)
# XXX the C module should give the same warnings as the Python module
with CleanContext(quiet=(module is not pyET)):
......
......@@ -46,14 +46,22 @@ class MiscTests(unittest.TestCase):
finally:
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.Element.__module__, '_elementtree')
self.assertEqual(cET.SubElement.__module__, '_elementtree')
def test_correct_import_cET_alias(self):
self.assertEqual(cET_alias.Element.__module__, '_elementtree')
self.assertEqual(cET_alias.SubElement.__module__, '_elementtree')
def test_main():
......@@ -61,13 +69,15 @@ def test_main():
# Run the tests specific to the C implementation
support.run_doctest(test_xml_etree_c, verbosity=True)
support.run_unittest(MiscTests, TestAcceleratorImported)
support.run_unittest(
MiscTests,
TestAliasWorking,
TestAcceleratorImported
)
# Run the same test suite as the Python module
test_xml_etree.test_main(module=cET)
# Exercise the deprecated alias
test_xml_etree.test_main(module=cET_alias)
if __name__ == '__main__':
test_main()
......@@ -101,7 +101,6 @@ import sys
import re
import warnings
class _SimpleElementPath:
# emulate pre-1.2 find/findtext/findall behaviour
def find(self, element, tag, namespaces=None):
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment