fix encoding errors with io.StringIO in python2

Sorry my last PR introduced a bug in some cases with unicode.
io.StringIO treat output as unicode, in py2 ioStringIO seems to call StringIO.StringIO.getvalue() which raises UnicodeError for char > 128.