From def252eb968da2954fae93826c14e91834f8d316 Mon Sep 17 00:00:00 2001 From: ricolin Date: Wed, 2 Aug 2017 17:19:59 +0800 Subject: [PATCH] Use six StringIO On python3, sys.stdin/stdout are Text-mode streams, which can be approximated with io.StringIO. On python2, there's no real distinction between Text-mode and Binary-mode (except for line endings, which sys.stdin/stdout also treat as Text-mode). Hence, there's no need to explicitly use io.BytesIO on python2. StringIO.StringIO is equally or more representative of how the mocked streams behave. Therefore, just use six.StringIO instead of different types for python2 vs. python3. Change-Id: Ib80e10a40e68ece946b344ce7bcfba5e182ce848 --- heat-config-kubelet/install.d/hook-kubelet.py | 5 ++--- tests/test_heat_config_notify.py | 6 +----- tests/test_hook_chef.py | 9 ++------- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/heat-config-kubelet/install.d/hook-kubelet.py b/heat-config-kubelet/install.d/hook-kubelet.py index 72ff0f9..ca50509 100755 --- a/heat-config-kubelet/install.d/hook-kubelet.py +++ b/heat-config-kubelet/install.d/hook-kubelet.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import io import json import logging import os @@ -82,13 +81,13 @@ def configure_logging(): handler.setFormatter(formatter) log.addHandler(handler) - deploy_stdout = io.StringIO() + deploy_stdout = six.StringIO() handler = logging.StreamHandler(deploy_stdout) handler.setFormatter(formatter) handler.setLevel('DEBUG') log.addHandler(handler) - deploy_stderr = io.StringIO() + deploy_stderr = six.StringIO() handler = logging.StreamHandler(deploy_stderr) handler.setFormatter(formatter) handler.setLevel('WARN') diff --git a/tests/test_heat_config_notify.py b/tests/test_heat_config_notify.py index 15ca558..6f6b02c 100644 --- a/tests/test_heat_config_notify.py +++ b/tests/test_heat_config_notify.py @@ -11,7 +11,6 @@ # License for the specific language governing permissions and limitations # under the License. -import io import json import tempfile @@ -77,10 +76,7 @@ class HeatConfigNotifyTest(common.RunScriptTest): super(HeatConfigNotifyTest, self).setUp() self.deployed_dir = self.useFixture(fixtures.TempDir()) hcn.init_logging = mock.MagicMock() - if six.PY2: - self.stdin = io.BytesIO() - else: - self.stdin = io.StringIO() + self.stdin = six.StringIO() def write_config_file(self, data): config_file = tempfile.NamedTemporaryFile(mode='w') diff --git a/tests/test_hook_chef.py b/tests/test_hook_chef.py index 05968dc..2c78ac9 100644 --- a/tests/test_hook_chef.py +++ b/tests/test_hook_chef.py @@ -13,7 +13,6 @@ import copy import imp -import io import json import logging import mock @@ -59,12 +58,8 @@ class HookChefTest(common.RunScriptTest): __file__, '..', 'heat-config-chef/install.d/hook-chef.py') - if six.PY2: - sys.stdin = io.BytesIO() - sys.stdout = io.BytesIO() - else: - sys.stdin = io.StringIO() - sys.stdout = io.StringIO() + sys.stdin = six.StringIO() + sys.stdout = six.StringIO() def tearDown(self): super(HookChefTest, self).tearDown()