From 24e7dcfcd882f55d08406d9fea7df5a35a3d9905 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Tue, 7 Sep 2021 16:10:10 -0400 Subject: [PATCH] Let me write unicode to stdout on Python 2 --- src/allmydata/scripts/tahoe_status.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/allmydata/scripts/tahoe_status.py b/src/allmydata/scripts/tahoe_status.py index 13677b189..9b2c89d24 100644 --- a/src/allmydata/scripts/tahoe_status.py +++ b/src/allmydata/scripts/tahoe_status.py @@ -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): """