[Biopython-dev] Python 2 and 3 migration thoughts

Peter Cock p.j.a.cock at googlemail.com
Mon Oct 14 19:36:21 EDT 2013


On Mon, Oct 14, 2013 at 9:34 PM, Eric Talevich  wrote:
> On Mon, Oct 14, 2013 at 8:00 AM, Peter Cock  wrote:
>
>>
>> [...] something far more common is
>>
>> StringIO, which also crops up in our doctests. e.g.
>>
>> Python 2 only:
>>
>> >>> from StringIO import StringIO
>>
>> Both 2 and 3:
>>
>> >>> try:
>> ...     from StringIO import StringIO # Python 2
>> ... except ImportError:
>> ...     from io import StringIO # Python 3
>> ...
>
> For the case of StringIO and BytesIO, the top-level
> io module was added in Python 2.6:
> http://docs.python.org/2/library/io.html

Yes, but under Python 2 io.StringIO is unicode
based, while StringIO.StringIO & cStringIO.StringIO
are (bytes) string based. It isn't a drop in replacement
for our text based parsers.

Where we do want byte strings (e.g. binary formats
like SFF) then we can and now do use io.BytesIO
in place of the old StringIO usage - and that then
works nicely without change under Python3 as
well.

> In Py2.6, the implementation of io.StringIO is
> slow, and io.BytesIO not meaningfully different
> from io.StringIO, but both should be fine for
> doctests.

Yeah - for doctests the speed of the different
StringIO options is immaterial.

Peter


More information about the Biopython-dev mailing list