cython: fixed problems with cythonized decorators on py2....
marcink -
r4175:a79bcd0c default
Not Reviewed
Show More
Add another comment
TODOs: 0 unresolved 0 Resolved
COMMENTS: 0 General 0 Inline
@@ -21,7 +21,6
21 import time
21 import time
22 import logging
22 import logging
23 import functools
23 import functools
24 from decorator import decorate
25 import threading
24 import threading
26
25
27 from dogpile.cache import CacheRegion
26 from dogpile.cache import CacheRegion
@@ -38,6 +37,13
38 log = logging.getLogger(__name__)
37 log = logging.getLogger(__name__)
39
38
40
39
40 def isCython(func):
41 """
42 Private helper that checks if a function is a cython function.
43 """
44 return func.__class__.__name__ == 'cython_function_or_method'
45
46
41 class RhodeCodeCacheRegion(CacheRegion):
47 class RhodeCodeCacheRegion(CacheRegion):
42
48
43 def conditional_cache_on_arguments(
49 def conditional_cache_on_arguments(
@@ -57,6 +63,61
57 if function_key_generator is None:
63 if function_key_generator is None:
58 function_key_generator = self.function_key_generator
64 function_key_generator = self.function_key_generator
59
65
66 # workaround for py2 and cython problems, this block should be removed
67 # once we've migrated to py3
68 if 'cython' == 'cython':
69 def decorator(fn):
70 if to_str is compat.string_type:
71 # backwards compatible
72 key_generator = function_key_generator(namespace, fn)
73 else:
74 key_generator = function_key_generator(namespace, fn, to_str=to_str)
75
76 @functools.wraps(fn)
77 def decorate(*arg, **kw):
78 key = key_generator(*arg, **kw)
79
80 @functools.wraps(fn)
81 def creator():
82 return fn(*arg, **kw)
83
84 if not condition:
85 return creator()
86
87 timeout = expiration_time() if expiration_time_is_callable \
88 else expiration_time
89
90 return self.get_or_create(key, creator, timeout, should_cache_fn)
91
92 def invalidate(*arg, **kw):
93 key = key_generator(*arg, **kw)
94 self.delete(key)
95
96 def set_(value, *arg, **kw):
97 key = key_generator(*arg, **kw)
98 self.set(key, value)
99
100 def get(*arg, **kw):
101 key = key_generator(*arg, **kw)
102 return self.get(key)
103
104 def refresh(*arg, **kw):
105 key = key_generator(*arg, **kw)
106 value = fn(*arg, **kw)
107 self.set(key, value)
108 return value
109
110 decorate.set = set_
111 decorate.invalidate = invalidate
112 decorate.refresh = refresh
113 decorate.get = get
114 decorate.original = fn
115 decorate.key_generator = key_generator
116 decorate.__wrapped__ = fn
117
118 return decorate
119 return decorator
120
60 def get_or_create_for_user_func(key_generator, user_func, *arg, **kw):
121 def get_or_create_for_user_func(key_generator, user_func, *arg, **kw):
61
122
62 if not condition:
123 if not condition:
@@ -107,8 +168,7
107 user_func.original = user_func
168 user_func.original = user_func
108
169
109 # Use `decorate` to preserve the signature of :param:`user_func`.
170 # Use `decorate` to preserve the signature of :param:`user_func`.
110
171 return decorator.decorate(user_func, functools.partial(
111 return decorate(user_func, functools.partial(
112 get_or_create_for_user_func, key_generator))
172 get_or_create_for_user_func, key_generator))
113
173
114 return cache_decorator
174 return cache_decorator
Comments 0
You need to be logged in to leave comments. Login now