-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-127750: Fix and optimize functools.singledispatchmethod() #130008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
7c5bb0e
93d0977
16604a1
259dd9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1026,9 +1026,6 @@ def __init__(self, func): | |
| self.dispatcher = singledispatch(func) | ||
| self.func = func | ||
|
|
||
| import weakref # see comment in singledispatch function | ||
| self._method_cache = weakref.WeakKeyDictionary() | ||
|
|
||
| def register(self, cls, method=None): | ||
| """generic_method.register(cls, func) -> func | ||
|
|
||
|
|
@@ -1037,36 +1034,45 @@ def register(self, cls, method=None): | |
| return self.dispatcher.register(cls, func=method) | ||
|
|
||
| def __get__(self, obj, cls=None): | ||
| if self._method_cache is not None: | ||
| try: | ||
| _method = self._method_cache[obj] | ||
| except TypeError: | ||
| self._method_cache = None | ||
| except KeyError: | ||
| pass | ||
| else: | ||
| return _method | ||
| return _singledispatchmethod_get(self, obj, cls) | ||
|
|
||
| dispatch = self.dispatcher.dispatch | ||
| funcname = getattr(self.func, '__name__', 'singledispatchmethod method') | ||
| def _method(*args, **kwargs): | ||
| if not args: | ||
| raise TypeError(f'{funcname} requires at least ' | ||
| '1 positional argument') | ||
| return dispatch(args[0].__class__).__get__(obj, cls)(*args, **kwargs) | ||
| @property | ||
| def __isabstractmethod__(self): | ||
| return getattr(self.func, '__isabstractmethod__', False) | ||
|
|
||
| _method.__isabstractmethod__ = self.__isabstractmethod__ | ||
| _method.register = self.register | ||
| update_wrapper(_method, self.func) | ||
|
|
||
| if self._method_cache is not None: | ||
| self._method_cache[obj] = _method | ||
| class _singledispatchmethod_get: | ||
| def __init__(self, unbound, obj, cls): | ||
| self._unbound = unbound | ||
| self._dispatch = unbound.dispatcher.dispatch | ||
| self._obj = obj | ||
| self._cls = cls | ||
|
|
||
| return _method | ||
| def __call__(self, /, *args, **kwargs): | ||
| if not args: | ||
| funcname = getattr(self._unbound.func, '__name__', | ||
| 'singledispatchmethod method') | ||
| raise TypeError(f'{funcname} requires at least ' | ||
| '1 positional argument') | ||
| return self._dispatch(args[0].__class__).__get__(self._obj, self._cls)(*args, **kwargs) | ||
|
|
||
| def __getattr__(self, name): | ||
| if name not in {'__name__', '__qualname__', '__isabstractmethod__', | ||
| '__annotations__', '__type_params__'}: | ||
| raise AttributeError | ||
| return getattr(self._unbound.func, name) | ||
|
|
||
| @property | ||
| def __isabstractmethod__(self): | ||
| return getattr(self.func, '__isabstractmethod__', False) | ||
| def __module__(self): | ||
|
||
| return self._unbound.func.__module__ | ||
|
|
||
| @property | ||
| def __doc__(self): | ||
| return self._unbound.func.__doc__ | ||
|
|
||
| @property | ||
| def register(self): | ||
| return self._unbound.register | ||
|
|
||
|
|
||
| ################################################################################ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Remove broken :func:`functools.singledispatchmethod` caching introduced in | ||
| :gh:`85160`. Achieve the same performance using different optimization. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this set is
functools.WRAPPER_ASSIGNMENTSminus__doc__and__module__which have special handling. Fine to leave it this wayThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plus
__isabstractmethod__.