Let me write unicode to stdout on Python 2

This commit is contained in:
Jean-Paul Calderone 2021-09-07 16:10:10 -04:00
parent 4db8c2209b
commit 24e7dcfcd8

View File

@ -21,6 +21,26 @@ from allmydata.scripts.common_http import BadResponse
from allmydata.util.abbreviate import abbreviate_space, abbreviate_time
from allmydata.util.encodingutil import argv_to_abspath
if PY2:
_print = print
def print(*args, **kwargs):
"""
Builtin ``print``-alike that will even write unicode not encodeable using
the specified output file's encoding.
"""
out = kwargs.pop("file", None)
if out is None:
from sys import stdout as out
encoding = out.encoding or "ascii"
def ensafe(o):
if isinstance(o, unicode):
return o.encode(encoding, errors="replace").decode(encoding)
return o
return _print(
*(ensafe(a) for a in args),
file=out,
**kwargs
)
def _get_request_parameters_for_fragment(options, fragment, method, post_args):
"""