mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Issue #16913: Fix Element.itertext()'s handling of text with XML entities.
Patch by Serhiy Storchaka
This commit is contained in:
commit
bb48151c8c
2 changed files with 18 additions and 3 deletions
|
@ -1904,6 +1904,10 @@ class ElementIterTest(unittest.TestCase):
|
||||||
tree = ET.ElementTree(None)
|
tree = ET.ElementTree(None)
|
||||||
self.assertRaises(AttributeError, tree.iter)
|
self.assertRaises(AttributeError, tree.iter)
|
||||||
|
|
||||||
|
# Issue #16913
|
||||||
|
doc = ET.XML("<root>a&<sub>b&</sub>c&</root>")
|
||||||
|
self.assertEqual(''.join(doc.itertext()), 'a&b&c&')
|
||||||
|
|
||||||
def test_corners(self):
|
def test_corners(self):
|
||||||
# single root, no subelements
|
# single root, no subelements
|
||||||
a = ET.Element('a')
|
a = ET.Element('a')
|
||||||
|
|
|
@ -2017,7 +2017,9 @@ elementiter_next(ElementIterObject *it)
|
||||||
PyObject_RichCompareBool(it->root_element->tag,
|
PyObject_RichCompareBool(it->root_element->tag,
|
||||||
it->sought_tag, Py_EQ) == 1) {
|
it->sought_tag, Py_EQ) == 1) {
|
||||||
if (it->gettext) {
|
if (it->gettext) {
|
||||||
PyObject *text = JOIN_OBJ(it->root_element->text);
|
PyObject *text = element_get_text(it->root_element);
|
||||||
|
if (!text)
|
||||||
|
return NULL;
|
||||||
if (PyObject_IsTrue(text)) {
|
if (PyObject_IsTrue(text)) {
|
||||||
Py_INCREF(text);
|
Py_INCREF(text);
|
||||||
return text;
|
return text;
|
||||||
|
@ -2047,7 +2049,9 @@ elementiter_next(ElementIterObject *it)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (it->gettext) {
|
if (it->gettext) {
|
||||||
PyObject *text = JOIN_OBJ(child->text);
|
PyObject *text = element_get_text(child);
|
||||||
|
if (!text)
|
||||||
|
return NULL;
|
||||||
if (PyObject_IsTrue(text)) {
|
if (PyObject_IsTrue(text)) {
|
||||||
Py_INCREF(text);
|
Py_INCREF(text);
|
||||||
return text;
|
return text;
|
||||||
|
@ -2062,8 +2066,15 @@ elementiter_next(ElementIterObject *it)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyObject *tail = it->gettext ? JOIN_OBJ(cur_parent->tail) : Py_None;
|
PyObject *tail;
|
||||||
ParentLocator *next = it->parent_stack->next;
|
ParentLocator *next = it->parent_stack->next;
|
||||||
|
if (it->gettext) {
|
||||||
|
tail = element_get_tail(cur_parent);
|
||||||
|
if (!tail)
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
tail = Py_None;
|
||||||
Py_XDECREF(it->parent_stack->parent);
|
Py_XDECREF(it->parent_stack->parent);
|
||||||
PyObject_Free(it->parent_stack);
|
PyObject_Free(it->parent_stack);
|
||||||
it->parent_stack = next;
|
it->parent_stack = next;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue