Commits

ltnooy committed 13671a7

Fixing a problem with utf8 url encoded strings.

  • Participants
  • Parent commits 26b022b

Comments (0)

Files changed (2)

-#encoding: utf-8
+#encoding: utf8
 """PYRQLATE - Python interpretation of RQL queries."""
 
 import datetime
     @classmethod
     def from_python(cls, value):
         lookup = {
-            unicode: lambda x: urllib.quote(x.encode('utf-8')),
+            unicode: lambda x: urllib.quote(x.encode('utf8')),
             bool: lambda x: unicode(x).lower(),
             decimal.Decimal: lambda x: unicode(x),
             datetime.datetime: lambda x: cls.datetime_to_iso(x),
     ]
 
     if not isinstance(rql, unicode):
-        # Note that we are assuming unicode characters are encoded as utf-8.
-        rql = rql.decode('utf-8')
+        # Note that we are assuming unicode characters are encoded as utf8.
+        rql = rql.decode('utf8')
  
     # Search left to right for an appropriate value.
     def find_value(uni, start):
     else:
         type = None
 
-    value = urllib.unquote(value)
+    value = urllib.unquote(value.encode('utf8'))
+    value = value.decode('utf8')
 
     if type:
         return Converters.to_python(type, value)
-#encoding: utf-8
+#encoding: utf8
 
 import unittest
 import pyrqlate
+import urllib
 
 #-------------------------------------------------------------------------------
 class TestParse(unittest.TestCase):
         q = pyrqlate.parse('id=орлов')
         self.assertEqual(pyrqlate.deparse(q), 'id=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
 
+        # UTF-8 encoded and URL quoted.
+        q = pyrqlate.parse('id=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
+        self.assertEqual(pyrqlate.deparse(q), 'id=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
+
         # Pre decoded unicode.
         q = pyrqlate.parse(u'id=орлов')
         self.assertEqual(pyrqlate.deparse(q), u'id=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
         # UTF-8 encoded unicode.
         q = pyrqlate.parse('id=ne=орлов')
         self.assertEqual(pyrqlate.deparse(q), 'id=ne=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
+        
+        # UTF-8 encoded and URL quoted.
+        q = pyrqlate.parse('id=ne=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
+        self.assertEqual(pyrqlate.deparse(q), 'id=ne=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
 
         # Pre decoded unicode.
         q = pyrqlate.parse(u'id=ne=орлов')
         # UTF-8 encoded unicode.
         q = pyrqlate.parse('id=lt=орлов')
         self.assertEqual(pyrqlate.deparse(q), 'id=lt=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
+        
+        # UTF-8 encoded and URL quoted.
+        q = pyrqlate.parse('id=lt=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
+        self.assertEqual(pyrqlate.deparse(q), 'id=lt=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
 
         # Pre decoded unicode.
         q = pyrqlate.parse(u'id=lt=орлов')
         # UTF-8 encoded unicode.
         q = pyrqlate.parse('id=le=орлов')
         self.assertEqual(pyrqlate.deparse(q), 'id=le=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
+        
+        # UTF-8 encoded and URL quoted.
+        q = pyrqlate.parse('id=le=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
+        self.assertEqual(pyrqlate.deparse(q), 'id=le=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
 
         # Pre decoded unicode.
         q = pyrqlate.parse(u'id=le=орлов')
         q = pyrqlate.parse('id=gt=орлов')
         self.assertEqual(pyrqlate.deparse(q), 'id=gt=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
 
+        # UTF-8 encoded and URL quoted.
+        q = pyrqlate.parse('id=gt=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
+        self.assertEqual(pyrqlate.deparse(q), 'id=gt=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
+
         # Pre decoded unicode.
         q = pyrqlate.parse(u'id=gt=орлов')
         self.assertEqual(pyrqlate.deparse(q), u'id=gt=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
         q = pyrqlate.parse('id=ge=орлов')
         self.assertEqual(pyrqlate.deparse(q), 'id=ge=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
 
+        # UTF-8 encoded and URL quoted.
+        q = pyrqlate.parse('id=ge=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
+        self.assertEqual(pyrqlate.deparse(q), 'id=ge=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')
+
         # Pre decoded unicode.
         q = pyrqlate.parse(u'id=ge=орлов')
         self.assertEqual(pyrqlate.deparse(q), u'id=ge=%D0%BE%D1%80%D0%BB%D0%BE%D0%B2')