]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Use six.StringIO instead of StringIO.StringIO
authorHe Yongli <yongli.he@intel.com>
Mon, 17 Feb 2014 04:42:33 +0000 (12:42 +0800)
committerHe Yongli <yongli.he@intel.com>
Tue, 25 Feb 2014 09:07:29 +0000 (17:07 +0800)
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
cinder/tests/api/middleware/test_sizelimit.py
cinder/tests/api/v1/test_limits.py
cinder/tests/api/v2/test_limits.py
cinder/tests/scheduler/test_scheduler_options.py
cinder/tests/test_api.py
cinder/tests/test_netapp.py
cinder/tests/test_netapp_ssc.py
cinder/tests/test_policy.py
cinder/tests/test_utils.py
cinder/tests/test_xenapi_sm.py

index 9abd39c332cb356484a7212970fb73b733cffd47..1ddf8fd5b6338e7e969ca76f2f42b4c5442742b0 100644 (file)
@@ -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,
index 80184d828c9accd48561d3081ba61b5c25131bd8..61d8f17425a21359d8c307964aeee895de9945ea 100644 (file)
@@ -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:
index 755a0532320afde56e9b3886e4d8a8a58fe21d21..c8e92eb8dcf985d5aec3a3255eb73e685e3774e3 100644 (file)
@@ -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."""
index 6c2316cd186da6b38c56aa5d3fbddf20f97ba4cf..0e428a9196a07aa183a7f08d68b4eb0a9d757093 100644 (file)
@@ -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."""
index c670c3a511b8143f5456ee037cc3c56a11549081..c1073973ef7eb401e5b962e8652dc914701ad6c5 100644 (file)
@@ -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
index ad6e051c0c4c97eb2042e4b95948b7332c43946b..7076bd5e3790751d7a254dc772cf8a11828b700d 100644 (file)
@@ -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."""
index bad9fc85f7a98e85140bc5d9d7ec1f396d251fc3..c246a718d30c4ca2682556c5b68730f12b07f122 100644 (file)
@@ -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():
index 99211269ccfd11e82b826ccf4a1defdb92fabfb3..ad2f3172655b5cb6a4c3c79282e97a5451d084e7 100644 (file)
@@ -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():
index e900f1306a707440e247fa0e3379402720b3d029..7cf7b2480aec066ed37bfc0c4b429e9da9849e79 100644 (file)
 """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 = {}
index 380c13522267a796316d9671eee38b6908a0f7fb..0e1514c07cc6a2e47803b7d3d17a9a9b0e7f3964 100644 (file)
@@ -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)
index e9df123c242979a67b757a30d763424a57b16a7c..51c7cf11dd8f7feaa04906df3cca745a38443ace 100644 (file)
 
 
 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)