Compare commits
4 Commits
amigaos
...
utf8-encod
Author | SHA1 | Date |
---|---|---|
Matt Martz | f6f4f4e7ef | |
Matt Martz | 87cfede4df | |
Matt Martz | fed0d94741 | |
Matt Martz | 51d5b2d2c9 |
38
speedtest.py
38
speedtest.py
|
@ -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:
|
||||||
|
|
Loading…
Reference in New Issue