From 8aa339127638b413a943727d2985a5bd7245487c Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Thu, 27 May 2021 13:26:46 -0400 Subject: [PATCH] Consistent behavior. --- src/allmydata/test/common_util.py | 3 ++- src/allmydata/util/encodingutil.py | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/allmydata/test/common_util.py b/src/allmydata/test/common_util.py index 9e7f0618c..21b8817a4 100644 --- a/src/allmydata/test/common_util.py +++ b/src/allmydata/test/common_util.py @@ -12,6 +12,7 @@ if PY2: from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, dict, list, object, range, str, max, min # noqa: F401 import os +import sys import time import signal from random import randrange @@ -85,7 +86,7 @@ def run_cli_native(verb, *args, **kwargs): bytes. """ nodeargs = kwargs.pop("nodeargs", []) - encoding = kwargs.pop("encoding", None) or "utf-8" + encoding = kwargs.pop("encoding", None) or getattr(sys.stdout, "encoding") or "utf-8" return_bytes = kwargs.pop("return_bytes", False) verb = maybe_unicode_to_argv(verb) args = [maybe_unicode_to_argv(a) for a in args] diff --git a/src/allmydata/util/encodingutil.py b/src/allmydata/util/encodingutil.py index 4cf690e65..576091539 100644 --- a/src/allmydata/util/encodingutil.py +++ b/src/allmydata/util/encodingutil.py @@ -256,7 +256,11 @@ def quote_output_u(*args, **kwargs): result = quote_output(*args, **kwargs) if isinstance(result, unicode): return result - return result.decode(kwargs.get("encoding", None) or io_encoding) + # Since we're quoting, the assumption is this will be read by a human, and + # therefore printed, so stdout's encoding is the plausible one. io_encoding + # is now always utf-8. + return result.decode(kwargs.get("encoding", None) or + getattr(sys.stdout, "encoding") or io_encoding) def quote_output(s, quotemarks=True, quote_newlines=None, encoding=None): @@ -287,7 +291,7 @@ def quote_output(s, quotemarks=True, quote_newlines=None, encoding=None): def _encode(s): if isinstance(s, bytes): try: - s = s.decode('utf-8') + s = s.decode("utf-8") except UnicodeDecodeError: return b'b"%s"' % (ESCAPABLE_8BIT.sub(lambda m: _bytes_escape(m, quote_newlines), s),)