summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@enovance.com>2014-03-24 18:16:51 +0100
committerGerrit Code Review <review@openstack.org>2014-03-28 15:48:50 +0000
commit12f86fdcc57896bac62d36c74a80fd19adeeac58 (patch)
treeedf38583f4ad8925e5d3b4162e77f4de13b9a1ed
parentcdf6f84c360088d39af1b8e1745c102fc44ac362 (diff)
downloadpython-swiftclient-12f86fdcc57896bac62d36c74a80fd19adeeac58.tar.gz
Python 3: Get compatible types from six
* Replace unicode with six.text_type * Replace basestring with six.string_types * The long type doesn't exist in Python 3 anymore: replace 1L with long(1) and only test this type with Python 2 * Fix quote(): quote the URL if the string is a byte string. Use "bytes" type instead of "str" to be Python 3 compatible. Change-Id: I1df5aa85e4e7d07191fb5c654d52fc4bd8b9f440
-rw-r--r--swiftclient/client.py5
-rw-r--r--swiftclient/utils.py4
-rw-r--r--tests/test_swiftclient.py15
3 files changed, 15 insertions, 9 deletions
diff --git a/swiftclient/client.py b/swiftclient/client.py
index ad77817..c48f90f 100644
--- a/swiftclient/client.py
+++ b/swiftclient/client.py
@@ -28,6 +28,7 @@ from requests.exceptions import RequestException, SSLError
from urllib import quote as _quote
from urlparse import urlparse, urlunparse
from time import sleep, time
+import six
from swiftclient.exceptions import ClientException, InvalidHeadersException
from swiftclient.utils import LengthWrapper
@@ -97,7 +98,7 @@ def quote(value, safe='/'):
Patched version of urllib.quote that encodes utf8 strings before quoting
"""
value = encode_utf8(value)
- if isinstance(value, str):
+ if isinstance(value, bytes):
return _quote(value, safe)
else:
return value
@@ -117,7 +118,7 @@ def validate_headers(headers):
def encode_utf8(value):
- if isinstance(value, unicode):
+ if isinstance(value, six.text_type):
value = value.encode('utf8')
return value
diff --git a/swiftclient/utils.py b/swiftclient/utils.py
index 5095f9d..058181d 100644
--- a/swiftclient/utils.py
+++ b/swiftclient/utils.py
@@ -14,6 +14,8 @@
# limitations under the License.
"""Miscellaneous utility functions for use with Swift."""
+import six
+
TRUE_VALUES = set(('true', '1', 'yes', 'on', 't', 'y'))
@@ -24,7 +26,7 @@ def config_true_value(value):
This function come from swift.common.utils.config_true_value()
"""
return value is True or \
- (isinstance(value, basestring) and value.lower() in TRUE_VALUES)
+ (isinstance(value, six.string_types) and value.lower() in TRUE_VALUES)
def prt_bytes(bytes, human_flag):
diff --git a/tests/test_swiftclient.py b/tests/test_swiftclient.py
index 8f70947..a31026e 100644
--- a/tests/test_swiftclient.py
+++ b/tests/test_swiftclient.py
@@ -177,9 +177,9 @@ class TestHttpHelpers(MockHttpTest):
def test_quote(self):
value = 'standard string'
- self.assertEqual('standard%20string', c.quote(value))
+ self.assertEqual(b'standard%20string', c.quote(value))
value = u'\u0075nicode string'
- self.assertEqual('unicode%20string', c.quote(value))
+ self.assertEqual(b'unicode%20string', c.quote(value))
def test_http_connection(self):
url = 'http://www.test.com'
@@ -204,7 +204,10 @@ class TestHttpHelpers(MockHttpTest):
headers)
def test_validate_headers_with_other_than_str(self):
- for t in (None, 1, 1.0, 1L, u"A"):
+ values = [None, 1, 1.0, u"A"]
+ if six.PY2:
+ values.append(long(1))
+ for t in values:
self.assertEqual(c.validate_headers({'key': t}),
None)
@@ -572,7 +575,7 @@ class TestPutObject(MockHttpTest):
c.http_connection = self.fake_http_connection(200)
args = ('http://www.test.com', 'asdf', 'asdf', 'asdf', 'asdf')
value = c.put_object(*args)
- self.assertTrue(isinstance(value, basestring))
+ self.assertTrue(isinstance(value, six.string_types))
def test_unicode_ok(self):
conn = c.http_connection(u'http://www.test.com/')
@@ -589,7 +592,7 @@ class TestPutObject(MockHttpTest):
conn[1].getresponse = resp.fake_response
conn[1]._request = resp._fake_request
value = c.put_object(*args, headers=headers, http_conn=conn)
- self.assertTrue(isinstance(value, basestring))
+ self.assertTrue(isinstance(value, six.string_types))
# Test for RFC-2616 encoded symbols
self.assertTrue("a-b: .x:yz mn:fg:lp" in resp.buffer[0],
"[a-b: .x:yz mn:fg:lp] header is missing")
@@ -969,7 +972,7 @@ class TestLogging(MockHttpTest):
c.http_connection = self.fake_http_connection(200)
args = ('http://www.test.com', 'asdf', 'asdf', 'asdf', 'asdf')
value = c.put_object(*args)
- self.assertTrue(isinstance(value, basestring))
+ self.assertTrue(isinstance(value, six.string_types))
def test_head_error(self):
c.http_connection = self.fake_http_connection(500)