Compare commits

...

4 Commits

Author SHA1 Message Date
Matt Martz f6f4f4e7ef Flake8 fix 2017-04-11 10:29:07 -05:00
Matt Martz 87cfede4df Invert logic for py3 print detection, to avoid confusion created by the future package 2017-04-11 10:18:42 -05:00
Matt Martz fed0d94741 Prefer io over cStringIO and StringIO 2017-04-10 19:43:46 -05:00
Matt Martz 51d5b2d2c9 Handle utf-8 output encoding in py3 also 2017-04-10 15:32:14 -05:00
1 changed files with 30 additions and 8 deletions

View File

@ -131,18 +131,42 @@ except ImportError:
PARSER_TYPE_STR = 'string' PARSER_TYPE_STR = 'string'
try: try:
from cStringIO import StringIO from io import StringIO, BytesIO, TextIOWrapper, FileIO
BytesIO = None
except ImportError: except ImportError:
try: try:
from io import StringIO, BytesIO from cStringIO import StringIO
BytesIO = None
except ImportError: except ImportError:
from StringIO import StringIO from StringIO import StringIO
BytesIO = None BytesIO = None
try: try:
import builtins import __builtin__
except ImportError: except ImportError:
import builtins
class _Py3Utf8Stdout(TextIOWrapper):
def __init__(self, **kwargs):
buf = FileIO(sys.stdout.fileno(), 'w')
super(_Py3Utf8Stdout, self).__init__(
buf,
encoding='utf8',
errors='strict'
)
def write(self, s):
super(_Py3Utf8Stdout, self).write(s)
self.flush()
_py3_print = getattr(builtins, 'print')
_py3_utf8_stdout = _Py3Utf8Stdout()
def print_(*args, **kwargs):
kwargs['file'] = _py3_utf8_stdout
_py3_print(*args, **kwargs)
else:
del __builtin__
def print_(*args, **kwargs): def print_(*args, **kwargs):
"""The new-style print function for Python 2.4 and 2.5. """The new-style print function for Python 2.4 and 2.5.
@ -158,7 +182,7 @@ except ImportError:
if not isinstance(data, basestring): if not isinstance(data, basestring):
data = str(data) data = str(data)
# If the file has an encoding, encode unicode with it. # If the file has an encoding, encode unicode with it.
encoding = 'UTF-8' # Always trust UTF-8 for output encoding = 'utf8' # Always trust UTF-8 for output
if (isinstance(fp, file) and if (isinstance(fp, file) and
isinstance(data, unicode) and isinstance(data, unicode) and
encoding is not None): encoding is not None):
@ -202,9 +226,7 @@ except ImportError:
write(sep) write(sep)
write(arg) write(arg)
write(end) write(end)
else:
print_ = getattr(builtins, 'print')
del builtins
# Exception "constants" to support Python 2 through Python 3 # Exception "constants" to support Python 2 through Python 3
try: try: