aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/functools.py
diff options
context:
space:
mode:
authorAlex Waygood <Alex.Waygood@Gmail.com>2023-08-07 13:46:36 +0100
committerGitHub <noreply@github.com>2023-08-07 13:46:36 +0100
commit2ac103c346ffe9d0e4c146402ce215c5ce6c1ef2 (patch)
treef02a8fcf52cb35b584799692a9b2b85fc0b36a98 /Lib/functools.py
parent33cb0b06efe33968eb32463fa1b02b5a729a17f8 (diff)
downloadcpython-2ac103c346ffe9d0e4c146402ce215c5ce6c1ef2.tar.gz
cpython-2ac103c346ffe9d0e4c146402ce215c5ce6c1ef2.zip
gh-85160: Reduce memory usage of `singledispatchmethod` (#107706)
A small followup to #107148 Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/functools.py')
-rw-r--r--Lib/functools.py10
1 files changed, 5 insertions, 5 deletions
diff --git a/Lib/functools.py b/Lib/functools.py
index 2a8a69b3c52..be44ccdae6b 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -928,14 +928,14 @@ class singledispatchmethod:
"""
def __init__(self, func):
- import weakref # see comment in singledispatch function
if not callable(func) and not hasattr(func, "__get__"):
raise TypeError(f"{func!r} is not callable or a descriptor")
self.dispatcher = singledispatch(func)
self.func = func
+
+ import weakref # see comment in singledispatch function
self._method_cache = weakref.WeakKeyDictionary()
- self._all_weakrefable_instances = True
def register(self, cls, method=None):
"""generic_method.register(cls, func) -> func
@@ -945,11 +945,11 @@ class singledispatchmethod:
return self.dispatcher.register(cls, func=method)
def __get__(self, obj, cls=None):
- if self._all_weakrefable_instances:
+ if self._method_cache is not None:
try:
_method = self._method_cache[obj]
except TypeError:
- self._all_weakrefable_instances = False
+ self._method_cache = None
except KeyError:
pass
else:
@@ -963,7 +963,7 @@ class singledispatchmethod:
_method.register = self.register
update_wrapper(_method, self.func)
- if self._all_weakrefable_instances:
+ if self._method_cache is not None:
self._method_cache[obj] = _method
return _method