@@ -928,14 +928,14 @@ class singledispatchmethod:
928
928
"""
929
929
930
930
def __init__ (self , func ):
931
- import weakref # see comment in singledispatch function
932
931
if not callable (func ) and not hasattr (func , "__get__" ):
933
932
raise TypeError (f"{ func !r} is not callable or a descriptor" )
934
933
935
934
self .dispatcher = singledispatch (func )
936
935
self .func = func
936
+
937
+ import weakref # see comment in singledispatch function
937
938
self ._method_cache = weakref .WeakKeyDictionary ()
938
- self ._all_weakrefable_instances = True
939
939
940
940
def register (self , cls , method = None ):
941
941
"""generic_method.register(cls, func) -> func
@@ -945,11 +945,11 @@ def register(self, cls, method=None):
945
945
return self .dispatcher .register (cls , func = method )
946
946
947
947
def __get__ (self , obj , cls = None ):
948
- if self ._all_weakrefable_instances :
948
+ if self ._method_cache is not None :
949
949
try :
950
950
_method = self ._method_cache [obj ]
951
951
except TypeError :
952
- self ._all_weakrefable_instances = False
952
+ self ._method_cache = None
953
953
except KeyError :
954
954
pass
955
955
else :
@@ -963,7 +963,7 @@ def _method(*args, **kwargs):
963
963
_method .register = self .register
964
964
update_wrapper (_method , self .func )
965
965
966
- if self ._all_weakrefable_instances :
966
+ if self ._method_cache is not None :
967
967
self ._method_cache [obj ] = _method
968
968
969
969
return _method
0 commit comments