Skip to content

Commit

Permalink
[PR #9054/c3da10cd backport][3.10] Sync reify Cython implementation w…
Browse files Browse the repository at this point in the history
…ith yarl (#9057)

Co-authored-by: J. Nick Koston <nick@koston.org>
  • Loading branch information
patchback[bot] and bdraco authored Sep 7, 2024
1 parent 4d022e4 commit f631015
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES/9054.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of reify Cython implementation -- by :user:`bdraco`.
22 changes: 11 additions & 11 deletions aiohttp/_helpers.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

cdef _sentinel = object()

cdef class reify:
"""Use as a class method decorator. It operates almost exactly like
the Python `@property` decorator, but it puts the result of the
Expand All @@ -19,17 +22,14 @@ cdef class reify:
return self.wrapped.__doc__

def __get__(self, inst, owner):
try:
try:
return inst._cache[self.name]
except KeyError:
val = self.wrapped(inst)
inst._cache[self.name] = val
return val
except AttributeError:
if inst is None:
return self
raise
if inst is None:
return self
cdef dict cache = inst._cache
val = cache.get(self.name, _sentinel)
if val is _sentinel:
val = self.wrapped(inst)
cache[self.name] = val
return val

def __set__(self, inst, value):
raise AttributeError("reified property is read-only")

0 comments on commit f631015

Please sign in to comment.