bpo-31581: Reduce the number of imports for functools (GH-3757)

This commit is contained in:
INADA Naoki 2017-09-30 16:13:02 +09:00 committed by GitHub
parent b24cd055ec
commit 9811e80fd0
2 changed files with 92 additions and 88 deletions

View File

@ -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

View File

@ -2019,6 +2019,8 @@ class TestSingleDispatch(unittest.TestCase):
def test_cache_invalidation(self):
from collections import UserDict
import weakref
class TracingDict(UserDict):
def __init__(self, *args, **kwargs):
super(TracingDict, self).__init__(*args, **kwargs)
@ -2033,9 +2035,9 @@ class TestSingleDispatch(unittest.TestCase):
self.data[key] = value
def clear(self):
self.data.clear()
_orig_wkd = functools.WeakKeyDictionary
td = TracingDict()
functools.WeakKeyDictionary = lambda: td
with support.swap_attr(weakref, "WeakKeyDictionary", lambda: td):
c = collections.abc
@functools.singledispatch
def g(arg):
@ -2116,7 +2118,6 @@ class TestSingleDispatch(unittest.TestCase):
self.assertEqual(g(l), "list")
g._clear_cache()
self.assertEqual(len(td), 0)
functools.WeakKeyDictionary = _orig_wkd
if __name__ == '__main__':