From 3a9bdbfa6ea78d42a627fb5e8a5b6ce1b6e408ac Mon Sep 17 00:00:00 2001 From: Kp Date: Mon, 15 Jun 2020 00:21:35 +0000 Subject: [PATCH] Use __slots__ for cached_property Remove the redundant storage of the method's name, since it is only read once in the getter. --- SConstruct | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/SConstruct b/SConstruct index bed92e0b7..5995813fb 100644 --- a/SConstruct +++ b/SConstruct @@ -2861,18 +2861,27 @@ BOOST_AUTO_TEST_CASE(f) ConfigureTests.register_preferred_compiler_options() class cached_property(object): + __slots__ = 'method', def __init__(self,f): self.method = f - self.name = f.__name__ def __get__(self,instance,cls): # This should never be accessed directly on the class. assert instance is not None - name = self.name + method = self.method + name = method.__name__ + # Python will rewrite double-underscore references to the + # attribute while processing references inside the class, but + # will not rewrite the key used to store the computed value + # below, so subsequent accesses will not find the computed value + # and will instead call this getter again. For now, disable + # attempts to use double-underscore properties instead of trying + # to handle their names correctly. + assert not name.startswith('__') d = instance.__dict__ # After the first access, Python should find the cached value in # the instance dictionary instead of calling __get__ again. assert name not in d - d[name] = r = self.method(instance) + d[name] = r = method(instance) return r class LazyObjectConstructor(object):