Commits

Mike Bayer committed 40a01e2 Merge

- changelog
- tests for unicode keys

Comments (0)

Files changed (4)

docs/build/changelog.rst

     :version: 0.5.0
 
     .. change::
+        :tags: bug
+
+      :meth:`.CacheRegion.cache_on_arguments` can now accept
+      non-ascii unicode arguments and correctly produce a concatenated
+      cache key.  Courtesy Lx Yu.
+
+    .. change::
         :tags: feature
 
       Now using the ``Lock`` included with the Python

dogpile/cache/compat.py

     def u(s):
         return s
 
+    def ue(s):
+        return s
+
     import configparser
     import io
     import _thread as thread
     def u(s):
         return unicode(s, "utf-8")
 
+    def ue(s):
+        return unicode(s, "unicode_escape")
+
     import ConfigParser as configparser
     import StringIO as io
 

dogpile/cache/util.py

                     "function does not accept keyword arguments.")
         if has_self:
             args = args[1:]
-        return namespace + "|" + " ".join(map(compat.string_type, args))
+        return namespace + "|" + " ".join(map(compat.text_type, args))
     return generate_key
 
 def function_multi_key_generator(namespace, fn):
                     "function does not accept keyword arguments.")
         if has_self:
             args = args[1:]
-        return [namespace + "|" + key for key in map(compat.string_type, args)]
+        return [namespace + "|" + key for key in map(compat.text_type, args)]
     return generate_keys
 
 def sha1_mangle_key(key):

tests/cache/test_decorator.py

             return fn
         return decorate, canary
 
+    def _multi_keygen_decorator(self, namespace=None):
+        canary = []
+        def decorate(fn):
+            canary.append(util.function_multi_key_generator(namespace, fn))
+            return fn
+        return decorate, canary
+
     def test_keygen_fn(self):
         decorate, canary = self._keygen_decorator()
 
         eq_(gen(1, 2), "tests.cache.test_decorator:one|1 2")
         eq_(gen(None, 5), "tests.cache.test_decorator:one|None 5")
 
+    def test_multi_keygen_fn(self):
+        decorate, canary = self._multi_keygen_decorator()
+
+        @decorate
+        def one(a, b):
+            pass
+        gen = canary[0]
+
+        eq_(gen(1, 2), [
+                "tests.cache.test_decorator:one|1",
+                "tests.cache.test_decorator:one|2"
+            ])
+
     def test_keygen_fn_namespace(self):
         decorate, canary = self._keygen_decorator("mynamespace")
 
         gen = canary[0]
 
         eq_(gen(compat.u('méil'), compat.u('drôle')),
-                compat.u("tests.cache.test_decorator:one|mynamespace|1 2"))
+                compat.ue("tests.cache.test_decorator:"
+                        "one|mynamespace|m\xe9il dr\xf4le"))
+
+    def test_unicode_key_multi(self):
+        decorate, canary = self._multi_keygen_decorator("mynamespace")
+
+        @decorate
+        def one(a, b):
+            pass
+        gen = canary[0]
+
+        eq_(gen(compat.u('méil'), compat.u('drôle')),
+            [
+                compat.ue('tests.cache.test_decorator:one|mynamespace|m\xe9il'),
+                compat.ue('tests.cache.test_decorator:one|mynamespace|dr\xf4le')
+            ])
 
 
 class CacheDecoratorTest(_GenericBackendFixture, TestCase):
     backend = "mock"
-    #def _region(self, init_args={}, config_args={}, backend="mock"):
-    #    reg = CacheRegion(**init_args)
-    #    reg.configure(backend, **config_args)
-    #    return reg
 
     def test_cache_arg(self):
         reg = self._region()