Commit 0762dbc1 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 79170005
...@@ -1184,9 +1184,12 @@ def tostring(element, encoding=None, method=None): ...@@ -1184,9 +1184,12 @@ def tostring(element, encoding=None, method=None):
# @defreturn sequence # @defreturn sequence
# @since 1.3 # @since 1.3
def tostringlist(element, encoding=None, method=None): class _ListDataStream(io.BufferedIOBase):
data = [] """ An auxiliary stream accumulating into a list reference
class DataStream(io.BufferedIOBase): """
def __init__(self, lst):
self.lst = lst
def writable(self): def writable(self):
return True return True
...@@ -1194,13 +1197,16 @@ def tostringlist(element, encoding=None, method=None): ...@@ -1194,13 +1197,16 @@ def tostringlist(element, encoding=None, method=None):
return True return True
def write(self, b): def write(self, b):
data.append(b) self.lst.append(b)
def tell(self): def tell(self):
return len(data) return len(self.lst)
ElementTree(element).write(DataStream(), encoding, method=method) def tostringlist(element, encoding=None, method=None):
return data lst = []
stream = _ListDataStream(lst)
ElementTree(element).write(stream, encoding, method=method)
return lst
## ##
# Writes an element tree or element structure to sys.stdout. This # 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