This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients BreamoreBoy, Richard.Urwin, amaury.forgeotdarc, bugok, effbot, eli.bendersky, flox, nnorwitz, python-dev, rurwin, serhiy.storchaka
Date 2012-07-15.07:02:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1342335759.61.0.225517200005.issue1767933@psf.upfronthosting.co.za>
In-reply-to
Content
Thank you, Eli.

However changes to tostring() and tostringlist() break the invariant b"".join(tostringlist(element, 'utf-16')) == tostring(element, 'utf-16'). You should add followed methods to DataStream:

        def seekable(self):
            return True

        def tell(self):
            return len(data)

Note, that monkey-patched version is faster.

    stream = io.BufferedIOBase()
    stream.writable = lambda: True
    stream.write = data.append
    stream.seekable = lambda: True
    stream.tell = data.__len__

Benchmark results:

tostring() with BytesIO:
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root/>')"  "ET.tostring(e, 'utf-16')"
1000 loops, best of 3: 268 usec per loop
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root>'+'<child/>'*100+'</root>' )"  "ET.tostring(e, 'utf-16')"
100 loops, best of 3: 4.63 msec per loop

tostring() with monkey-patching:
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root/>')"  "ET.tostring(e, 'utf-16')"
1000 loops, best of 3: 263 usec per loop
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root>'+'<child/>'*100+'</root>' )"  "ET.tostring(e, 'utf-16')"
100 loops, best of 3: 3.84 msec per loop

tostringlist() with DataStream class:
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root/>')"  "ET.tostringlist(e, 'utf-16')"
1000 loops, best of 3: 624 usec per loop
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root>'+'<child/>'*100+'</root>' )"  "ET.tostringlist(e, 'utf-16')"
100 loops, best of 3: 4.09 msec per loop

tostringlist() with monkey-patching:
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root/>')"  "ET.tostringlist(e, 'utf-16')"1000 loops, best of 3: 259 usec per loop
$ ./python -m timeit -s "import xml.etree.ElementTree as ET; e=ET.XML('<root>'+'<child/>'*100+'</root>' )"  "ET.tostringlist(e, 'utf-16')"
100 loops, best of 3: 3.81 msec per loop
History
Date User Action Args
2012-07-15 07:02:39serhiy.storchakasetrecipients: + serhiy.storchaka, effbot, nnorwitz, amaury.forgeotdarc, bugok, rurwin, eli.bendersky, flox, BreamoreBoy, Richard.Urwin, python-dev
2012-07-15 07:02:39serhiy.storchakasetmessageid: <1342335759.61.0.225517200005.issue1767933@psf.upfronthosting.co.za>
2012-07-15 07:02:39serhiy.storchakalinkissue1767933 messages
2012-07-15 07:02:38serhiy.storchakacreate