From 2bdad357fa19f05a3a579b47578f1510974eb0a4 Mon Sep 17 00:00:00 2001 From: Chuck Short Date: Fri, 24 May 2013 10:35:28 -0500 Subject: python3: python3 binary/text data compatbility Python3 enforces the distinction between byte strings far more rigorously than Python 2 does; binary data cannot be automatically coerced to or from text data. Use six to provide a fake file object for textual data. It provides an alias for StringIO.StringIO in python2 and io.StringIO in python3 Change-Id: I65897bb0cca2cbeb5819a769b98645c9eb066401 Signed-off-by: Chuck Short --- tests/unit/middleware/test_sizelimit.py | 10 +++++----- tests/unit/test_jsonutils.py | 5 +++-- tests/unit/test_log.py | 4 ++-- tests/unit/test_policy.py | 6 +++--- tests/unit/test_processutils.py | 9 +++++---- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/tests/unit/middleware/test_sizelimit.py b/tests/unit/middleware/test_sizelimit.py index 500f29d..22098d0 100644 --- a/tests/unit/middleware/test_sizelimit.py +++ b/tests/unit/middleware/test_sizelimit.py @@ -13,7 +13,7 @@ # under the License. from oslo.config import cfg -import StringIO +from six import StringIO import webob from openstack.common.middleware import sizelimit @@ -28,14 +28,14 @@ class TestLimitingReader(utils.BaseTestCase): def test_limiting_reader(self): BYTES = 1024 bytes_read = 0 - data = StringIO.StringIO("*" * BYTES) + data = StringIO("*" * BYTES) for chunk in sizelimit.LimitingReader(data, BYTES): bytes_read += len(chunk) self.assertEquals(bytes_read, BYTES) bytes_read = 0 - data = StringIO.StringIO("*" * BYTES) + data = StringIO("*" * BYTES) reader = sizelimit.LimitingReader(data, BYTES) byte = reader.read(1) while len(byte) != 0: @@ -49,7 +49,7 @@ class TestLimitingReader(utils.BaseTestCase): def _consume_all_iter(): bytes_read = 0 - data = StringIO.StringIO("*" * BYTES) + data = StringIO("*" * BYTES) for chunk in sizelimit.LimitingReader(data, BYTES - 1): bytes_read += len(chunk) @@ -58,7 +58,7 @@ class TestLimitingReader(utils.BaseTestCase): def _consume_all_read(): bytes_read = 0 - data = StringIO.StringIO("*" * BYTES) + data = StringIO("*" * BYTES) reader = sizelimit.LimitingReader(data, BYTES - 1) byte = reader.read(1) while len(byte) != 0: diff --git a/tests/unit/test_jsonutils.py b/tests/unit/test_jsonutils.py index 35a9487..758455b 100644 --- a/tests/unit/test_jsonutils.py +++ b/tests/unit/test_jsonutils.py @@ -16,9 +16,10 @@ # under the License. import datetime -import StringIO import xmlrpclib +from six import StringIO + from openstack.common import jsonutils from tests import utils @@ -32,7 +33,7 @@ class JSONUtilsTestCase(utils.BaseTestCase): self.assertEqual(jsonutils.loads('{"a": "b"}'), {'a': 'b'}) def test_load(self): - x = StringIO.StringIO('{"a": "b"}') + x = StringIO('{"a": "b"}') self.assertEqual(jsonutils.load(x), {'a': 'b'}) diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index f87a1da..a2045d2 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -1,11 +1,11 @@ import cStringIO import logging import os -import StringIO import sys import tempfile from oslo.config import cfg +from six import StringIO from openstack.common import context from openstack.common import jsonutils @@ -351,7 +351,7 @@ class SetDefaultsTestCase(test_utils.BaseTestCase): class LogConfigOptsTestCase(test_utils.BaseTestCase): def test_print_help(self): - f = StringIO.StringIO() + f = StringIO() CONF([]) CONF.print_help(file=f) self.assertTrue('debug' in f.getvalue()) diff --git a/tests/unit/test_policy.py b/tests/unit/test_policy.py index 582021b..cc96168 100644 --- a/tests/unit/test_policy.py +++ b/tests/unit/test_policy.py @@ -18,10 +18,10 @@ """Test of Policy Engine""" import os -import StringIO import urllib import mock +from six import StringIO import urllib2 from oslo.config import cfg @@ -779,7 +779,7 @@ class HttpCheckTestCase(PolicyBaseTestCase): return result @mock.patch.object(urllib2, 'urlopen', - return_value=StringIO.StringIO('True')) + return_value=StringIO('True')) def test_accept(self, mock_urlopen): check = policy.HttpCheck('http', '//example.com/%(name)s') @@ -798,7 +798,7 @@ class HttpCheckTestCase(PolicyBaseTestCase): )) @mock.patch.object(urllib2, 'urlopen', - return_value=StringIO.StringIO('other')) + return_value=StringIO('other')) def test_reject(self, mock_urlopen): check = policy.HttpCheck('http', '//example.com/%(name)s') diff --git a/tests/unit/test_processutils.py b/tests/unit/test_processutils.py index e00a66e..fbe9f34 100644 --- a/tests/unit/test_processutils.py +++ b/tests/unit/test_processutils.py @@ -19,9 +19,10 @@ from __future__ import print_function import fixtures import os -import StringIO import tempfile +from six import StringIO + from openstack.common import processutils from tests import utils @@ -213,7 +214,7 @@ class FakeSshChannel(object): return self.rc -class FakeSshStream(StringIO.StringIO): +class FakeSshStream(StringIO): def setup_channel(self, rc): self.channel = FakeSshChannel(rc) @@ -225,9 +226,9 @@ class FakeSshConnection(object): def exec_command(self, cmd): stdout = FakeSshStream('stdout') stdout.setup_channel(self.rc) - return (StringIO.StringIO(), + return (StringIO(), stdout, - StringIO.StringIO('stderr')) + StringIO('stderr')) class SshExecuteTestCase(utils.BaseTestCase): -- cgit