Skip to content

Commit b11c566

Browse files
bpo-34941: Fix searching Element subclasses. (GH-9766)
Methods find(), findtext() and findall() of xml.etree.ElementTree.Element were not able to find chldren which are instances of Element subclasses.
1 parent d274afb commit b11c566

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

Lib/test/test_xml_etree.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,6 +2160,21 @@ def newmethod(self):
21602160
mye = MyElement('joe')
21612161
self.assertEqual(mye.newmethod(), 'joe')
21622162

2163+
def test_Element_subclass_find(self):
2164+
class MyElement(ET.Element):
2165+
pass
2166+
2167+
e = ET.Element('foo')
2168+
e.text = 'text'
2169+
sub = MyElement('bar')
2170+
sub.text = 'subtext'
2171+
e.append(sub)
2172+
self.assertEqual(e.findtext('bar'), 'subtext')
2173+
self.assertEqual(e.find('bar').tag, 'bar')
2174+
found = list(e.findall('bar'))
2175+
self.assertEqual(len(found), 1, found)
2176+
self.assertEqual(found[0].tag, 'bar')
2177+
21632178

21642179
class ElementFindTest(unittest.TestCase):
21652180
def test_find_simple(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Methods ``find()``, ``findtext()`` and ``findall()`` of the ``Element``
2+
class in the :mod:`xml.etree.ElementTree` module are now able to find
3+
children which are instances of ``Element`` subclasses.

Modules/_elementtree.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ typedef struct {
204204

205205

206206
#define Element_CheckExact(op) (Py_TYPE(op) == &Element_Type)
207+
#define Element_Check(op) PyObject_TypeCheck(op, &Element_Type)
208+
207209

208210
/* -------------------------------------------------------------------- */
209211
/* Element constructors and destructor */
@@ -1132,7 +1134,7 @@ _elementtree_Element_extend(ElementObject *self, PyObject *elements)
11321134
for (i = 0; i < PySequence_Fast_GET_SIZE(seq); i++) {
11331135
PyObject* element = PySequence_Fast_GET_ITEM(seq, i);
11341136
Py_INCREF(element);
1135-
if (!PyObject_TypeCheck(element, (PyTypeObject *)&Element_Type)) {
1137+
if (!Element_Check(element)) {
11361138
PyErr_Format(
11371139
PyExc_TypeError,
11381140
"expected an Element, not \"%.200s\"",
@@ -1184,7 +1186,7 @@ _elementtree_Element_find_impl(ElementObject *self, PyObject *path,
11841186
for (i = 0; i < self->extra->length; i++) {
11851187
PyObject* item = self->extra->children[i];
11861188
int rc;
1187-
if (!Element_CheckExact(item))
1189+
if (!Element_Check(item))
11881190
continue;
11891191
Py_INCREF(item);
11901192
rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ);
@@ -1229,14 +1231,14 @@ _elementtree_Element_findtext_impl(ElementObject *self, PyObject *path,
12291231
}
12301232

12311233
for (i = 0; i < self->extra->length; i++) {
1232-
ElementObject* item = (ElementObject*) self->extra->children[i];
1234+
PyObject *item = self->extra->children[i];
12331235
int rc;
1234-
if (!Element_CheckExact(item))
1236+
if (!Element_Check(item))
12351237
continue;
12361238
Py_INCREF(item);
1237-
rc = PyObject_RichCompareBool(item->tag, path, Py_EQ);
1239+
rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ);
12381240
if (rc > 0) {
1239-
PyObject* text = element_get_text(item);
1241+
PyObject* text = element_get_text((ElementObject*)item);
12401242
if (text == Py_None) {
12411243
Py_DECREF(item);
12421244
return PyUnicode_New(0, 0);
@@ -1269,13 +1271,12 @@ _elementtree_Element_findall_impl(ElementObject *self, PyObject *path,
12691271
{
12701272
Py_ssize_t i;
12711273
PyObject* out;
1272-
PyObject* tag = path;
12731274
elementtreestate *st = ET_STATE_GLOBAL;
12741275

1275-
if (checkpath(tag) || namespaces != Py_None) {
1276+
if (checkpath(path) || namespaces != Py_None) {
12761277
_Py_IDENTIFIER(findall);
12771278
return _PyObject_CallMethodIdObjArgs(
1278-
st->elementpath_obj, &PyId_findall, self, tag, namespaces, NULL
1279+
st->elementpath_obj, &PyId_findall, self, path, namespaces, NULL
12791280
);
12801281
}
12811282

@@ -1289,10 +1290,10 @@ _elementtree_Element_findall_impl(ElementObject *self, PyObject *path,
12891290
for (i = 0; i < self->extra->length; i++) {
12901291
PyObject* item = self->extra->children[i];
12911292
int rc;
1292-
if (!Element_CheckExact(item))
1293+
if (!Element_Check(item))
12931294
continue;
12941295
Py_INCREF(item);
1295-
rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, tag, Py_EQ);
1296+
rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ);
12961297
if (rc != 0 && (rc < 0 || PyList_Append(out, item) < 0)) {
12971298
Py_DECREF(item);
12981299
Py_DECREF(out);
@@ -2173,7 +2174,7 @@ elementiter_next(ElementIterObject *it)
21732174
continue;
21742175
}
21752176

2176-
if (!PyObject_TypeCheck(extra->children[child_index], &Element_Type)) {
2177+
if (!Element_Check(extra->children[child_index])) {
21772178
PyErr_Format(PyExc_AttributeError,
21782179
"'%.100s' object has no attribute 'iter'",
21792180
Py_TYPE(extra->children[child_index])->tp_name);

0 commit comments

Comments
 (0)