I'm currently working in a Python 2 codebase, but I'd like to write code in a way where moving to Python 3 would be simple.
I'm trying to refactor code to use cStringIO instead of StringIO for reading in file bytes, since it's the same API but faster. But there is also something called io.BytesIO which seems to do the same thing, but for bytes only (which is good for my use case, since I'm reading file data). See this SO answer: Confusing about StringIO, cStringIO and ByteIO
However, I don't see any documentation that says you can cleanly replace StringIO.StringIO objects with BytesIO objects, or that BytesIO has the same performance gains that cStringIO has over StringIO.
Basically, I'm asking what is the best method for this refactor? I can either just replace all my StringIO objects with BytesIO objects, or do something like this:
try:
from cStringIO import StringIO
except ImportError: # Py3
from io import BytesIO as StringIO
The first would make sense if BytesIO and cStringIO were comparable in interface and implementation in Python2, and the second would make sense if cStringIO was still considered faster in Python 2.
Thank you!