Skip to content

Commit fd29203

Browse files
committed
align C implementation with Python implementation as much as possible
1 parent 4b74caf commit fd29203

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

Modules/_elementtree.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,24 +1633,21 @@ static PyObject *
16331633
_elementtree_Element_remove_impl(ElementObject *self, PyObject *subelement)
16341634
/*[clinic end generated code: output=38fe6c07d6d87d1f input=6133e1d05597d5ee]*/
16351635
{
1636-
if (self->extra == NULL) {
1637-
/* element has no children, so raise exception */
1638-
goto error;
1639-
}
1640-
16411636
Py_ssize_t i;
16421637
// When iterating over the list of children, we need to check that the
16431638
// list is not cleared (self->extra != NULL) and that we are still within
16441639
// the correct bounds (i < self->extra->length).
16451640
//
16461641
// We deliberately avoid protecting against children lists that grow
16471642
// faster than the index since list objects do not protect against it.
1643+
int rc = 0;
16481644
for (i = 0; self->extra && i < self->extra->length; i++) {
16491645
if (self->extra->children[i] == subelement) {
1646+
rc = 1;
16501647
break;
16511648
}
16521649
PyObject *child = Py_NewRef(self->extra->children[i]);
1653-
int rc = PyObject_RichCompareBool(child, subelement, Py_EQ);
1650+
rc = PyObject_RichCompareBool(child, subelement, Py_EQ);
16541651
Py_DECREF(child);
16551652
if (rc < 0) {
16561653
return NULL;
@@ -1660,11 +1657,16 @@ _elementtree_Element_remove_impl(ElementObject *self, PyObject *subelement)
16601657
}
16611658
}
16621659

1660+
if (rc == 0) {
1661+
PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
1662+
return NULL;
1663+
}
1664+
16631665
// An extra check must be done if the mutation occurs at the very last
16641666
// step and removes or clears the 'extra' list (the condition on the
16651667
// length would not be satisfied any more).
16661668
if (self->extra == NULL || i >= self->extra->length) {
1667-
goto error;
1669+
Py_RETURN_NONE;
16681670
}
16691671

16701672
PyObject *found = self->extra->children[i];
@@ -1676,10 +1678,6 @@ _elementtree_Element_remove_impl(ElementObject *self, PyObject *subelement)
16761678

16771679
Py_DECREF(found);
16781680
Py_RETURN_NONE;
1679-
1680-
error:
1681-
PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
1682-
return NULL;
16831681
}
16841682

16851683
static PyObject*

0 commit comments

Comments
 (0)