Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6174ca

Browse files
committed
Issue #16913: Fix Element.itertext()'s handling of text with XML entities.
Patch by Serhiy Storchaka
1 parent 458c0d5 commit e6174ca

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

Lib/test/test_xml_etree.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,6 +1904,10 @@ def test_basic(self):
19041904
tree = ET.ElementTree(None)
19051905
self.assertRaises(AttributeError, tree.iter)
19061906

1907+
# Issue #16913
1908+
doc = ET.XML("<root>a&amp;<sub>b&amp;</sub>c&amp;</root>")
1909+
self.assertEqual(''.join(doc.itertext()), 'a&b&c&')
1910+
19071911
def test_corners(self):
19081912
# single root, no subelements
19091913
a = ET.Element('a')

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ Library
348348
- Issue #16089: Allow ElementTree.TreeBuilder to work again with a non-Element
349349
element_factory (fixes a regression in SimpleTAL).
350350

351+
- Issue #16913: Fix Element.itertext()'s handling of text with XML entities.
352+
351353
- Issue #16034: Fix performance regressions in the new `bz2.BZ2File`
352354
implementation. Initial patch by Serhiy Storchaka.
353355

Modules/_elementtree.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,7 +2017,9 @@ elementiter_next(ElementIterObject *it)
20172017
PyObject_RichCompareBool(it->root_element->tag,
20182018
it->sought_tag, Py_EQ) == 1) {
20192019
if (it->gettext) {
2020-
PyObject *text = JOIN_OBJ(it->root_element->text);
2020+
PyObject *text = element_get_text(it->root_element);
2021+
if (!text)
2022+
return NULL;
20212023
if (PyObject_IsTrue(text)) {
20222024
Py_INCREF(text);
20232025
return text;
@@ -2047,7 +2049,9 @@ elementiter_next(ElementIterObject *it)
20472049
}
20482050

20492051
if (it->gettext) {
2050-
PyObject *text = JOIN_OBJ(child->text);
2052+
PyObject *text = element_get_text(child);
2053+
if (!text)
2054+
return NULL;
20512055
if (PyObject_IsTrue(text)) {
20522056
Py_INCREF(text);
20532057
return text;
@@ -2062,8 +2066,15 @@ elementiter_next(ElementIterObject *it)
20622066
continue;
20632067
}
20642068
else {
2065-
PyObject *tail = it->gettext ? JOIN_OBJ(cur_parent->tail) : Py_None;
2069+
PyObject *tail;
20662070
ParentLocator *next = it->parent_stack->next;
2071+
if (it->gettext) {
2072+
tail = element_get_tail(cur_parent);
2073+
if (!tail)
2074+
return NULL;
2075+
}
2076+
else
2077+
tail = Py_None;
20672078
Py_XDECREF(it->parent_stack->parent);
20682079
PyObject_Free(it->parent_stack);
20692080
it->parent_stack = next;

0 commit comments

Comments
 (0)