Commit 43cc5f29 authored by Eli Bendersky's avatar Eli Bendersky

Optimize tostringlist by taking the stream class outside the function. It's...

Optimize tostringlist by taking the stream class outside the function. It's now 2x faster on short calls. Related to #1767933
parent 94554921
......@@ -1184,9 +1184,12 @@ def tostring(element, encoding=None, method=None):
# @defreturn sequence
# @since 1.3
def tostringlist(element, encoding=None, method=None):
data = []
class DataStream(io.BufferedIOBase):
class _ListDataStream(io.BufferedIOBase):
""" An auxiliary stream accumulating into a list reference
"""
def __init__(self, lst):
self.lst = lst
def writable(self):
return True
......@@ -1194,13 +1197,16 @@ def tostringlist(element, encoding=None, method=None):
return True
def write(self, b):
data.append(b)
self.lst.append(b)
def tell(self):
return len(data)
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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment