From aa7dd439c580378b1ca2d647c7c2b0c32289b2f1 Mon Sep 17 00:00:00 2001 From: He Yongli Date: Mon, 17 Feb 2014 12:42:33 +0800 Subject: [PATCH] Use six.StringIO instead of StringIO.StringIO to keep Python 3.x compatibility, use six.StringIO/BytesIO to replace StringIO.StringIO StringIO works for unicode BytesIO works for bytes Change-Id: I5e40809b2347d4dbe031ba92dac4848b044d3af6 Closes-Bug: #1280100 --- cinder/backup/drivers/swift.py | 6 +++--- cinder/tests/api/middleware/test_sizelimit.py | 11 +++++------ cinder/tests/api/v1/test_limits.py | 4 ++-- cinder/tests/api/v2/test_limits.py | 4 ++-- cinder/tests/scheduler/test_scheduler_options.py | 5 +++-- cinder/tests/test_api.py | 4 ++-- cinder/tests/test_netapp.py | 7 ++++--- cinder/tests/test_netapp_ssc.py | 7 ++++--- cinder/tests/test_policy.py | 6 +++--- cinder/tests/test_utils.py | 4 ++-- cinder/tests/test_xenapi_sm.py | 4 ++-- 11 files changed, 32 insertions(+), 30 deletions(-) diff --git a/cinder/backup/drivers/swift.py b/cinder/backup/drivers/swift.py index 9abd39c33..1ddf8fd5b 100644 --- a/cinder/backup/drivers/swift.py +++ b/cinder/backup/drivers/swift.py @@ -35,10 +35,10 @@ import httplib import json import os import socket -import StringIO import eventlet from oslo.config import cfg +import six from cinder.backup.driver import BackupDriver from cinder import exception @@ -199,7 +199,7 @@ class SwiftBackupDriver(BackupDriver): metadata['created_at'] = str(backup['created_at']) metadata['objects'] = object_list metadata_json = json.dumps(metadata, sort_keys=True, indent=2) - reader = StringIO.StringIO(metadata_json) + reader = six.StringIO(metadata_json) etag = self.conn.put_object(container, filename, reader, content_length=reader.len) md5 = hashlib.md5(metadata_json).hexdigest() @@ -285,7 +285,7 @@ class SwiftBackupDriver(BackupDriver): LOG.debug(_('not compressing data')) obj[object_name]['compression'] = 'none' - reader = StringIO.StringIO(data) + reader = six.StringIO(data) LOG.debug(_('About to put_object')) try: etag = self.conn.put_object(container, object_name, reader, diff --git a/cinder/tests/api/middleware/test_sizelimit.py b/cinder/tests/api/middleware/test_sizelimit.py index 80184d828..61d8f1742 100644 --- a/cinder/tests/api/middleware/test_sizelimit.py +++ b/cinder/tests/api/middleware/test_sizelimit.py @@ -12,9 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. -import StringIO - from oslo.config import cfg +import six import webob from cinder.api.middleware import sizelimit @@ -31,14 +30,14 @@ class TestLimitingReader(test.TestCase): def test_limiting_reader(self): BYTES = 1024 bytes_read = 0 - data = StringIO.StringIO("*" * BYTES) + data = six.StringIO("*" * BYTES) for chunk in sizelimit.LimitingReader(data, BYTES): bytes_read += len(chunk) self.assertEqual(bytes_read, BYTES) bytes_read = 0 - data = StringIO.StringIO("*" * BYTES) + data = six.StringIO("*" * BYTES) reader = sizelimit.LimitingReader(data, BYTES) byte = reader.read(1) while len(byte) != 0: @@ -52,7 +51,7 @@ class TestLimitingReader(test.TestCase): def _consume_all_iter(): bytes_read = 0 - data = StringIO.StringIO("*" * BYTES) + data = six.StringIO("*" * BYTES) for chunk in sizelimit.LimitingReader(data, BYTES - 1): bytes_read += len(chunk) @@ -61,7 +60,7 @@ class TestLimitingReader(test.TestCase): def _consume_all_read(): bytes_read = 0 - data = StringIO.StringIO("*" * BYTES) + data = six.StringIO("*" * BYTES) reader = sizelimit.LimitingReader(data, BYTES - 1) byte = reader.read(1) while len(byte) != 0: diff --git a/cinder/tests/api/v1/test_limits.py b/cinder/tests/api/v1/test_limits.py index 755a05323..c8e92eb8d 100644 --- a/cinder/tests/api/v1/test_limits.py +++ b/cinder/tests/api/v1/test_limits.py @@ -18,10 +18,10 @@ Tests dealing with HTTP rate-limiting. """ import httplib -import StringIO from xml.dom import minidom from lxml import etree +import six import webob from cinder.api.v1 import limits @@ -637,7 +637,7 @@ class FakeHttplibSocket(object): def __init__(self, response_string): """Initialize new `FakeHttplibSocket`.""" - self._buffer = StringIO.StringIO(response_string) + self._buffer = six.StringIO(response_string) def makefile(self, _mode, _other): """Returns the socket's internal buffer.""" diff --git a/cinder/tests/api/v2/test_limits.py b/cinder/tests/api/v2/test_limits.py index 6c2316cd1..0e428a919 100644 --- a/cinder/tests/api/v2/test_limits.py +++ b/cinder/tests/api/v2/test_limits.py @@ -18,9 +18,9 @@ Tests dealing with HTTP rate-limiting. """ import httplib -import StringIO from lxml import etree +import six import webob from xml.dom import minidom @@ -639,7 +639,7 @@ class FakeHttplibSocket(object): def __init__(self, response_string): """Initialize new `FakeHttplibSocket`.""" - self._buffer = StringIO.StringIO(response_string) + self._buffer = six.StringIO(response_string) def makefile(self, _mode, _other): """Returns the socket's internal buffer.""" diff --git a/cinder/tests/scheduler/test_scheduler_options.py b/cinder/tests/scheduler/test_scheduler_options.py index c670c3a51..c1073973e 100644 --- a/cinder/tests/scheduler/test_scheduler_options.py +++ b/cinder/tests/scheduler/test_scheduler_options.py @@ -17,7 +17,8 @@ Tests For PickledScheduler. """ import datetime -import StringIO + +import six from cinder.openstack.common import jsonutils from cinder.scheduler import scheduler_options @@ -44,7 +45,7 @@ class FakeSchedulerOptions(scheduler_options.SchedulerOptions): def _get_file_handle(self, filename): self.file_was_loaded = True - return StringIO.StringIO(self._file_data) + return six.StringIO(self._file_data) def _get_time_now(self): return self._time_now diff --git a/cinder/tests/test_api.py b/cinder/tests/test_api.py index ad6e051c0..7076bd5e3 100644 --- a/cinder/tests/test_api.py +++ b/cinder/tests/test_api.py @@ -18,8 +18,8 @@ """Unit tests for the API endpoint.""" import httplib -import StringIO +import six import webob @@ -27,7 +27,7 @@ class FakeHttplibSocket(object): """A fake socket implementation for httplib.HTTPResponse, trivial.""" def __init__(self, response_string): self.response_string = response_string - self._buffer = StringIO.StringIO(response_string) + self._buffer = six.StringIO(response_string) def makefile(self, _mode, _other): """Returns the socket's internal buffer.""" diff --git a/cinder/tests/test_netapp.py b/cinder/tests/test_netapp.py index bad9fc85f..c246a718d 100644 --- a/cinder/tests/test_netapp.py +++ b/cinder/tests/test_netapp.py @@ -21,7 +21,8 @@ Tests for NetApp volume driver import BaseHTTPServer import httplib from lxml import etree -import StringIO + +import six from cinder import exception from cinder.openstack.common import log as logging @@ -61,8 +62,8 @@ class FakeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): class FakeHttplibSocket(object): """A fake socket implementation for httplib.HTTPResponse.""" def __init__(self, value): - self._rbuffer = StringIO.StringIO(value) - self._wbuffer = StringIO.StringIO('') + self._rbuffer = six.StringIO(value) + self._wbuffer = six.StringIO('') oldclose = self._wbuffer.close def newclose(): diff --git a/cinder/tests/test_netapp_ssc.py b/cinder/tests/test_netapp_ssc.py index 99211269c..ad2f31726 100644 --- a/cinder/tests/test_netapp_ssc.py +++ b/cinder/tests/test_netapp_ssc.py @@ -20,7 +20,8 @@ import copy import httplib from lxml import etree from mox import IgnoreArg -import StringIO + +import six from cinder import exception from cinder import test @@ -38,8 +39,8 @@ class FakeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): class FakeHttplibSocket(object): """A fake socket implementation for httplib.HTTPResponse.""" def __init__(self, value): - self._rbuffer = StringIO.StringIO(value) - self._wbuffer = StringIO.StringIO('') + self._rbuffer = six.StringIO(value) + self._wbuffer = six.StringIO('') oldclose = self._wbuffer.close def newclose(): diff --git a/cinder/tests/test_policy.py b/cinder/tests/test_policy.py index e900f1306..7cf7b2480 100644 --- a/cinder/tests/test_policy.py +++ b/cinder/tests/test_policy.py @@ -17,10 +17,10 @@ """Test of Policy Engine For Cinder.""" import os.path -import StringIO import urllib2 from oslo.config import cfg +import six from cinder import context from cinder import exception @@ -108,7 +108,7 @@ class PolicyTestCase(test.TestCase): def test_enforce_http_true(self): def fakeurlopen(url, post_data): - return StringIO.StringIO("True") + return six.StringIO("True") self.stubs.Set(urllib2, 'urlopen', fakeurlopen) action = "example:get_http" target = {} @@ -118,7 +118,7 @@ class PolicyTestCase(test.TestCase): def test_enforce_http_false(self): def fakeurlopen(url, post_data): - return StringIO.StringIO("False") + return six.StringIO("False") self.stubs.Set(urllib2, 'urlopen', fakeurlopen) action = "example:get_http" target = {} diff --git a/cinder/tests/test_utils.py b/cinder/tests/test_utils.py index 380c13522..0e1514c07 100644 --- a/cinder/tests/test_utils.py +++ b/cinder/tests/test_utils.py @@ -18,13 +18,13 @@ import datetime import hashlib import os import socket -import StringIO import tempfile import uuid import mox from oslo.config import cfg import paramiko +import six import cinder from cinder.brick.initiator import connector @@ -446,7 +446,7 @@ class GenericUtilsTestCase(test.TestCase): def test_hash_file(self): data = 'Mary had a little lamb, its fleece as white as snow' - flo = StringIO.StringIO(data) + flo = six.StringIO(data) h1 = utils.hash_file(flo) h2 = hashlib.sha1(data).hexdigest() self.assertEqual(h1, h2) diff --git a/cinder/tests/test_xenapi_sm.py b/cinder/tests/test_xenapi_sm.py index e9df123c2..51c7cf11d 100644 --- a/cinder/tests/test_xenapi_sm.py +++ b/cinder/tests/test_xenapi_sm.py @@ -16,10 +16,10 @@ import contextlib -import StringIO import mock import mox +import six from cinder.db import api as db_api from cinder import exception @@ -497,7 +497,7 @@ class ToolsTest(test.TestCase): def test_stripped_first_line_of(self): mock_context_manager = mock.Mock() mock_context_manager.__enter__ = mock.Mock( - return_value=StringIO.StringIO(' blah \n second line \n')) + return_value=six.StringIO(' blah \n second line \n')) mock_context_manager.__exit__ = mock.Mock(return_value=False) mock_open = mock.Mock(return_value=mock_context_manager) -- 2.45.2