blob: 67af370d016367fb88f4962850dd05b583b9bef7 [file] [log] [blame]
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001"""functools.py - Tools for working with functions and callable objects
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002"""
3# Python module wrapper for _functools C module
4# to allow utilities written in Python to be added
5# to the functools module.
Łukasz Langa6f692512013-06-05 12:20:24 +02006# Written by Nick Coghlan <ncoghlan at gmail.com>,
7# Raymond Hettinger <python at rcn.com>,
8# and Łukasz Langa <lukasz at langa.pl>.
9# Copyright (C) 2006-2013 Python Software Foundation.
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000010# See C source code for _functools credits/copyright
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000011
Georg Brandl2e7346a2010-07-31 18:09:23 +000012__all__ = ['update_wrapper', 'wraps', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES',
Łukasz Langa6f692512013-06-05 12:20:24 +020013 'total_ordering', 'cmp_to_key', 'lru_cache', 'reduce', 'partial',
14 'singledispatch']
Georg Brandl2e7346a2010-07-31 18:09:23 +000015
Antoine Pitroub5b37142012-11-13 21:35:40 +010016try:
17 from _functools import reduce
18except ImportError:
19 pass
Łukasz Langa6f692512013-06-05 12:20:24 +020020from abc import get_cache_token
Raymond Hettingerec0e9102012-03-16 01:16:31 -070021from collections import namedtuple
Łukasz Langa6f692512013-06-05 12:20:24 +020022from types import MappingProxyType
23from weakref import WeakKeyDictionary
Raymond Hettingercbe88132010-08-14 22:22:10 +000024try:
Raymond Hettingerfd541172013-03-01 03:47:57 -080025 from _thread import RLock
Raymond Hettingercbe88132010-08-14 22:22:10 +000026except:
Raymond Hettinger409f6632013-03-01 23:20:13 -080027 class RLock:
Raymond Hettingerf96b2b02013-03-08 21:11:55 -070028 'Dummy reentrant lock for builds without threads'
Raymond Hettinger409f6632013-03-01 23:20:13 -080029 def __enter__(self): pass
30 def __exit__(self, exctype, excinst, exctb): pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000031
Raymond Hettingerbc8e81d2012-03-17 00:24:09 -070032
33################################################################################
34### update_wrapper() and wraps() decorator
35################################################################################
36
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000037# update_wrapper() and wraps() are tools to help write
38# wrapper functions that can handle naive introspection
39
Meador Ingeff7f64c2011-12-11 22:37:31 -060040WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__qualname__', '__doc__',
41 '__annotations__')
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000042WRAPPER_UPDATES = ('__dict__',)
43def update_wrapper(wrapper,
44 wrapped,
45 assigned = WRAPPER_ASSIGNMENTS,
46 updated = WRAPPER_UPDATES):
47 """Update a wrapper function to look like the wrapped function
48
49 wrapper is the function to be updated
50 wrapped is the original function
51 assigned is a tuple naming the attributes assigned directly
52 from the wrapped function to the wrapper function (defaults to
53 functools.WRAPPER_ASSIGNMENTS)
Thomas Wouters89f507f2006-12-13 04:49:30 +000054 updated is a tuple naming the attributes of the wrapper that
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000055 are updated with the corresponding attribute from the wrapped
56 function (defaults to functools.WRAPPER_UPDATES)
57 """
Nick Coghlan98876832010-08-17 06:17:18 +000058 wrapper.__wrapped__ = wrapped
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000059 for attr in assigned:
Nick Coghlan98876832010-08-17 06:17:18 +000060 try:
61 value = getattr(wrapped, attr)
62 except AttributeError:
63 pass
64 else:
65 setattr(wrapper, attr, value)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000066 for attr in updated:
Thomas Wouters89f507f2006-12-13 04:49:30 +000067 getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000068 # Return the wrapper so this can be used as a decorator via partial()
69 return wrapper
70
71def wraps(wrapped,
72 assigned = WRAPPER_ASSIGNMENTS,
73 updated = WRAPPER_UPDATES):
74 """Decorator factory to apply update_wrapper() to a wrapper function
75
76 Returns a decorator that invokes update_wrapper() with the decorated
77 function as the wrapper argument and the arguments to wraps() as the
78 remaining arguments. Default arguments are as for update_wrapper().
79 This is a convenience function to simplify applying partial() to
80 update_wrapper().
81 """
82 return partial(update_wrapper, wrapped=wrapped,
83 assigned=assigned, updated=updated)
Raymond Hettingerc50846a2010-04-05 18:56:31 +000084
Raymond Hettingerbc8e81d2012-03-17 00:24:09 -070085
86################################################################################
87### total_ordering class decorator
88################################################################################
89
Raymond Hettingerc50846a2010-04-05 18:56:31 +000090def total_ordering(cls):
Georg Brandle5a26732010-05-19 21:06:36 +000091 """Class decorator that fills in missing ordering methods"""
Raymond Hettingerc50846a2010-04-05 18:56:31 +000092 convert = {
Raymond Hettinger23f9fc32011-01-08 07:01:56 +000093 '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)),
94 ('__le__', lambda self, other: self < other or self == other),
Raymond Hettingerc50846a2010-04-05 18:56:31 +000095 ('__ge__', lambda self, other: not self < other)],
Raymond Hettinger23f9fc32011-01-08 07:01:56 +000096 '__le__': [('__ge__', lambda self, other: not self <= other or self == other),
97 ('__lt__', lambda self, other: self <= other and not self == other),
Raymond Hettingerc50846a2010-04-05 18:56:31 +000098 ('__gt__', lambda self, other: not self <= other)],
Raymond Hettinger23f9fc32011-01-08 07:01:56 +000099 '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)),
100 ('__ge__', lambda self, other: self > other or self == other),
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000101 ('__le__', lambda self, other: not self > other)],
Raymond Hettinger23f9fc32011-01-08 07:01:56 +0000102 '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other),
103 ('__gt__', lambda self, other: self >= other and not self == other),
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000104 ('__lt__', lambda self, other: not self >= other)]
105 }
Raymond Hettinger3255c632010-09-16 00:31:21 +0000106 # Find user-defined comparisons (not those inherited from object).
Raymond Hettinger1006bd42010-09-14 22:55:13 +0000107 roots = [op for op in convert if getattr(cls, op, None) is not getattr(object, op, None)]
Raymond Hettinger56de7e22010-04-10 16:59:03 +0000108 if not roots:
109 raise ValueError('must define at least one ordering operation: < > <= >=')
110 root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000111 for opname, opfunc in convert[root]:
112 if opname not in roots:
113 opfunc.__name__ = opname
114 opfunc.__doc__ = getattr(int, opname).__doc__
115 setattr(cls, opname, opfunc)
116 return cls
117
Raymond Hettingerbc8e81d2012-03-17 00:24:09 -0700118
119################################################################################
120### cmp_to_key() function converter
121################################################################################
122
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000123def cmp_to_key(mycmp):
Georg Brandle5a26732010-05-19 21:06:36 +0000124 """Convert a cmp= function into a key= function"""
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000125 class K(object):
Raymond Hettingera0d1d962011-03-21 17:50:28 -0700126 __slots__ = ['obj']
Raymond Hettinger7ab9e222011-04-05 02:33:54 -0700127 def __init__(self, obj):
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000128 self.obj = obj
129 def __lt__(self, other):
130 return mycmp(self.obj, other.obj) < 0
131 def __gt__(self, other):
132 return mycmp(self.obj, other.obj) > 0
133 def __eq__(self, other):
134 return mycmp(self.obj, other.obj) == 0
135 def __le__(self, other):
136 return mycmp(self.obj, other.obj) <= 0
137 def __ge__(self, other):
138 return mycmp(self.obj, other.obj) >= 0
139 def __ne__(self, other):
140 return mycmp(self.obj, other.obj) != 0
Raymond Hettinger003be522011-05-03 11:01:32 -0700141 __hash__ = None
Raymond Hettingerc50846a2010-04-05 18:56:31 +0000142 return K
Georg Brandl2e7346a2010-07-31 18:09:23 +0000143
Raymond Hettinger7ab9e222011-04-05 02:33:54 -0700144try:
145 from _functools import cmp_to_key
146except ImportError:
147 pass
148
Raymond Hettingerbc8e81d2012-03-17 00:24:09 -0700149
150################################################################################
Antoine Pitroub5b37142012-11-13 21:35:40 +0100151### partial() argument application
152################################################################################
153
154def partial(func, *args, **keywords):
155 """new function with partial application of the given arguments
156 and keywords.
157 """
158 def newfunc(*fargs, **fkeywords):
159 newkeywords = keywords.copy()
160 newkeywords.update(fkeywords)
161 return func(*(args + fargs), **newkeywords)
162 newfunc.func = func
163 newfunc.args = args
164 newfunc.keywords = keywords
165 return newfunc
166
167try:
168 from _functools import partial
169except ImportError:
170 pass
171
172
173################################################################################
Raymond Hettingerbc8e81d2012-03-17 00:24:09 -0700174### LRU Cache function decorator
175################################################################################
176
Raymond Hettingerdce583e2012-03-16 22:12:20 -0700177_CacheInfo = namedtuple("CacheInfo", ["hits", "misses", "maxsize", "currsize"])
Nick Coghlan234515a2010-11-30 06:19:46 +0000178
Raymond Hettinger0c9050c2012-06-04 00:21:14 -0700179class _HashedSeq(list):
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700180 """ This class guarantees that hash() will be called no more than once
181 per element. This is important because the lru_cache() will hash
182 the key multiple times on a cache miss.
183
184 """
185
Raymond Hettinger9acbb602012-04-30 22:32:16 -0700186 __slots__ = 'hashvalue'
187
Raymond Hettinger0c9050c2012-06-04 00:21:14 -0700188 def __init__(self, tup, hash=hash):
189 self[:] = tup
190 self.hashvalue = hash(tup)
Raymond Hettinger9acbb602012-04-30 22:32:16 -0700191
192 def __hash__(self):
193 return self.hashvalue
194
Raymond Hettinger0c9050c2012-06-04 00:21:14 -0700195def _make_key(args, kwds, typed,
196 kwd_mark = (object(),),
197 fasttypes = {int, str, frozenset, type(None)},
198 sorted=sorted, tuple=tuple, type=type, len=len):
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700199 """Make a cache key from optionally typed positional and keyword arguments
200
201 The key is constructed in a way that is flat as possible rather than
202 as a nested structure that would take more memory.
203
204 If there is only a single argument and its data type is known to cache
205 its hash value, then that argument is returned without a wrapper. This
206 saves space and improves lookup speed.
207
208 """
Raymond Hettinger0c9050c2012-06-04 00:21:14 -0700209 key = args
210 if kwds:
211 sorted_items = sorted(kwds.items())
212 key += kwd_mark
213 for item in sorted_items:
214 key += item
215 if typed:
216 key += tuple(type(v) for v in args)
217 if kwds:
218 key += tuple(type(v) for k, v in sorted_items)
219 elif len(key) == 1 and type(key[0]) in fasttypes:
220 return key[0]
221 return _HashedSeq(key)
222
Raymond Hettinger010ce322012-05-19 21:20:48 -0700223def lru_cache(maxsize=128, typed=False):
Benjamin Peterson1f594ad2010-08-08 13:17:07 +0000224 """Least-recently-used cache decorator.
Georg Brandl2e7346a2010-07-31 18:09:23 +0000225
Raymond Hettingerc79fb0e2010-12-01 03:45:41 +0000226 If *maxsize* is set to None, the LRU features are disabled and the cache
227 can grow without bound.
228
Raymond Hettingercd9fdfd2011-10-20 08:57:45 -0700229 If *typed* is True, arguments of different types will be cached separately.
230 For example, f(3.0) and f(3) will be treated as distinct calls with
231 distinct results.
232
Georg Brandl2e7346a2010-07-31 18:09:23 +0000233 Arguments to the cached function must be hashable.
Raymond Hettinger5fa40c02010-11-25 08:11:57 +0000234
Raymond Hettinger7f7a5a72012-03-30 21:50:40 -0700235 View the cache statistics named tuple (hits, misses, maxsize, currsize)
236 with f.cache_info(). Clear the cache and statistics with f.cache_clear().
Raymond Hettinger00f2f972010-12-01 00:47:56 +0000237 Access the underlying function with f.__wrapped__.
Raymond Hettinger5fa40c02010-11-25 08:11:57 +0000238
239 See: http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
Georg Brandl2e7346a2010-07-31 18:09:23 +0000240
Benjamin Peterson1f594ad2010-08-08 13:17:07 +0000241 """
Raymond Hettinger1ff50df2012-03-30 13:15:48 -0700242
Raymond Hettinger5fa40c02010-11-25 08:11:57 +0000243 # Users should only access the lru_cache through its public API:
Raymond Hettinger5e20bab2010-11-30 07:13:04 +0000244 # cache_info, cache_clear, and f.__wrapped__
Raymond Hettinger5fa40c02010-11-25 08:11:57 +0000245 # The internals of the lru_cache are encapsulated for thread safety and
246 # to allow the implementation to change (including a possible C version).
247
Raymond Hettinger9f0ab9f2012-04-29 14:55:27 -0700248 # Constants shared by all lru cache instances:
Raymond Hettingerb6b98c02012-04-29 18:09:02 -0700249 sentinel = object() # unique object used to signal cache misses
Raymond Hettinger0c9050c2012-06-04 00:21:14 -0700250 make_key = _make_key # build a key from the function arguments
Raymond Hettinger9f0ab9f2012-04-29 14:55:27 -0700251 PREV, NEXT, KEY, RESULT = 0, 1, 2, 3 # names for the link fields
252
Raymond Hettinger6e8c8172012-03-16 16:53:05 -0700253 def decorating_function(user_function):
Raymond Hettinger7f7a5a72012-03-30 21:50:40 -0700254 cache = {}
Raymond Hettinger832edde2013-02-17 00:08:45 -0800255 hits = misses = 0
Raymond Hettinger018b4fb2012-04-30 20:48:55 -0700256 full = False
Raymond Hettingerc6897852012-03-31 02:19:06 -0700257 cache_get = cache.get # bound method to lookup a key or return None
Raymond Hettingerfd541172013-03-01 03:47:57 -0800258 lock = RLock() # because linkedlist updates aren't threadsafe
Raymond Hettinger7f7a5a72012-03-30 21:50:40 -0700259 root = [] # root of the circular doubly linked list
260 root[:] = [root, root, None, None] # initialize by pointing to self
Raymond Hettinger6e8c8172012-03-16 16:53:05 -0700261
Raymond Hettinger7e0c5812012-03-17 15:10:24 -0700262 if maxsize == 0:
263
Raymond Hettinger7e0c5812012-03-17 15:10:24 -0700264 def wrapper(*args, **kwds):
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700265 # No caching -- just a statistics update after a successful call
Raymond Hettinger7e0c5812012-03-17 15:10:24 -0700266 nonlocal misses
Raymond Hettinger7dabfed2012-03-17 15:11:09 -0700267 result = user_function(*args, **kwds)
Raymond Hettinger7e0c5812012-03-17 15:10:24 -0700268 misses += 1
269 return result
270
271 elif maxsize is None:
Raymond Hettingerbc8e81d2012-03-17 00:24:09 -0700272
Raymond Hettingerc79fb0e2010-12-01 03:45:41 +0000273 def wrapper(*args, **kwds):
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700274 # Simple caching without ordering or size limit
Raymond Hettinger832edde2013-02-17 00:08:45 -0800275 nonlocal hits, misses
Raymond Hettinger9acbb602012-04-30 22:32:16 -0700276 key = make_key(args, kwds, typed)
Raymond Hettinger7f7a5a72012-03-30 21:50:40 -0700277 result = cache_get(key, sentinel)
278 if result is not sentinel:
Nick Coghlan234515a2010-11-30 06:19:46 +0000279 hits += 1
Raymond Hettinger4b779b32011-10-15 23:50:42 -0700280 return result
Raymond Hettinger4b779b32011-10-15 23:50:42 -0700281 result = user_function(*args, **kwds)
282 cache[key] = result
283 misses += 1
Raymond Hettingerc79fb0e2010-12-01 03:45:41 +0000284 return result
Raymond Hettingerbc8e81d2012-03-17 00:24:09 -0700285
Raymond Hettingerc79fb0e2010-12-01 03:45:41 +0000286 else:
Raymond Hettingerbc8e81d2012-03-17 00:24:09 -0700287
Raymond Hettingerc79fb0e2010-12-01 03:45:41 +0000288 def wrapper(*args, **kwds):
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700289 # Size limited caching that tracks accesses by recency
Raymond Hettinger832edde2013-02-17 00:08:45 -0800290 nonlocal root, hits, misses, full
Raymond Hettinger9acbb602012-04-30 22:32:16 -0700291 key = make_key(args, kwds, typed)
Raymond Hettinger4b779b32011-10-15 23:50:42 -0700292 with lock:
Raymond Hettingerec0e9102012-03-16 01:16:31 -0700293 link = cache_get(key)
294 if link is not None:
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700295 # Move the link to the front of the circular queue
296 link_prev, link_next, _key, result = link
Raymond Hettingerec0e9102012-03-16 01:16:31 -0700297 link_prev[NEXT] = link_next
298 link_next[PREV] = link_prev
299 last = root[PREV]
300 last[NEXT] = root[PREV] = link
301 link[PREV] = last
302 link[NEXT] = root
Raymond Hettingerc79fb0e2010-12-01 03:45:41 +0000303 hits += 1
Raymond Hettinger4b779b32011-10-15 23:50:42 -0700304 return result
Raymond Hettinger4b779b32011-10-15 23:50:42 -0700305 result = user_function(*args, **kwds)
306 with lock:
Raymond Hettinger34d94a22012-04-30 14:14:28 -0700307 if key in cache:
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700308 # Getting here means that this same key was added to the
309 # cache while the lock was released. Since the link
Raymond Hettinger34d94a22012-04-30 14:14:28 -0700310 # update is already done, we need only return the
311 # computed result and update the count of misses.
312 pass
Raymond Hettinger018b4fb2012-04-30 20:48:55 -0700313 elif full:
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700314 # Use the old root to store the new key and result.
Raymond Hettingerf2c17a92013-03-04 03:34:09 -0500315 oldroot = root
316 oldroot[KEY] = key
317 oldroot[RESULT] = result
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700318 # Empty the oldest link and make it the new root.
319 # Keep a reference to the old key and old result to
320 # prevent their ref counts from going to zero during the
321 # update. That will prevent potentially arbitrary object
322 # clean-up code (i.e. __del__) from running while we're
323 # still adjusting the links.
Raymond Hettingerf2c17a92013-03-04 03:34:09 -0500324 root = oldroot[NEXT]
325 oldkey = root[KEY]
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700326 oldresult = root[RESULT]
Raymond Hettingerc6897852012-03-31 02:19:06 -0700327 root[KEY] = root[RESULT] = None
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700328 # Now update the cache dictionary.
Raymond Hettingerf2c17a92013-03-04 03:34:09 -0500329 del cache[oldkey]
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700330 # Save the potentially reentrant cache[key] assignment
331 # for last, after the root and links have been put in
332 # a consistent state.
Raymond Hettingerf2c17a92013-03-04 03:34:09 -0500333 cache[key] = oldroot
Raymond Hettinger018b4fb2012-04-30 20:48:55 -0700334 else:
Raymond Hettingerf96b2b02013-03-08 21:11:55 -0700335 # Put result in a new link at the front of the queue.
Raymond Hettinger018b4fb2012-04-30 20:48:55 -0700336 last = root[PREV]
337 link = [last, root, key, result]
Raymond Hettingerf2c17a92013-03-04 03:34:09 -0500338 last[NEXT] = root[PREV] = cache[key] = link
Raymond Hettingerbb5f4802013-03-04 04:20:46 -0500339 full = (len(cache) >= maxsize)
Raymond Hettingerec0e9102012-03-16 01:16:31 -0700340 misses += 1
Raymond Hettingerc79fb0e2010-12-01 03:45:41 +0000341 return result
Georg Brandl2e7346a2010-07-31 18:09:23 +0000342
Nick Coghlan234515a2010-11-30 06:19:46 +0000343 def cache_info():
Raymond Hettinger5e20bab2010-11-30 07:13:04 +0000344 """Report cache statistics"""
Nick Coghlan234515a2010-11-30 06:19:46 +0000345 with lock:
Raymond Hettinger832edde2013-02-17 00:08:45 -0800346 return _CacheInfo(hits, misses, maxsize, len(cache))
Nick Coghlan234515a2010-11-30 06:19:46 +0000347
Raymond Hettinger02566ec2010-09-04 22:46:06 +0000348 def cache_clear():
Benjamin Peterson1f594ad2010-08-08 13:17:07 +0000349 """Clear the cache and cache statistics"""
Raymond Hettinger832edde2013-02-17 00:08:45 -0800350 nonlocal hits, misses, full
Raymond Hettingercbe88132010-08-14 22:22:10 +0000351 with lock:
352 cache.clear()
Benjamin Peterson954cf572012-03-16 18:22:26 -0500353 root[:] = [root, root, None, None]
Raymond Hettinger832edde2013-02-17 00:08:45 -0800354 hits = misses = 0
Raymond Hettinger018b4fb2012-04-30 20:48:55 -0700355 full = False
Georg Brandl2e7346a2010-07-31 18:09:23 +0000356
Nick Coghlan234515a2010-11-30 06:19:46 +0000357 wrapper.cache_info = cache_info
Raymond Hettinger02566ec2010-09-04 22:46:06 +0000358 wrapper.cache_clear = cache_clear
Raymond Hettinger1ff50df2012-03-30 13:15:48 -0700359 return update_wrapper(wrapper, user_function)
Raymond Hettinger5fa40c02010-11-25 08:11:57 +0000360
Georg Brandl2e7346a2010-07-31 18:09:23 +0000361 return decorating_function
Łukasz Langa6f692512013-06-05 12:20:24 +0200362
363
364################################################################################
365### singledispatch() - single-dispatch generic function decorator
366################################################################################
367
368def _compose_mro(cls, haystack):
369 """Calculates the MRO for a given class `cls`, including relevant abstract
370 base classes from `haystack`.
371
372 """
373 bases = set(cls.__mro__)
374 mro = list(cls.__mro__)
375 for needle in haystack:
376 if (needle in bases or not hasattr(needle, '__mro__')
377 or not issubclass(cls, needle)):
378 continue # either present in the __mro__ already or unrelated
379 for index, base in enumerate(mro):
380 if not issubclass(base, needle):
381 break
382 if base in bases and not issubclass(needle, base):
383 # Conflict resolution: put classes present in __mro__ and their
384 # subclasses first. See test_mro_conflicts() in test_functools.py
385 # for examples.
386 index += 1
387 mro.insert(index, needle)
388 return mro
389
390def _find_impl(cls, registry):
391 """Returns the best matching implementation for the given class `cls` in
392 `registry`. Where there is no registered implementation for a specific
393 type, its method resolution order is used to find a more generic
394 implementation.
395
396 Note: if `registry` does not contain an implementation for the base
397 `object` type, this function may return None.
398
399 """
400 mro = _compose_mro(cls, registry.keys())
401 match = None
402 for t in mro:
403 if match is not None:
404 # If `match` is an ABC but there is another unrelated, equally
405 # matching ABC. Refuse the temptation to guess.
406 if (t in registry and not issubclass(match, t)
407 and match not in cls.__mro__):
408 raise RuntimeError("Ambiguous dispatch: {} or {}".format(
409 match, t))
410 break
411 if t in registry:
412 match = t
413 return registry.get(match)
414
415def singledispatch(func):
416 """Single-dispatch generic function decorator.
417
418 Transforms a function into a generic function, which can have different
419 behaviours depending upon the type of its first argument. The decorated
420 function acts as the default implementation, and additional
421 implementations can be registered using the 'register()' attribute of
422 the generic function.
423
424 """
425 registry = {}
426 dispatch_cache = WeakKeyDictionary()
427 cache_token = None
428
429 def dispatch(typ):
430 """generic_func.dispatch(type) -> <function implementation>
431
432 Runs the dispatch algorithm to return the best available implementation
433 for the given `type` registered on `generic_func`.
434
435 """
436 nonlocal cache_token
437 if cache_token is not None:
438 current_token = get_cache_token()
439 if cache_token != current_token:
440 dispatch_cache.clear()
441 cache_token = current_token
442 try:
443 impl = dispatch_cache[typ]
444 except KeyError:
445 try:
446 impl = registry[typ]
447 except KeyError:
448 impl = _find_impl(typ, registry)
449 dispatch_cache[typ] = impl
450 return impl
451
452 def register(typ, func=None):
453 """generic_func.register(type, func) -> func
454
455 Registers a new implementation for the given `type` on a `generic_func`.
456
457 """
458 nonlocal cache_token
459 if func is None:
460 return lambda f: register(typ, f)
461 registry[typ] = func
462 if cache_token is None and hasattr(typ, '__abstractmethods__'):
463 cache_token = get_cache_token()
464 dispatch_cache.clear()
465 return func
466
467 def wrapper(*args, **kw):
468 return dispatch(args[0].__class__)(*args, **kw)
469
470 registry[object] = func
471 wrapper.register = register
472 wrapper.dispatch = dispatch
473 wrapper.registry = MappingProxyType(registry)
474 wrapper._clear_cache = dispatch_cache.clear
475 update_wrapper(wrapper, func)
476 return wrapper