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 io.StringIO seems to call StringIO.StringIO.getvalue() which raises UnicodeError for char > 128.