libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject [1/2] git commit: [LIBCLOUD-429] Fix libcloud.utils.py3.urlquote so it works with unicode strings under Python 2.
Date Mon, 04 Nov 2013 20:14:20 GMT
Updated Branches:
  refs/heads/trunk 69c5429c1 -> 93fde1568


[LIBCLOUD-429] Fix libcloud.utils.py3.urlquote so it works with unicode
strings under Python 2.


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/3e012e37
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/3e012e37
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/3e012e37

Branch: refs/heads/trunk
Commit: 3e012e37565c8b8a0d62fc8265d82b2c3b4e9744
Parents: 69c5429
Author: Michael Farrell <micolous+git@gmail.com>
Authored: Wed Oct 30 13:00:44 2013 +1030
Committer: Tomaz Muraus <tomaz@apache.org>
Committed: Mon Nov 4 20:11:08 2013 +0000

----------------------------------------------------------------------
 libcloud/test/test_utils.py | 21 +++++++++++++++++++++
 libcloud/utils/py3.py       | 11 ++++++++++-
 2 files changed, 31 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/3e012e37/libcloud/test/test_utils.py
----------------------------------------------------------------------
diff --git a/libcloud/test/test_utils.py b/libcloud/test/test_utils.py
index 0265dfb..d5a373b 100644
--- a/libcloud/test/test_utils.py
+++ b/libcloud/test/test_utils.py
@@ -14,6 +14,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+# Allows unicode literals ('' == unicode) on Python 2, for compatibility with
+# Python 3
+from __future__ import unicode_literals
+
 import sys
 import unittest
 import warnings
@@ -29,9 +33,11 @@ from libcloud.utils.misc import get_driver, set_driver
 from libcloud.utils.py3 import PY3
 from libcloud.utils.py3 import StringIO
 from libcloud.utils.py3 import b
+from libcloud.utils.py3 import urlquote
 from libcloud.compute.types import Provider
 from libcloud.compute.providers import DRIVERS
 
+
 WARNINGS_BUFFER = []
 
 if PY3:
@@ -204,6 +210,21 @@ class TestUtils(unittest.TestCase):
         result = libcloud.utils.files.exhaust_iterator(iterator=iterator)
         self.assertEqual(result, b(data))
 
+    def test_unicode_urlquote(self):
+        # Regression tests for LIBCLOUD-429
+
+        # Note: this is a unicode literal
+        uri = libcloud.utils.py3.urlquote('\xe9')
+        self.assertEqual(b(uri), b('%C3%A9'))
+
+        # Unicode without unicode characters
+        uri = libcloud.utils.py3.urlquote('~abc')
+        self.assertEqual(b(uri), b('%7Eabc'))
+
+        # Already-encoded bytestring without unicode characters
+        uri = libcloud.utils.py3.urlquote(b('~abc'))
+        self.assertEqual(b(uri), b('%7Eabc'))
+
 
 if __name__ == '__main__':
     sys.exit(unittest.main())

http://git-wip-us.apache.org/repos/asf/libcloud/blob/3e012e37/libcloud/utils/py3.py
----------------------------------------------------------------------
diff --git a/libcloud/utils/py3.py b/libcloud/utils/py3.py
index 5453bfb..6625c86 100644
--- a/libcloud/utils/py3.py
+++ b/libcloud/utils/py3.py
@@ -96,7 +96,7 @@ else:
     import urllib2  # NOQA
     import urlparse  # NOQA
     import xmlrpclib  # NOQA
-    from urllib import quote as urlquote  # NOQA
+    from urllib import quote as _urlquote  # NOQA
     from urllib import unquote as urlunquote  # NOQA
     from urllib import urlencode as urlencode  # NOQA
 
@@ -114,6 +114,9 @@ else:
     if not PY25:
         from os.path import relpath  # NOQA
 
+    # Save the real value of unicode because urlquote needs it to tell the
+    # difference between a unicode string and a byte string.
+    _real_unicode = unicode
     basestring = unicode = str
 
     method_type = types.MethodType
@@ -133,6 +136,12 @@ else:
 
     tostring = ET.tostring
 
+    def urlquote(s, safe='/'):
+        if isinstance(s, _real_unicode):
+            # Pretend to be py3 by encoding the URI automatically.
+            s = s.encode('utf8')
+        return _urlquote(s, safe)
+
 if PY25:
     import posixpath
 


Mime
View raw message