From b217020399ae17631f44d782bc35b137df82a506 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Tue, 12 Apr 2022 17:42:45 -0500 Subject: [PATCH 1/3] Fix findtext to only give an empty string on None The API documentation for it states that this function gives back an empty string on "no text content." With the previous implementation, this would give back an emtpy string even on text content values such as 0 or False. This attempts to resolve that by only giving back an empty string if the text attribute is set to None. --- Lib/test/test_xml_etree.py | 16 ++++++++++++++++ Lib/xml/etree/ElementPath.py | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 60a41506d8795df..0275da81e8d12c5 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -2735,6 +2735,22 @@ def test_findtext_with_error(self): 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 cd3c354d0813ee4..c2ad7509b2f9381 100644 --- a/Lib/xml/etree/ElementPath.py +++ b/Lib/xml/etree/ElementPath.py @@ -416,6 +416,10 @@ 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 From 484a46a75378c279b005c1c0e8f66254e211f513 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Tue, 12 Apr 2022 18:05:44 -0500 Subject: [PATCH 2/3] Add news blurb --- .../next/Library/2022-04-12-18-05-40.gh-issue-91447.N_Fs4H.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-04-12-18-05-40.gh-issue-91447.N_Fs4H.rst 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 000000000000000..6f9be2d3e9be416 --- /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. From 47f4e10a9b2e0f138c20fe8133ad43bfa206add4 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 1 Aug 2022 09:28:50 -0700 Subject: [PATCH 3/3] Remove redundant blank lines --- Lib/test/test_xml_etree.py | 2 -- Lib/xml/etree/ElementPath.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 0275da81e8d12c5..54dc9193870aec7 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -2739,7 +2739,6 @@ 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) @@ -2748,7 +2747,6 @@ 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): diff --git a/Lib/xml/etree/ElementPath.py b/Lib/xml/etree/ElementPath.py index c2ad7509b2f9381..dc6bd28c03137de 100644 --- a/Lib/xml/etree/ElementPath.py +++ b/Lib/xml/etree/ElementPath.py @@ -416,10 +416,8 @@ def findall(elem, path, namespaces=None): def findtext(elem, path, default=None, namespaces=None): try: elem = next(iterfind(elem, path, namespaces)) - if elem.text is None: return "" - return elem.text except StopIteration: return default