From 25771b3749cac588ad2ecc8fc796a496d35769e5 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sun, 13 Jan 2013 05:26:07 -0800 Subject: [PATCH] Issue #16922: fixed findtext() to return empty Unicode string instead of empty bytes object when there's no text. Patch by Serhiy Storchaka. --- Lib/test/test_xml_etree.py | 3 +++ Modules/_elementtree.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 59a85081d81..b4edd7476f9 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -1773,6 +1773,9 @@ class ElementFindTest(unittest.TestCase): self.assertIsNone(e.findtext('tog')) self.assertEqual(e.findtext('tog', 'default'), 'default') + # Issue #16922 + self.assertEqual(ET.XML('').findtext('empty'), '') + def test_findall(self): e = ET.XML(SAMPLE_XML) e[2] = ET.XML(SAMPLE_SECTION) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 274c65ed1eb..49441cc827e 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -1129,7 +1129,7 @@ element_findtext(ElementObject *self, PyObject *args, PyObject *kwds) PyObject* text = element_get_text(item); if (text == Py_None) - return PyBytes_FromString(""); + return PyUnicode_New(0, 0); Py_XINCREF(text); return text; }