Python3: use six.StringIO rather than StringIO.StringIO

It’s an alias for StringIO.StringIO in Python 2 and
io.StringIO in Python 3.

Change-Id: I5316eaffa2d9d2d5679b85a901933ef0fbfcc2f7
This commit is contained in:
Yassine Lamgarchal
2014-01-10 17:25:25 +01:00
parent c473f19fc9
commit ae579aae09
6 changed files with 30 additions and 30 deletions

View File

@@ -15,10 +15,10 @@
import errno import errno
import socket import socket
import StringIO
import urlparse import urlparse
import mox import mox
import six
import testtools import testtools
import glanceclient import glanceclient
@@ -102,7 +102,7 @@ class TestClient(testtools.TestCase):
def test_request_redirected(self): def test_request_redirected(self):
resp = utils.FakeResponse({'location': 'http://www.example.com'}, resp = utils.FakeResponse({'location': 'http://www.example.com'},
status=302, body=StringIO.StringIO()) status=302, body=six.StringIO())
http_client.HTTPConnection.request( http_client.HTTPConnection.request(
mox.IgnoreArg(), mox.IgnoreArg(),
mox.IgnoreArg(), mox.IgnoreArg(),
@@ -112,7 +112,7 @@ class TestClient(testtools.TestCase):
# The second request should be to the redirected location # The second request should be to the redirected location
expected_response = 'Ok' expected_response = 'Ok'
resp2 = utils.FakeResponse({}, StringIO.StringIO(expected_response)) resp2 = utils.FakeResponse({}, six.StringIO(expected_response))
http_client.HTTPConnection.request( http_client.HTTPConnection.request(
'GET', 'GET',
'http://www.example.com', 'http://www.example.com',
@@ -133,7 +133,7 @@ class TestClient(testtools.TestCase):
# Lets fake the response # Lets fake the response
# returned by httplib # returned by httplib
expected_response = 'Ok' expected_response = 'Ok'
fake = utils.FakeResponse({}, StringIO.StringIO(expected_response)) fake = utils.FakeResponse({}, six.StringIO(expected_response))
http_client.HTTPConnection.getresponse().AndReturn(fake) http_client.HTTPConnection.getresponse().AndReturn(fake)
self.mock.ReplayAll() self.mock.ReplayAll()
@@ -161,7 +161,7 @@ class TestClient(testtools.TestCase):
headers=mox.IgnoreArg()).WithSideEffects(check_request) headers=mox.IgnoreArg()).WithSideEffects(check_request)
# fake the response returned by httplib # fake the response returned by httplib
fake = utils.FakeResponse({}, StringIO.StringIO('Ok')) fake = utils.FakeResponse({}, six.StringIO('Ok'))
http_client.HTTPConnection.getresponse().AndReturn(fake) http_client.HTTPConnection.getresponse().AndReturn(fake)
self.mock.ReplayAll() self.mock.ReplayAll()
@@ -189,7 +189,7 @@ class TestClient(testtools.TestCase):
headers=mox.IgnoreArg()).WithSideEffects(check_request) headers=mox.IgnoreArg()).WithSideEffects(check_request)
# fake the response returned by httplib # fake the response returned by httplib
fake = utils.FakeResponse({}, StringIO.StringIO('Ok')) fake = utils.FakeResponse({}, six.StringIO('Ok'))
http_client.HTTPConnection.getresponse().AndReturn(fake) http_client.HTTPConnection.getresponse().AndReturn(fake)
self.mock.ReplayAll() self.mock.ReplayAll()
@@ -313,19 +313,19 @@ class TestVerifiedHTTPSConnection(testtools.TestCase):
class TestResponseBodyIterator(testtools.TestCase): class TestResponseBodyIterator(testtools.TestCase):
def test_iter_default_chunk_size_64k(self): def test_iter_default_chunk_size_64k(self):
resp = utils.FakeResponse({}, StringIO.StringIO('X' * 98304)) resp = utils.FakeResponse({}, six.StringIO('X' * 98304))
iterator = http.ResponseBodyIterator(resp) iterator = http.ResponseBodyIterator(resp)
chunks = list(iterator) chunks = list(iterator)
self.assertEqual(chunks, ['X' * 65536, 'X' * 32768]) self.assertEqual(chunks, ['X' * 65536, 'X' * 32768])
def test_integrity_check_with_correct_checksum(self): def test_integrity_check_with_correct_checksum(self):
resp = utils.FakeResponse({}, StringIO.StringIO('CCC')) resp = utils.FakeResponse({}, six.StringIO('CCC'))
body = http.ResponseBodyIterator(resp) body = http.ResponseBodyIterator(resp)
body.set_checksum('defb99e69a9f1f6e06f15006b1f166ae') body.set_checksum('defb99e69a9f1f6e06f15006b1f166ae')
list(body) list(body)
def test_integrity_check_with_wrong_checksum(self): def test_integrity_check_with_wrong_checksum(self):
resp = utils.FakeResponse({}, StringIO.StringIO('BB')) resp = utils.FakeResponse({}, six.StringIO('BB'))
body = http.ResponseBodyIterator(resp) body = http.ResponseBodyIterator(resp)
body.set_checksum('wrong') body.set_checksum('wrong')
try: try:
@@ -335,7 +335,7 @@ class TestResponseBodyIterator(testtools.TestCase):
self.assertEqual(errno.EPIPE, e.errno) self.assertEqual(errno.EPIPE, e.errno)
def test_set_checksum_in_consumed_iterator(self): def test_set_checksum_in_consumed_iterator(self):
resp = utils.FakeResponse({}, StringIO.StringIO('CCC')) resp = utils.FakeResponse({}, six.StringIO('CCC'))
body = http.ResponseBodyIterator(resp) body = http.ResponseBodyIterator(resp)
list(body) list(body)
# Setting checksum for an already consumed iterator should raise an # Setting checksum for an already consumed iterator should raise an
@@ -347,6 +347,6 @@ class TestResponseBodyIterator(testtools.TestCase):
def test_body_size(self): def test_body_size(self):
size = 1000000007 size = 1000000007
resp = utils.FakeResponse( resp = utils.FakeResponse(
{'content-length': str(size)}, StringIO.StringIO('BB')) {'content-length': str(size)}, six.StringIO('BB'))
body = http.ResponseBodyIterator(resp) body = http.ResponseBodyIterator(resp)
self.assertEqual(len(body), size) self.assertEqual(len(body), size)

