diff options
author | Eli Bendersky <eliben@gmail.com> | 2012-07-17 12:09:12 (GMT) |
---|---|---|
committer | Eli Bendersky <eliben@gmail.com> | 2012-07-17 12:09:12 (GMT) |
commit | 43cc5f29a8666404255c9d1619c1aef0fe817486 (patch) | |
tree | ba85b1377dba2602fcd47f7c102e7bf0d2a4315e /Lib/xml/etree/ElementTree.py | |
parent | 94554921421145569bfa4319e0a0610afdb68c78 (diff) | |
download | cpython-43cc5f29a8666404255c9d1619c1aef0fe817486.zip cpython-43cc5f29a8666404255c9d1619c1aef0fe817486.tar.gz cpython-43cc5f29a8666404255c9d1619c1aef0fe817486.tar.bz2 |
Optimize tostringlist by taking the stream class outside the function. It's now 2x faster on short calls. Related to #1767933
Diffstat (limited to 'Lib/xml/etree/ElementTree.py')
-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 |