From 426e248febc4b86ea4512baaaa81966ab54c83da Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Tue, 17 Jul 2012 05:45:11 +0300 Subject: Preserve the invariant tostring(elem) == b''.join(tostringlist(elem)) and add a test to make sure it keeps working --- Lib/test/test_xml_etree.py | 9 +++++++++ Lib/xml/etree/ElementTree.py | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index d90f978..da1ad09 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -2286,6 +2286,15 @@ class IOTest(unittest.TestCase): '''\n''' ''''''.encode("utf-16")) + def test_tostringlist_invariant(self): + root = ET.fromstring('foo') + self.assertEqual( + ET.tostring(root, 'unicode'), + ''.join(ET.tostringlist(root, 'unicode'))) + self.assertEqual( + ET.tostring(root, 'utf-16'), + b''.join(ET.tostringlist(root, 'utf-16'))) + class ParseErrorTest(unittest.TestCase): def test_subclass(self): diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index 10bf849..3981659 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -1190,9 +1190,15 @@ def tostringlist(element, encoding=None, method=None): def writable(self): return True + def seekable(self): + return True + def write(self, b): data.append(b) + def tell(self): + return len(data) + ElementTree(element).write(DataStream(), encoding, method=method) return data -- cgit v0.12