Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,20 @@ 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')])
Expand Down
4 changes: 3 additions & 1 deletion Lib/xml/etree/ElementPath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix findtext in the xml module to only give an empty string when the text
attribute is set to None.