diff options
author | INADA Naoki <methane@users.noreply.github.com> | 2017-09-30 16:13:02 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-30 16:13:02 +0900 |
commit | 9811e80fd0ed9d74c76a66f1dd4e4b8afa9e8f53 (patch) | |
tree | ca58c45f84820067c85f0487719de31b290cdc84 /Lib/functools.py | |
parent | b24cd055ecb3eea9a15405a6ca72dafc739e6531 (diff) | |
download | cpython-git-9811e80fd0ed9d74c76a66f1dd4e4b8afa9e8f53.tar.gz |
bpo-31581: Reduce the number of imports for functools (GH-3757)
Diffstat (limited to 'Lib/functools.py')
-rw-r--r-- | Lib/functools.py | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/Lib/functools.py b/Lib/functools.py index 25075de5b4..a51dddf878 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -19,8 +19,7 @@ except ImportError: pass from abc import get_cache_token from collections import namedtuple -from types import MappingProxyType -from weakref import WeakKeyDictionary +# import types, weakref # Deferred to single_dispatch() from reprlib import recursive_repr from _thread import RLock @@ -753,10 +752,14 @@ def singledispatch(func): function acts as the default implementation, and additional implementations can be registered using the register() attribute of the generic function. - """ + # There are many programs that use functools without singledispatch, so we + # trade-off making singledispatch marginally slower for the benefit of + # making start-up of such applications slightly faster. + import types, weakref + registry = {} - dispatch_cache = WeakKeyDictionary() + dispatch_cache = weakref.WeakKeyDictionary() cache_token = None def dispatch(cls): @@ -803,7 +806,7 @@ def singledispatch(func): registry[object] = func wrapper.register = register wrapper.dispatch = dispatch - wrapper.registry = MappingProxyType(registry) + wrapper.registry = types.MappingProxyType(registry) wrapper._clear_cache = dispatch_cache.clear update_wrapper(wrapper, func) return wrapper |