diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/xml/etree/ElementTree.py | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index 3981659..be6cf11 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -1184,23 +1184,29 @@ def tostring(element, encoding=None, method=None): # @defreturn sequence # @since 1.3 -def tostringlist(element, encoding=None, method=None): - data = [] - class DataStream(io.BufferedIOBase): - def writable(self): - return True +class _ListDataStream(io.BufferedIOBase): + """ An auxiliary stream accumulating into a list reference + """ + def __init__(self, lst): + self.lst = lst + + def writable(self): + return True - def seekable(self): - return True + def seekable(self): + return True - def write(self, b): - data.append(b) + def write(self, b): + self.lst.append(b) - def tell(self): - return len(data) + def tell(self): + return len(self.lst) - ElementTree(element).write(DataStream(), encoding, method=method) - return data +def tostringlist(element, encoding=None, method=None): + lst = [] + stream = _ListDataStream(lst) + ElementTree(element).write(stream, encoding, method=method) + return lst ## # Writes an element tree or element structure to sys.stdout. This |