Fix StringIO usage in tests for Python 3