Browse Source

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
tags/2014.1.b3
He Yongli 5 years ago
parent
commit
aa7dd439c5

+ 3
- 3
cinder/backup/drivers/swift.py View File

@@ -35,10 +35,10 @@ import httplib
35 35
 import json
36 36
 import os
37 37
 import socket
38
-import StringIO
39 38
 
40 39
 import eventlet
41 40
 from oslo.config import cfg
41
+import six
42 42
 
43 43
 from cinder.backup.driver import BackupDriver
44 44
 from cinder import exception
@@ -199,7 +199,7 @@ class SwiftBackupDriver(BackupDriver):
199 199
         metadata['created_at'] = str(backup['created_at'])
200 200
         metadata['objects'] = object_list
201 201
         metadata_json = json.dumps(metadata, sort_keys=True, indent=2)
202
-        reader = StringIO.StringIO(metadata_json)
202
+        reader = six.StringIO(metadata_json)
203 203
         etag = self.conn.put_object(container, filename, reader,
204 204
                                     content_length=reader.len)
205 205
         md5 = hashlib.md5(metadata_json).hexdigest()
@@ -285,7 +285,7 @@ class SwiftBackupDriver(BackupDriver):
285 285
             LOG.debug(_('not compressing data'))
286 286
             obj[object_name]['compression'] = 'none'
287 287
 
288
-        reader = StringIO.StringIO(data)
288
+        reader = six.StringIO(data)
289 289
         LOG.debug(_('About to put_object'))
290 290
         try:
