summaryrefslogtreecommitdiff
path: root/singledispatch.py
diff options
context:
space:
mode:
author?ukasz Langa <lukasz@langa.pl>2013-05-26 00:02:51 +0200
committer?ukasz Langa <lukasz@langa.pl>2013-05-26 00:02:51 +0200
commit6a3ac50c476620e9aed3ff3c70ba539e7b6d7d1c (patch)
treee180e320d97d3cef67d265a0266d69a5d84a790e /singledispatch.py
parentffa12b2ae9b9b4ae1e2519414cf784e896832424 (diff)
downloadsingledispatch-6a3ac50c476620e9aed3ff3c70ba539e7b6d7d1c.tar.gz
make tests pass on Python 2.6 - 3.33.4.0.0
Diffstat (limited to 'singledispatch.py')
-rw-r--r--singledispatch.py19
1 files changed, 9 insertions, 10 deletions
diff --git a/singledispatch.py b/singledispatch.py
index 5093904..e1009b9 100644
--- a/singledispatch.py
+++ b/singledispatch.py
@@ -47,7 +47,8 @@ def singledispatch(func):
"""
registry = {}
dispatch_cache = WeakKeyDictionary()
- cache_token = None
+ def ns(): pass
+ ns.cache_token = None
def dispatch(cls):
"""generic_func.dispatch(type) -> <function implementation>
@@ -56,7 +57,7 @@ def singledispatch(func):
for the given `type` registered on `generic_func`.
"""
- if cache_token is not None:
+ if ns.cache_token is not None:
mro = _compose_mro(cls, registry.keys())
match = None
for t in mro:
@@ -68,7 +69,7 @@ def singledispatch(func):
and match not in cls.__mro__):
# `match` is an ABC but there is another unrelated, equally
# matching ABC. Refuse the temptation to guess.
- raise RuntimeError("Ambiguous dispatch: {} or {}".format(
+ raise RuntimeError("Ambiguous dispatch: {0} or {1}".format(
match, t))
return registry[match]
else:
@@ -78,12 +79,11 @@ def singledispatch(func):
return func
def wrapper(*args, **kw):
- nonlocal cache_token
- if cache_token is not None:
+ if ns.cache_token is not None:
current_token = get_cache_token()
- if cache_token != current_token:
+ if ns.cache_token != current_token:
dispatch_cache.clear()
- cache_token = current_token
+ ns.cache_token = current_token
cls = args[0].__class__
try:
impl = dispatch_cache[cls]
@@ -97,12 +97,11 @@ def singledispatch(func):
Registers a new overload for the given `type` on a `generic_func`.
"""
- nonlocal cache_token
if func is None:
return lambda f: register(typ, f)
registry[typ] = func
- if cache_token is None and hasattr(typ, '__abstractmethods__'):
- cache_token = get_cache_token()
+ if ns.cache_token is None and hasattr(typ, '__abstractmethods__'):
+ ns.cache_token = get_cache_token()
dispatch_cache.clear()
return func