From e6532305bf25e44602fb4e6f20ac1a1e7479ef53 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Mon, 12 Apr 2021 10:17:01 -0400 Subject: [PATCH] Match Python 3 behavior for stdout/stderr. --- src/allmydata/test/common_util.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/allmydata/test/common_util.py b/src/allmydata/test/common_util.py index 91f0b0f78..34d25e441 100644 --- a/src/allmydata/test/common_util.py +++ b/src/allmydata/test/common_util.py @@ -87,13 +87,18 @@ def run_cli_native(verb, *args, **kwargs): argv = nodeargs + [verb] + list(args) stdin = kwargs.get("stdin", "") if encoding is None: - # The original behavior, the Python 2 behavior, is to accept either - # bytes or unicode and try to automatically encode or decode as - # necessary. This works okay for ASCII and if LANG is set - # appropriately. These aren't great constraints so we should move - # away from this behavior. - stdout = StringIO() - stderr = StringIO() + if PY2: + # The original behavior, the Python 2 behavior, is to accept either + # bytes or unicode and try to automatically encode or decode as + # necessary. This works okay for ASCII and if LANG is set + # appropriately. These aren't great constraints so we should move + # away from this behavior. + stdout = StringIO() + stderr = StringIO() + else: + # Default on Python 3 is accepting text. + stdout = TextIOWrapper(BytesIO(), "utf-8") + stderr = TextIOWrapper(BytesIO(), "utf-8") else: # The new behavior, the Python 3 behavior, is to accept unicode and # encode it using a specific encoding. For older versions of Python