Debian Codegraph @

third_party/python/aiohttp/aiohttp/_helpers.pyx

raw
1
2 cdef _sentinel = object()
3
4 cdef class reify:
5 """Use as a class method decorator. It operates almost exactly like
6 the Python `@property` decorator, but it puts the result of the
7 method it decorates into the instance dict after the first call,
8 effectively replacing the function it decorates with an instance
9 variable. It is, in Python parlance, a data descriptor.
10
11 """
12
13 cdef object wrapped
14 cdef object name
15
16 def __init__(self, wrapped):
17 self.wrapped = wrapped
18 self.name = wrapped.__name__
19
20 @property
21 def __doc__(self):
22 return self.wrapped.__doc__
23
24 def __get__(self, inst, owner):
25 if inst is None:
26 return self
27 cdef dict cache = inst._cache
28 val = cache.get(self.name, _sentinel)
29 if val is _sentinel:
30 val = self.wrapped(inst)
31 cache[self.name] = val
32 return val
33
34 def __set__(self, inst, value):
35 raise AttributeError("reified property is read-only")