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
This commit is contained in:
ricolin 2017-08-02 17:19:59 +08:00 committed by Zane Bitter
parent bd91db747d
commit def252eb96
3 changed files with 5 additions and 15 deletions

View File

@ -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')

View File

@ -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')

View File

@ -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()