Skip to content

Commit 092af1f

Browse files
committed
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.
1 parent c9590ad commit 092af1f

4 files changed

Lines changed: 198 additions & 91 deletions

File tree

Lib/test/test_xml_etree.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,16 +1901,51 @@ def __exit__(self, *args):
19011901
class TestAcceleratorNotImported(unittest.TestCase):
19021902
# Test that the C accelerator was not imported for pyET
19031903
def test_correct_import_pyET(self):
1904-
self.assertEqual(pyET.Element.__module__, 'xml.etree.ElementTree')
1904+
self.assertEqual(pyET.SubElement.__module__, 'xml.etree.ElementTree')
1905+
1906+
1907+
class TestElementClass(unittest.TestCase):
1908+
def test_Element_is_a_type(self):
1909+
self.assertIsInstance(ET.Element, type)
1910+
1911+
def test_Element_subclass_trivial(self):
1912+
class MyElement(ET.Element):
1913+
pass
1914+
1915+
mye = MyElement('foo')
1916+
self.assertIsInstance(mye, ET.Element)
1917+
self.assertIsInstance(mye, MyElement)
1918+
self.assertEqual(mye.tag, 'foo')
1919+
1920+
def test_Element_subclass_constructor(self):
1921+
class MyElement(ET.Element):
1922+
def __init__(self, tag, attrib={}, **extra):
1923+
super(MyElement, self).__init__(tag + '__', attrib, **extra)
1924+
1925+
mye = MyElement('foo', {'a': 1, 'b': 2}, c=3, d=4)
1926+
self.assertEqual(mye.tag, 'foo__')
1927+
self.assertEqual(sorted(mye.items()),
1928+
[('a', 1), ('b', 2), ('c', 3), ('d', 4)])
1929+
1930+
def test_Element_subclass_new_method(self):
1931+
class MyElement(ET.Element):
1932+
def newmethod(self):
1933+
return self.tag
1934+
1935+
mye = MyElement('joe')
1936+
self.assertEqual(mye.newmethod(), 'joe')
19051937

19061938

19071939
def test_main(module=pyET):
19081940
from test import test_xml_etree
19091941

1942+
# Run the tests specific to the Python implementation
1943+
support.run_unittest(TestAcceleratorNotImported)
1944+
19101945
# The same doctests are used for both the Python and the C implementations
19111946
test_xml_etree.ET = module
19121947

1913-
support.run_unittest(TestAcceleratorNotImported)
1948+
support.run_unittest(TestElementClass)
19141949

19151950
# XXX the C module should give the same warnings as the Python module
19161951
with CleanContext(quiet=(module is not pyET)):

Lib/test/test_xml_etree_c.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,38 @@ def test_length_overflow(self, size):
4646
finally:
4747
data = None
4848

49+
@unittest.skipUnless(cET, 'requires _elementtree')
50+
class TestAliasWorking(unittest.TestCase):
51+
# Test that the cET alias module is alive
52+
def test_alias_working(self):
53+
e = cET_alias.Element('foo')
54+
self.assertEqual(e.tag, 'foo')
55+
56+
4957
@unittest.skipUnless(cET, 'requires _elementtree')
5058
class TestAcceleratorImported(unittest.TestCase):
5159
# Test that the C accelerator was imported, as expected
5260
def test_correct_import_cET(self):
53-
self.assertEqual(cET.Element.__module__, '_elementtree')
61+
self.assertEqual(cET.SubElement.__module__, '_elementtree')
5462

5563
def test_correct_import_cET_alias(self):
56-
self.assertEqual(cET_alias.Element.__module__, '_elementtree')
64+
self.assertEqual(cET_alias.SubElement.__module__, '_elementtree')
5765

5866

5967
def test_main():
6068
from test import test_xml_etree, test_xml_etree_c
6169

6270
# Run the tests specific to the C implementation
6371
support.run_doctest(test_xml_etree_c, verbosity=True)
64-
65-
support.run_unittest(MiscTests, TestAcceleratorImported)
72+
support.run_unittest(
73+
MiscTests,
74+
TestAliasWorking,
75+
TestAcceleratorImported
76+
)
6677

6778
# Run the same test suite as the Python module
6879
test_xml_etree.test_main(module=cET)
69-
# Exercise the deprecated alias
70-
test_xml_etree.test_main(module=cET_alias)
80+
7181

7282
if __name__ == '__main__':
7383
test_main()

Lib/xml/etree/ElementTree.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
import re
102102
import warnings
103103

104-
105104
class _SimpleElementPath:
106105
# emulate pre-1.2 find/findtext/findall behaviour
107106
def find(self, element, tag, namespaces=None):

0 commit comments

Comments
 (0)