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
This commit is contained in:
He Yongli 2014-02-17 12:42:33 +08:00
parent ee1b6428c7
commit aa7dd439c5
11 changed files with 32 additions and 30 deletions

View 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,

View 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:

View 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."""

View 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."""

View 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

View 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."""

View 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():

View 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():

View File

@ -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 = {}

View 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)

View File

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