[Python-checkins] cpython (3.3): Issue #16913: Fix Element.itertext()'s handling of text with XML entities.

eli.bendersky python-checkins at python.org
Thu Jan 10 15:31:24 CET 2013


http://hg.python.org/cpython/rev/d965ff47cf94
changeset:   81363:d965ff47cf94
branch:      3.3
parent:      81361:fe4f334056bd
user:        Eli Bendersky <eliben at gmail.com>
date:        Thu Jan 10 06:27:53 2013 -0800
summary:
  Issue #16913: Fix Element.itertext()'s handling of text with XML entities.

Patch by Serhiy Storchaka

files:
  Lib/test/test_xml_etree.py |   4 ++++
  Misc/NEWS                  |   2 ++
  Modules/_elementtree.c     |  17 ++++++++++++++---
  3 files changed, 20 insertions(+), 3 deletions(-)


diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -1904,6 +1904,10 @@
         tree = ET.ElementTree(None)
         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):
         # single root, no subelements
         a = ET.Element('a')
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -348,6 +348,8 @@
 - Issue #16089: Allow ElementTree.TreeBuilder to work again with a non-Element
   element_factory (fixes a regression in SimpleTAL).
 
+- Issue #16913: Fix Element.itertext()'s handling of text with XML entities.
+
 - Issue #16034: Fix performance regressions in the new `bz2.BZ2File`
   implementation.  Initial patch by Serhiy Storchaka.
 
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -2017,7 +2017,9 @@
                     PyObject_RichCompareBool(it->root_element->tag,
                                              it->sought_tag, Py_EQ) == 1) {
                     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)) {
                             Py_INCREF(text);
                             return text;
@@ -2047,7 +2049,9 @@
             }
 
             if (it->gettext) {
-                PyObject *text = JOIN_OBJ(child->text);
+                PyObject *text = element_get_text(child);
+                if (!text)
+                    return NULL;
                 if (PyObject_IsTrue(text)) {
                     Py_INCREF(text);
                     return text;
@@ -2062,8 +2066,15 @@
                 continue;
         }
         else {
-            PyObject *tail = it->gettext ? JOIN_OBJ(cur_parent->tail) : Py_None;            
+            PyObject *tail;
             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);
             PyObject_Free(it->parent_stack);
             it->parent_stack = next;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list