@@ -10,25 +10,27 @@ from cpython.dict cimport (
10
10
cdef class CachedProperty:
11
11
12
12
cdef readonly:
13
- object func, name, __doc__
13
+ object func, name
14
14
15
15
def __init__ (self , func ):
16
16
self .func = func
17
17
self .name = func.__name__
18
- self .__doc__ = getattr (func, ' __doc__' , None )
19
18
20
19
def __get__ (self , obj , typ ):
21
20
if obj is None :
22
21
# accessed on the class, not the instance
23
- return self
22
+ return self .func
24
23
25
24
# Get the cache or set a default one if needed
26
- cache = getattr (obj, ' _cache ' , None )
25
+ cache = getattr (obj, ' _cache_property ' , None )
27
26
if cache is None :
28
27
try :
29
- cache = obj._cache = {}
28
+ cache = obj._cache_property = {}
30
29
except (AttributeError ):
31
- return self
30
+ raise raise TypeError (
31
+ f" Cython extension type {type(obj)} must declare attribute "
32
+ " `_cache_property` to use ``@cache_readonly``."
33
+ )
32
34
33
35
if PyDict_Contains(cache, self .name):
34
36
# not necessary to Py_INCREF
@@ -41,6 +43,10 @@ cdef class CachedProperty:
41
43
def __set__ (self , obj , value ):
42
44
raise AttributeError (" Can't set attribute" )
43
45
46
+ cdef class PropertyCacheMixin:
47
+
48
+ def __cinit__ (self ):
49
+ self ._cache_property = {}
44
50
45
51
cache_readonly = CachedProperty
46
52
0 commit comments