291 291
             etag = self.conn.put_object(container, object_name, reader,

+ 5
- 6
cinder/tests/api/middleware/test_sizelimit.py View File

@@ -12,9 +12,8 @@
12 12
 #    License for the specific language governing permissions and limitations
13 13
 #    under the License.
14 14
 
15
-import StringIO
16
-
17 15
 from oslo.config import cfg
16
+import six
18 17
 import webob
19 18
 
20 19
 from cinder.api.middleware import sizelimit
@@ -31,14 +30,14 @@ class TestLimitingReader(test.TestCase):
31 30
     def test_limiting_reader(self):
32 31
         BYTES = 1024
33 32
         bytes_read = 0
34
-        data = StringIO.StringIO("*" * BYTES)
33
+        data = six.StringIO("*" * BYTES)
35 34
         for chunk in sizelimit.LimitingReader(data, BYTES):
36 35
             bytes_read += len(chunk)
37 36
 
38 37
         self.assertEqual(bytes_read, BYTES)
39 38
 
40 39
         bytes_read = 0
41
-        data = StringIO.StringIO("*" * BYTES)
40
+        data = six.StringIO("*" * BYTES)
42 41
         reader = sizelimit.LimitingReader(data, BYTES)
43 42
         byte = reader.read(1)
44 43
         while len(byte) != 0:
@@ -52,7 +51,7 @@ class TestLimitingReader(test.TestCase):
52 51
 
53 52
         def _consume_all_iter():
54 53
             bytes_read = 0
55
-            data = StringIO.StringIO("*" * BYTES)
54
+            data = six.StringIO("*" * BYTES)
56 55
             for chunk in sizelimit.LimitingReader(data, BYTES - 1):
57 56
                 bytes_read += len(chunk)
58 57
 
@@ -61,7 +60,7 @@ class TestLimitingReader(test.TestCase):
61 60
 
62 61
         def _consume_all_read():
63 62
             bytes_read = 0
64
-            data = StringIO.StringIO("*" * BYTES)
63
+            data = six.StringIO("*" * BYTES)
65 64
             reader = sizelimit.LimitingReader(data, BYTES - 1)
66 65
             byte = reader.read(1)
67 66
             while len(byte) != 0:

+ 2
- 2
cinder/tests/api/v1/test_limits.py View File

@@ -18,10 +18,10 @@ Tests dealing with HTTP rate-limiting.
18 18
 """
19 19
 
20 20
 import httplib
21
-import StringIO
22 21
 from xml.dom import minidom
23 22
 
24 23
 from lxml import etree
24
+import six
25 25
 import webob
26 26
 
27 27
 from cinder.api.v1 import limits
@@ -637,7 +637,7 @@ class FakeHttplibSocket(object):
637 637
 
638 638
     def __init__(self, response_string):
639 639
         """Initialize new `FakeHttplibSocket`."""
640
-        self._buffer = StringIO.StringIO(response_string)
640
+        self._buffer = six.StringIO(response_string)
641 641
 
642 642
     def makefile(self, _mode, _other):
643 643
         """Returns the socket's internal buffer."""

+ 2
- 2
cinder/tests/api/v2/test_limits.py View File

@@ -18,9 +18,9 @@ Tests dealing with HTTP rate-limiting.
18 18
 """
19 19
 
20 20
 import httplib
21
-import StringIO
22 21
 
23 22
 from lxml import etree
23
+import six
24 24
 import webob
25 25
 from xml.dom import minidom
26 26
 
@@ -639,7 +639,7 @@ class FakeHttplibSocket(object):
639 639
 
640 640
     def __init__(self, response_string):
641 641
         """Initialize new `FakeHttplibSocket`."""
642
-        self._buffer = StringIO.StringIO(response_string)
642
+        self._buffer = six.StringIO(response_string)
643 643
 
644 644
     def makefile(self, _mode, _other):
645 645
         """Returns the socket's internal buffer."""

+ 3
- 2
cinder/tests/scheduler/test_scheduler_options.py View File

@@ -17,7 +17,8 @@ Tests For PickledScheduler.
17 17
 """
18 18
 
19 19
 import datetime
20
-import StringIO
20
+
21
+import six
21 22
 
22 23
 from cinder.openstack.common import jsonutils
23 24
 from cinder.scheduler import scheduler_options
@@ -44,7 +45,7 @@ class FakeSchedulerOptions(scheduler_options.SchedulerOptions):
44 45
 
45 46
     def _get_file_handle(self, filename):
46 47
         self.file_was_loaded = True
47
-        return StringIO.StringIO(self._file_data)
48
+        return six.StringIO(self._file_data)
48 49
 
49 50
     def _get_time_now(self):
50 51
         return self._time_now

+ 2
- 2
cinder/tests/test_api.py View File

@@ -18,8 +18,8 @@
18 18
 """Unit tests for the API endpoint."""
19 19
 
20 20
 import httplib
21
-import StringIO
22 21
 
22
+import six
23 23
 import webob
24 24
 
25 25
 
@@ -27,7 +27,7 @@ class FakeHttplibSocket(object):
27 27
     """A fake socket implementation for httplib.HTTPResponse, trivial."""
28 28
     def __init__(self, response_string):
29 29
         self.response_string = response_string
30
-        self._buffer = StringIO.StringIO(response_string)
30
+        self._buffer = six.StringIO(response_string)
31 31
 
32 32
     def makefile(self, _mode, _other):
33 33
         """Returns the socket's internal buffer."""

+ 4
- 3
cinder/tests/test_netapp.py View File

@@ -21,7 +21,8 @@ Tests for NetApp volume driver
21 21
 import BaseHTTPServer
22 22
 import httplib
23 23
 from lxml import etree
24
-import StringIO
24
+
25
+import six
25 26
 
26 27
 from cinder import exception
27 28
 from cinder.openstack.common import log as logging
@@ -61,8 +62,8 @@ class FakeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
61 62
 class FakeHttplibSocket(object):
62 63
     """A fake socket implementation for httplib.HTTPResponse."""
63 64
     def __init__(self, value):
64
-        self._rbuffer = StringIO.StringIO(value)
65
-        self._wbuffer = StringIO.StringIO('')
65
+        self._rbuffer = six.StringIO(value)
66
+        self._wbuffer = six.StringIO('')
66 67
         oldclose = self._wbuffer.close
67 68
 
68 69
         def newclose():

+ 4
- 3
cinder/tests/test_netapp_ssc.py View File

@@ -20,7 +20,8 @@ import copy
20 20
 import httplib
21 21
 from lxml import etree
22 22
 from mox import IgnoreArg
23
-import StringIO
23
+
24
+import six
24 25
 
25 26
 from cinder import exception
26 27
 from cinder import test
@@ -38,8 +39,8 @@ class FakeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
38 39
 class FakeHttplibSocket(object):
39 40
     """A fake socket implementation for httplib.HTTPResponse."""
40 41
     def __init__(self, value):
41
-        self._rbuffer = StringIO.StringIO(value)
42
-        self._wbuffer = StringIO.StringIO('')
42
+        self._rbuffer = six.StringIO(value)
43
+        self._wbuffer = six.StringIO('')
43 44
         oldclose = self._wbuffer.close
44 45
 
45 46
         def newclose():

+ 3
- 3
cinder/tests/test_policy.py View File

@@ -17,10 +17,10 @@
17 17
 """Test of Policy Engine For Cinder."""
18 18
 
19 19
 import os.path
20
-import StringIO
21 20
 import urllib2
22 21
 
23 22
 from oslo.config import cfg
23
+import six
24 24
 
25 25
 from cinder import context
26 26
 from cinder import exception
@@ -108,7 +108,7 @@ class PolicyTestCase(test.TestCase):
108 108
     def test_enforce_http_true(self):
109 109
 
110 110
         def fakeurlopen(url, post_data):
111
-            return StringIO.StringIO("True")
111
+            return six.StringIO("True")
112 112
         self.stubs.Set(urllib2, 'urlopen', fakeurlopen)
113 113
         action = "example:get_http"
114 114
         target = {}
@@ -118,7 +118,7 @@ class PolicyTestCase(test.TestCase):
118 118
     def test_enforce_http_false(self):
119 119
 
120 120
         def fakeurlopen(url, post_data):
121
-            return StringIO.StringIO("False")
121
+            return six.StringIO("False")
122 122
         self.stubs.Set(urllib2, 'urlopen', fakeurlopen)
123 123
         action = "example:get_http"
124 124
         target = {}

+ 2
- 2
cinder/tests/test_utils.py View File

@@ -18,13 +18,13 @@ import datetime
18 18
 import hashlib
19 19
 import os
20 20
 import socket
21
-import StringIO
22 21
 import tempfile
23 22
 import uuid
24 23
 
25 24
 import mox
26 25
 from oslo.config import cfg
27 26
 import paramiko
27
+import six
28 28
 
29 29
 import cinder
30 30
 from cinder.brick.initiator import connector
@@ -446,7 +446,7 @@ class GenericUtilsTestCase(test.TestCase):
446 446
 
447 447
     def test_hash_file(self):
448 448
         data = 'Mary had a little lamb, its fleece as white as snow'
449
-        flo = StringIO.StringIO(data)
449
+        flo = six.StringIO(data)
450 450
         h1 = utils.hash_file(flo)
451 451
         h2 = hashlib.sha1(data).hexdigest()
452 452
         self.assertEqual(h1, h2)

+ 2
- 2
cinder/tests/test_xenapi_sm.py View File

@@ -16,10 +16,10 @@
16 16
 
17 17
 
18 18
 import contextlib
19
-import StringIO
20 19
 
21 20
 import mock
22 21
 import mox
22
+import six
23 23
 
24 24
 from cinder.db import api as db_api
25 25
 from cinder import exception
@@ -497,7 +497,7 @@ class ToolsTest(test.TestCase):
497 497
     def test_stripped_first_line_of(self):
498 498
         mock_context_manager = mock.Mock()
499 499
         mock_context_manager.__enter__ = mock.Mock(
500
-            return_value=StringIO.StringIO('  blah  \n second line \n'))
500
+            return_value=six.StringIO('  blah  \n second line \n'))
501 501
         mock_context_manager.__exit__ = mock.Mock(return_value=False)
502 502
         mock_open = mock.Mock(return_value=mock_context_manager)
503 503
 

Loading…
Cancel
Save