summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2022-08-01 16:52:39 (GMT)
committerGitHub <noreply@github.com>2022-08-01 16:52:39 (GMT)
commita95e60db748ec6f2c19b5710c11f62e1e4d669f4 (patch)
treef8604f233cab3f2839bf53c59c086841580e211d
parent858c9a58bf56cefc792bf0eb1ba22984b7b2d150 (diff)
downloadcpython-a95e60db748ec6f2c19b5710c11f62e1e4d669f4.zip
cpython-a95e60db748ec6f2c19b5710c11f62e1e4d669f4.tar.gz
cpython-a95e60db748ec6f2c19b5710c11f62e1e4d669f4.tar.bz2
gh-91447: Fix findtext to only give an empty string on None (GH-91486)
The API documentation for [findtext](https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.findtext) states that this function gives back an empty string on "no text content." With the previous implementation, this would give back a empty string even on text content values such as 0 or False. This patch attempts to resolve that by only giving back an empty string if the text attribute is set to `None`. Resolves #91447. Automerge-Triggered-By: GH:gvanrossum
-rw-r--r--Lib/test/test_xml_etree.py14
-rw-r--r--Lib/xml/etree/ElementPath.py4
-rw-r--r--Misc/NEWS.d/next/Library/2022-04-12-18-05-40.gh-issue-91447.N_Fs4H.rst2
3 files changed, 19 insertions, 1 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index 8399556..ee3154e 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -2705,6 +2705,20 @@ class BadElementPathTest(ElementTestCase, unittest.TestCase):
except ZeroDivisionError:
pass
+ def test_findtext_with_falsey_text_attribute(self):
+ root_elem = ET.Element('foo')
+ sub_elem = ET.SubElement(root_elem, 'bar')
+ falsey = ["", 0, False, [], (), {}]
+ for val in falsey:
+ sub_elem.text = val
+ self.assertEqual(root_elem.findtext('./bar'), val)
+
+ def test_findtext_with_none_text_attribute(self):
+ root_elem = ET.Element('foo')
+ sub_elem = ET.SubElement(root_elem, 'bar')
+ sub_elem.text = None
+ self.assertEqual(root_elem.findtext('./bar'), '')
+
def test_findall_with_mutating(self):
e = ET.Element('foo')
e.extend([ET.Element('bar')])
diff --git a/Lib/xml/etree/ElementPath.py b/Lib/xml/etree/ElementPath.py
index cd3c354..dc6bd28 100644
--- a/Lib/xml/etree/ElementPath.py
+++ b/Lib/xml/etree/ElementPath.py
@@ -416,6 +416,8 @@ def findall(elem, path, namespaces=None):
def findtext(elem, path, default=None, namespaces=None):
try:
elem = next(iterfind(elem, path, namespaces))
- return elem.text or ""
+ if elem.text is None:
+ return ""
+ return elem.text
except StopIteration:
return default
diff --git a/Misc/NEWS.d/next/Library/2022-04-12-18-05-40.gh-issue-91447.N_Fs4H.rst b/Misc/NEWS.d/next/Library/2022-04-12-18-05-40.gh-issue-91447.N_Fs4H.rst
new file mode 100644
index 0000000..6f9be2d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-04-12-18-05-40.gh-issue-91447.N_Fs4H.rst
@@ -0,0 +1,2 @@
+Fix findtext in the xml module to only give an empty string when the text
+attribute is set to None.