diff -r b671ac7ae620 Lib/test/libregrtest/refleak.py --- a/Lib/test/libregrtest/refleak.py Wed Nov 09 09:43:08 2016 -0500 +++ b/Lib/test/libregrtest/refleak.py Wed Nov 09 20:48:40 2016 +0200 @@ -184,6 +184,14 @@ def dash_R_cleanup(fs, ps, pic, zdc, abc else: ctypes._reset_cache() + try: + typing = sys.modules['typing'] + except KeyError: + pass + else: + for f in typing._cleanups: + f() + # Collect cyclic trash and read memory statistics immediately after. func1 = sys.getallocatedblocks func2 = sys.gettotalrefcount diff -r b671ac7ae620 Lib/typing.py --- a/Lib/typing.py Wed Nov 09 09:43:08 2016 -0500 +++ b/Lib/typing.py Wed Nov 09 20:48:40 2016 +0200 @@ -613,12 +613,15 @@ def _check_generic(cls, parameters): ("many" if alen > elen else "few", repr(cls), alen, elen)) +_cleanups = [] + def _tp_cache(func): """ Caching for __getitem__ of generic types with a fallback to original function for non-hashable arguments. """ cached = functools.lru_cache()(func) + _cleanups.append(cached.cache_clear) @functools.wraps(func) def inner(*args, **kwds): try: