Preserve the invariant tostring(elem) == b''.join(tostringlist(elem)) and add a test to make sure it keeps working

This commit is contained in:
Eli Bendersky 2012-07-17 05:45:11 +03:00
parent daa4c6b2e6
commit 426e248feb
2 changed files with 15 additions and 0 deletions

View File

@ -2286,6 +2286,15 @@ class IOTest(unittest.TestCase):
'''<?xml version='1.0' encoding='utf-16'?>\n''' '''<?xml version='1.0' encoding='utf-16'?>\n'''
'''<site />'''.encode("utf-16")) '''<site />'''.encode("utf-16"))
def test_tostringlist_invariant(self):
root = ET.fromstring('<tag>foo</tag>')
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): class ParseErrorTest(unittest.TestCase):
def test_subclass(self): def test_subclass(self):

View File

@ -1190,9 +1190,15 @@ def tostringlist(element, encoding=None, method=None):
def writable(self): def writable(self):
return True return True
def seekable(self):
return True
def write(self, b): def write(self, b):
data.append(b) data.append(b)
def tell(self):
return len(data)
ElementTree(element).write(DataStream(), encoding, method=method) ElementTree(element).write(DataStream(), encoding, method=method)
return data return data