View File

@@ -13,9 +13,9 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import StringIO
import sys import sys
import six
import testtools import testtools
from glanceclient.common import progressbar from glanceclient.common import progressbar
@@ -42,7 +42,7 @@ class TestProgressBarWrapper(testtools.TestCase):
def test_iter_file_display_progress_bar(self): def test_iter_file_display_progress_bar(self):
size = 98304 size = 98304
file_obj = StringIO.StringIO('X' * size) file_obj = six.StringIO('X' * size)
saved_stdout = sys.stdout saved_stdout = sys.stdout
try: try:
sys.stdout = output = test_utils.FakeTTYStdout() sys.stdout = output = test_utils.FakeTTYStdout()
@@ -60,7 +60,7 @@ class TestProgressBarWrapper(testtools.TestCase):
def test_iter_file_no_tty(self): def test_iter_file_no_tty(self):
size = 98304 size = 98304
file_obj = StringIO.StringIO('X' * size) file_obj = six.StringIO('X' * size)
saved_stdout = sys.stdout saved_stdout = sys.stdout
try: try:
sys.stdout = output = test_utils.FakeNoTTYStdout() sys.stdout = output = test_utils.FakeNoTTYStdout()

View File

@@ -13,9 +13,9 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import StringIO
import sys import sys
import six
import testtools import testtools
from glanceclient.common import utils from glanceclient.common import utils
@@ -32,7 +32,7 @@ class TestUtils(testtools.TestCase):
def test_get_new_file_size(self): def test_get_new_file_size(self):
size = 98304 size = 98304
file_obj = StringIO.StringIO('X' * size) file_obj = six.StringIO('X' * size)
try: try:
self.assertEqual(utils.get_file_size(file_obj), size) self.assertEqual(utils.get_file_size(file_obj), size)
# Check that get_file_size didn't change original file position. # Check that get_file_size didn't change original file position.
@@ -42,7 +42,7 @@ class TestUtils(testtools.TestCase):
def test_get_consumed_file_size(self): def test_get_consumed_file_size(self):
size, consumed = 98304, 304 size, consumed = 98304, 304
file_obj = StringIO.StringIO('X' * size) file_obj = six.StringIO('X' * size)
file_obj.seek(consumed) file_obj.seek(consumed)
try: try:
self.assertEqual(utils.get_file_size(file_obj), size) self.assertEqual(utils.get_file_size(file_obj), size)
@@ -64,10 +64,10 @@ class TestUtils(testtools.TestCase):
saved_stdout = sys.stdout saved_stdout = sys.stdout
try: try:
sys.stdout = output_list = StringIO.StringIO() sys.stdout = output_list = six.StringIO()
utils.print_list(images, columns) utils.print_list(images, columns)
sys.stdout = output_dict = StringIO.StringIO() sys.stdout = output_dict = six.StringIO()
utils.print_dict({'K': 'k', 'Key': 'veeeeeeeeeeeeeeeeeeeeeeee' utils.print_dict({'K': 'k', 'Key': 'veeeeeeeeeeeeeeeeeeeeeeee'
'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'

View File

@@ -15,7 +15,7 @@
import copy import copy
import requests import requests
import StringIO import six
import testtools import testtools
from glanceclient.common import http from glanceclient.common import http
@@ -33,7 +33,7 @@ class FakeAPI(object):
def raw_request(self, *args, **kwargs): def raw_request(self, *args, **kwargs):
fixture = self._request(*args, **kwargs) fixture = self._request(*args, **kwargs)
resp = FakeResponse(fixture[0], StringIO.StringIO(fixture[1])) resp = FakeResponse(fixture[0], six.StringIO(fixture[1]))
body_iter = http.ResponseBodyIterator(resp) body_iter = http.ResponseBodyIterator(resp)
return resp, body_iter return resp, body_iter
@@ -98,7 +98,7 @@ class TestResponse(requests.Response):
return self._text return self._text
class FakeTTYStdout(StringIO.StringIO): class FakeTTYStdout(six.StringIO):
"""A Fake stdout that try to emulate a TTY device as much as possible.""" """A Fake stdout that try to emulate a TTY device as much as possible."""
def isatty(self): def isatty(self):
@@ -109,7 +109,7 @@ class FakeTTYStdout(StringIO.StringIO):
if data.startswith('\r'): if data.startswith('\r'):
self.seek(0) self.seek(0)
data = data[1:] data = data[1:]
return StringIO.StringIO.write(self, data) return six.StringIO.write(self, data)
class FakeNoTTYStdout(FakeTTYStdout): class FakeNoTTYStdout(FakeTTYStdout):

View File

@@ -15,11 +15,12 @@
import errno import errno
import json import json
import StringIO
import sys import sys
import testtools import testtools
import urlparse import urlparse
import six
from glanceclient.v1 import client from glanceclient.v1 import client
from glanceclient.v1 import images from glanceclient.v1 import images
from glanceclient.v1 import legacy_shell from glanceclient.v1 import legacy_shell
@@ -540,7 +541,7 @@ class ImageManagerTest(testtools.TestCase):
self.assertEqual(image.properties, {'a': 'b', 'c': 'd'}) self.assertEqual(image.properties, {'a': 'b', 'c': 'd'})
def test_create_with_data(self): def test_create_with_data(self):
image_data = StringIO.StringIO('XXX') image_data = six.StringIO('XXX')
self.mgr.create(data=image_data) self.mgr.create(data=image_data)
expect_headers = {'x-image-meta-size': '3'} expect_headers = {'x-image-meta-size': '3'}
expect = [('POST', '/v1/images', expect_headers, image_data)] expect = [('POST', '/v1/images', expect_headers, image_data)]
@@ -582,7 +583,7 @@ class ImageManagerTest(testtools.TestCase):
self.assertEqual(image.min_disk, 10) self.assertEqual(image.min_disk, 10)
def test_update_with_data(self): def test_update_with_data(self):
image_data = StringIO.StringIO('XXX') image_data = six.StringIO('XXX')
self.mgr.update('1', data=image_data) self.mgr.update('1', data=image_data)
expect_headers = {'x-image-meta-size': '3'} expect_headers = {'x-image-meta-size': '3'}
expect = [('PUT', '/v1/images/1', expect_headers, image_data)] expect = [('PUT', '/v1/images/1', expect_headers, image_data)]

View File

@@ -15,9 +15,8 @@
# under the License. # under the License.
# vim: tabstop=4 shiftwidth=4 softtabstop=4 # vim: tabstop=4 shiftwidth=4 softtabstop=4
import StringIO
import mock import mock
import six
import testtools import testtools
from glanceclient.common import http from glanceclient.common import http
@@ -236,7 +235,7 @@ class ShellV2Test(testtools.TestCase):
{'id': 'pass', 'file': 'test', 'progress': False}) {'id': 'pass', 'file': 'test', 'progress': False})
with mock.patch.object(self.gc.images, 'data') as mocked_data: with mock.patch.object(self.gc.images, 'data') as mocked_data:
resp = test_utils.FakeResponse({}, StringIO.StringIO('CCC')) resp = test_utils.FakeResponse({}, six.StringIO('CCC'))
ret = mocked_data.return_value = http.ResponseBodyIterator(resp) ret = mocked_data.return_value = http.ResponseBodyIterator(resp)
test_shell.do_image_download(self.gc, args) test_shell.do_image_download(self.gc, args)
@@ -248,7 +247,7 @@ class ShellV2Test(testtools.TestCase):
{'id': 'pass', 'file': 'test', 'progress': True}) {'id': 'pass', 'file': 'test', 'progress': True})
with mock.patch.object(self.gc.images, 'data') as mocked_data: with mock.patch.object(self.gc.images, 'data') as mocked_data:
resp = test_utils.FakeResponse({}, StringIO.StringIO('CCC')) resp = test_utils.FakeResponse({}, six.StringIO('CCC'))
mocked_data.return_value = http.ResponseBodyIterator(resp) mocked_data.return_value = http.ResponseBodyIterator(resp)
test_shell.do_image_download(self.gc, args) test_shell.do_image_download(self.gc, args)