[Python-Dev] [Python-checkins] cpython: Add reference implementation for PEP 443 (original) (raw)
Brett Cannon brett at python.org
Wed Jun 5 16:31:04 CEST 2013
- Previous message: [Python-Dev] PyPI upload error
- Next message: [Python-Dev] [Python-checkins] cpython: Add reference implementation for PEP 443
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Any chance you could move your definitions for "generic function" and "single dispatch" to the glossary and just link to them here?
On Wed, Jun 5, 2013 at 6:20 AM, lukasz.langa <python-checkins at python.org>wrote:
http://hg.python.org/cpython/rev/dfcb64f51f7b changeset: 84039:dfcb64f51f7b user: Łukasz Langa <lukasz at langa.pl> date: Wed Jun 05 12:20:24 2013 +0200 summary: Add reference implementation for PEP 443
PEP accepted: http://mail.python.org/pipermail/python-dev/2013-June/126734.html files: Doc/library/functools.rst | 110 +++++++ Lib/functools.py | 128 ++++++++- Lib/pkgutil.py | 52 +--- Lib/test/testfunctools.py | 374 ++++++++++++++++++++++++- Misc/NEWS | 3 + Modules/Setup.dist | 2 +- 6 files changed, 614 insertions(+), 55 deletions(-)
diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -6,6 +6,7 @@ .. moduleauthor:: Peter Harris <scav at blueyonder.co.uk> .. moduleauthor:: Raymond Hettinger <python at rcn.com> .. moduleauthor:: Nick Coghlan <ncoghlan at gmail.com> +.. moduleauthor:: Łukasz Langa <lukasz at langa.pl> .. sectionauthor:: Peter Harris <scav at blueyonder.co.uk> Source code: :source:
Lib/functools.py
@@ -186,6 +187,115 @@ sequence contains only one item, the first item is returned. +.. decorator:: singledispatch(default) + + Transforms a function into a single-dispatch generic function. A **generic + function** is composed of multiple functions implementing the same operation + for different types. Which implementation should be used during a call is + determined by the dispatch algorithm. When the implementation is chosen + based on the type of a single argument, this is known as **single + dispatch**. + + To define a generic function, decorate it with the@singledispatch
+ decorator. Note that the dispatch happens on the type of the first argument, + create your function accordingly:: + + >>> from functools import singledispatch + >>> @singledispatch + ... def fun(arg, verbose=False): + ... if verbose: + ... print("Let me just say,", end=" ") + ... print(arg) + + To add overloaded implementations to the function, use the :func:register
+ attribute of the generic function. It is a decorator, taking a type + parameter and decorating a function implementing the operation for that + type:: + + >>> @fun.register(int) + ... def (arg, verbose=False): + ... if verbose: + ... print("Strength in numbers, eh?", end=" ") + ... print(arg) + ... + >>> @fun.register(list) + ... def (arg, verbose=False): + ... if verbose: + ... print("Enumerate this:") + ... for i, elem in enumerate(arg): + ... print(i, elem) + + To enable registering lambdas and pre-existing functions, the + :func:register
attribute can be used in a functional form:: + + >>> def nothing(arg, verbose=False): + ... print("Nothing.") + ... + >>> fun.register(type(None), nothing) + + The :func:register
attribute returns the undecorated function which + enables decorator stacking, pickling, as well as creating unit tests for + each variant independently:: + + >>> @fun.register(float) + ... @fun.register(Decimal) + ... def funnum(arg, verbose=False): + ... if verbose: + ... print("Half of your number:", end=" ") + ... print(arg / 2) + ... + >>> funnum is fun + False + + When called, the generic function dispatches on the type of the first + argument:: + + >>> fun("Hello, world.") + Hello, world. + >>> fun("test.", verbose=True) + Let me just say, test. + >>> fun(42, verbose=True) + Strength in numbers, eh? 42 + >>> fun(['spam', 'spam', 'eggs', 'spam'], verbose=True) + Enumerate this: + 0 spam + 1 spam + 2 eggs + 3 spam + >>> fun(None) + Nothing. + >>> fun(1.23) + 0.615 + + Where there is no registered implementation for a specific type, its + method resolution order is used to find a more generic implementation. + The original function decorated with@singledispatch
is registered + for the baseobject
type, which means it is used if no better + implementation is found. + + To check which implementation will the generic function choose for + a given type, use thedispatch()
attribute:: + + >>> fun.dispatch(float) + <function funnum at 0x1035a2840> + >>> fun.dispatch(dict) # note: default implementation + <function fun at 0x103fe0000> + + To access all registered implementations, use the read-onlyregistry
+ attribute:: + + >>> fun.registry.keys() + dictkeys([<class 'NoneType'>, <class 'int'>, <class 'object'>, + <class 'decimal.Decimal'>, <class 'list'>, + <class 'float'>]) + >>> fun.registry[float] + <function funnum at 0x1035a2840> + >>> fun.registry[object] + <function fun at 0x103fe0000> + + .. versionadded:: 3.4 + + .. function:: updatewrapper(wrapper, wrapped, assigned=WRAPPERASSIGNMENTS, updated=WRAPPERUPDATES) Update a wrapper function to look like the wrapped function. The optional diff --git a/Lib/functools.py b/Lib/functools.py --- a/Lib/functools.py +++ b/Lib/functools.py @@ -3,19 +3,24 @@ # Python module wrapper for functools C module # to allow utilities written in Python to be added # to the functools module. -# Written by Nick Coghlan -# and Raymond Hettinger -# Copyright (C) 2006-2010 Python Software Foundation. +# Written by Nick Coghlan , +# Raymond Hettinger , +# and Łukasz Langa . +# Copyright (C) 2006-2013 Python Software Foundation. # See C source code for functools credits/copyright all = ['updatewrapper', 'wraps', 'WRAPPERASSIGNMENTS', 'WRAPPERUPDATES', - 'totalordering', 'cmptokey', 'lrucache', 'reduce', 'partial'] + 'totalordering', 'cmptokey', 'lrucache', 'reduce', 'partial', + 'singledispatch'] try: from functools import reduce except ImportError: pass +from abc import getcachetoken from collections import namedtuple +from types import MappingProxyType +from weakref import WeakKeyDictionary try: from thread import RLock except: @@ -354,3 +359,118 @@ return updatewrapper(wrapper, userfunction) return decoratingfunction + + +################################################################################ +### singledispatch() - single-dispatch generic function decorator +################################################################################ + +def composemro(cls, haystack): + """Calculates the MRO for a given classcls
, including relevant abstract + base classes fromhaystack
. + + """ + bases = set(cls.mro) + mro = list(cls.mro) + for needle in haystack: + if (needle in bases or not hasattr(needle, 'mro') + or not issubclass(cls, needle)): + continue # either present in the mro already or unrelated + for index, base in enumerate(mro): + if not issubclass(base, needle): + break + if base in bases and not issubclass(needle, base): + # Conflict resolution: put classes present in mro and their + # subclasses first. See testmroconflicts() in testfunctools.py + # for examples. + index += 1 + mro.insert(index, needle) + return mro + +def findimpl(cls, registry): + """Returns the best matching implementation for the given classcls
in +registry
. Where there is no registered implementation for a specific + type, its method resolution order is used to find a more generic + implementation. + + Note: ifregistry
does not contain an implementation for the base +object
type, this function may return None. + + """ + mro = composemro(cls, registry.keys()) + match = None + for t in mro: + if match is not None: + # Ifmatch
is an ABC but there is another unrelated, equally + # matching ABC. Refuse the temptation to guess. + if (t in registry and not issubclass(match, t) + and match not in cls.mro): + raise RuntimeError("Ambiguous dispatch: {} or {}".format( + match, t)) + break + if t in registry: + match = t + return registry.get(match) + +def singledispatch(func): + """Single-dispatch generic function decorator. + + Transforms a function into a generic function, which can have different + behaviours depending upon the type of its first argument. The decorated + function acts as the default implementation, and additional + implementations can be registered using the 'register()' attribute of + the generic function. + + """ + registry = {} + dispatchcache = WeakKeyDictionary() + cachetoken = None + + def dispatch(typ): + """genericfunc.dispatch(type) -> + + Runs the dispatch algorithm to return the best available implementation + for the giventype
registered ongenericfunc
. + + """ + nonlocal cachetoken + if cachetoken is not None: + currenttoken = getcachetoken() + if cachetoken != currenttoken: + dispatchcache.clear() + cachetoken = currenttoken + try: + impl = dispatchcache[typ] + except KeyError: + try: + impl = registry[typ] + except KeyError: + impl = findimpl(typ, registry) + dispatchcache[typ] = impl + return impl + + def register(typ, func=None): + """genericfunc.register(type, func) -> func + + Registers a new implementation for the giventype
on agenericfunc
. + + """ + nonlocal cachetoken + if func is None: + return lambda f: register(typ, f) + registry[typ] = func + if cachetoken is None and hasattr(typ, 'abstractmethods'): + cachetoken = getcachetoken() + dispatchcache.clear() + return func + + def wrapper(*args, **kw): + return dispatch(args[0].class)(*args, **kw) + + registry[object] = func + wrapper.register = register + wrapper.dispatch = dispatch + wrapper.registry = MappingProxyType(registry) + wrapper.clearcache = dispatchcache.clear + updatewrapper(wrapper, func) + return wrapper diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py --- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -1,12 +1,13 @@ """Utilities to support packages.""" +from functools import singledispatch as simplegeneric +import imp +import importlib import os +import os.path import sys -import importlib -import imp -import os.path +from types import ModuleType from warnings import warn -from types import ModuleType all = [ 'getimporter', 'iterimporters', 'getloader', 'findloader', @@ -27,46 +28,6 @@ return marshal.load(stream) -def simplegeneric(func): - """Make a trivial single-dispatch generic function""" - registry = {} - def wrapper(*args, **kw): - ob = args[0] - try: - cls = ob.class - except AttributeError: - cls = type(ob) - try: - mro = cls.mro - except AttributeError: - try: - class cls(cls, object): - pass - mro = cls.mro[1:] - except TypeError: - mro = object, # must be an ExtensionClass or some such :( - for t in mro: - if t in registry: - return registry[t](*args, **kw) - else: - return func(*args, **kw) - try: - wrapper.name = func.name - except (TypeError, AttributeError): - pass # Python 2.3 doesn't allow functions to be renamed - - def register(typ, func=None): - if func is None: - return lambda f: register(typ, f) - registry[typ] = func - return func - - wrapper.dict = func.dict - wrapper.doc = func.doc - wrapper.register = register - return wrapper - - def walkpackages(path=None, prefix='', onerror=None): """Yields (moduleloader, name, ispkg) for all modules recursively on path, or, if path is None, all accessible modules. @@ -148,13 +109,12 @@ yield i, name, ispkg -#@simplegeneric + at simplegeneric def iterimportermodules(importer, prefix=''): if not hasattr(importer, 'itermodules'): return [] return importer.itermodules(prefix) -iterimportermodules = simplegeneric(iterimportermodules) # Implement a file walker for the normal importlib path hook def iterfilefindermodules(importer, prefix=''): diff --git a/Lib/test/testfunctools.py b/Lib/test/testfunctools.py --- a/Lib/test/testfunctools.py +++ b/Lib/test/testfunctools.py @@ -1,24 +1,30 @@ import collections -import sys -import unittest -from test import support -from weakref import proxy +from itertools import permutations import pickle from random import choice +import sys +from test import support +import unittest +from weakref import proxy import functools pyfunctools = support.importfreshmodule('functools', blocked=['functools']) cfunctools = support.importfreshmodule('functools', fresh=['functools']) +decimal = support.importfreshmodule('decimal', fresh=['decimal']) + + def capture(*args, **kw): """capture all positional and keyword arguments""" return args, kw + def signature(part): """ return the signature of a partial object """ return (part.func, part.args, part.keywords, part.dict) + class TestPartial: def testbasicexamples(self): @@ -138,6 +144,7 @@ join = self.partial(''.join) self.assertEqual(join(data), '0123456789') + @unittest.skipUnless(cfunctools, 'requires the C functools module') class TestPartialC(TestPartial, unittest.TestCase): if cfunctools: @@ -194,18 +201,22 @@ "new style getargs format but argument is not a tuple", f.setstate, BadSequence()) + class TestPartialPy(TestPartial, unittest.TestCase): partial = staticmethod(pyfunctools.partial) + if cfunctools: class PartialSubclass(cfunctools.partial): pass + @unittest.skipUnless(cfunctools, 'requires the C functools module') class TestPartialCSubclass(TestPartialC): if cfunctools: partial = PartialSubclass + class TestUpdateWrapper(unittest.TestCase): def checkwrapper(self, wrapper, wrapped, @@ -312,6 +323,7 @@ self.assertTrue(wrapper.doc.startswith('max(')) self.assertEqual(wrapper.annotations, {}) + class TestWraps(TestUpdateWrapper): def defaultupdate(self): @@ -372,6 +384,7 @@ self.assertEqual(wrapper.attr, 'This is a different test') self.assertEqual(wrapper.dictattr, f.dictattr) + class TestReduce(unittest.TestCase): func = functools.reduce @@ -452,6 +465,7 @@ d = {"one": 1, "two": 2, "three": 3} self.assertEqual(self.func(add, d), "".join(d.keys())) + class TestCmpToKey: def testcmptokey(self): @@ -534,14 +548,17 @@ self.assertRaises(TypeError, hash, k) self.assertNotIsInstance(k, collections.Hashable) + @unittest.skipUnless(cfunctools, 'requires the C functools module') class TestCmpToKeyC(TestCmpToKey, unittest.TestCase): if cfunctools: cmptokey = cfunctools.cmptokey + class TestCmpToKeyPy(TestCmpToKey, unittest.TestCase): cmptokey = staticmethod(pyfunctools.cmptokey) + class TestTotalOrdering(unittest.TestCase): def testtotalorderinglt(self): @@ -642,6 +659,7 @@ with self.assertRaises(TypeError): TestTO(8) <= ()_ _+_ _class TestLRU(unittest.TestCase):_ _def testlru(self):_ _@@ -834,6 +852,353 @@_ _DoubleEq(2)) # Verify the correct_ _return value_ _+class TestSingleDispatch(unittest.TestCase):_ _+ def testsimpleoverloads(self):_ _+ @functools.singledispatch_ _+ def g(obj):_ _+ return "base"_ _+ def gint(i):_ _+ return "integer"_ _+ g.register(int, gint)_ _+ self.assertEqual(g("str"), "base")_ _+ self.assertEqual(g(1), "integer")_ _+ self.assertEqual(g([1,2,3]), "base")_ _+_ _+ def testmro(self):_ _+ @functools.singledispatch_ _+ def g(obj):_ _+ return "base"_ _+ class C:_ _+ pass_ _+ class D(C):_ _+ pass_ _+ def gC(c):_ _+ return "C"_ _+ g.register(C, gC)_ _+ self.assertEqual(g(C()), "C")_ _+ self.assertEqual(g(D()), "C")_ _+_ _+ def testclassicclasses(self):_ _+ @functools.singledispatch_ _+ def g(obj):_ _+ return "base"_ _+ class C:_ _+ pass_ _+ class D(C):_ _+ pass_ _+ def gC(c):_ _+ return "C"_ _+ g.register(C, gC)_ _+ self.assertEqual(g(C()), "C")_ _+ self.assertEqual(g(D()), "C")_ _+_ _+ def testregisterdecorator(self):_ _+ @functools.singledispatch_ _+ def g(obj):_ _+ return "base"_ _+ @g.register(int)_ _+ def gint(i):_ _+ return "int %s" % (i,)_ _+ self.assertEqual(g(""), "base")_ _+ self.assertEqual(g(12), "int 12")_ _+ self.assertIs(g.dispatch(int), gint)_ _+ self.assertIs(g.dispatch(object), g.dispatch(str))_ _+ # Note: in the assert above this is not g._ _+ # @singledispatch returns the wrapper._ _+_ _+ def testwrappingattributes(self):_ _+ @functools.singledispatch_ _+ def g(obj):_ _+ "Simple test"_ _+ return "Test"_ _+ self.assertEqual(g._name_, "g")_ _+ self.assertEqual(g._doc_, "Simple test")_ _+_ _+ @unittest.skipUnless(decimal, 'requires decimal')_ _+ @support.cpythononly_ _+ def testcclasses(self):_ _+ @functools.singledispatch_ _+ def g(obj):_ _+ return "base"_ _+ @g.register(decimal.DecimalException)_ _+ def (obj):_ _+ return obj.args_ _+ subn = decimal.Subnormal("Exponent < Emin")_ _+ rnd = decimal.Rounded("Number got rounded")_ _+ self.assertEqual(g(subn), ("Exponent < Emin",))_ _+ self.assertEqual(g(rnd), ("Number got rounded",))_ _+ @g.register(decimal.Subnormal)_ _+ def (obj):_ _+ return "Too small to care."_ _+ self.assertEqual(g(subn), "Too small to care.")_ _+ self.assertEqual(g(rnd), ("Number got rounded",))_ _+_ _+ def testcomposemro(self):_ _+ c = collections_ _+ mro = functools.composemro_ _+ bases = [c.Sequence, c.MutableMapping, c.Mapping, c.Set]_ _+ for haystack in permutations(bases):_ _+ m = mro(dict, haystack)_ _+ self.assertEqual(m, [dict, c.MutableMapping, c.Mapping,_ _object])_ _+ bases = [c.Container, c.Mapping, c.MutableMapping, c.OrderedDict]_ _+ for haystack in permutations(bases):_ _+ m = mro(c.ChainMap, haystack)_ _+ self.assertEqual(m, [c.ChainMap, c.MutableMapping, c.Mapping,_ _+ c.Sized, c.Iterable, c.Container,_ _object])_ _+ # Note: The MRO order below depends on haystack ordering._ _+ m = mro(c.defaultdict, [c.Sized, c.Container, str])_ _+ self.assertEqual(m, [c.defaultdict, dict, c.Container, c.Sized,_ _object])_ _+ m = mro(c.defaultdict, [c.Container, c.Sized, str])_ _+ self.assertEqual(m, [c.defaultdict, dict, c.Sized, c.Container,_ _object])_ _+_ _+ def testregisterabc(self):_ _+ c = collections_ _+ d = {"a": "b"}_ _+ l = [1, 2, 3]_ _+ s = {object(), None}_ _+ f = frozenset(s)_ _+ t = (1, 2, 3)_ _+ @functools.singledispatch_ _+ def g(obj):_ _+ return "base"_ _+ self.assertEqual(g(d), "base")_ _+ self.assertEqual(g(l), "base")_ _+ self.assertEqual(g(s), "base")_ _+ self.assertEqual(g(f), "base")_ _+ self.assertEqual(g(t), "base")_ _+ g.register(c.Sized, lambda obj: "sized")_ _+ self.assertEqual(g(d), "sized")_ _+ self.assertEqual(g(l), "sized")_ _+ self.assertEqual(g(s), "sized")_ _+ self.assertEqual(g(f), "sized")_ _+ self.assertEqual(g(t), "sized")_ _+ g.register(c.MutableMapping, lambda obj: "mutablemapping")_ _+ self.assertEqual(g(d), "mutablemapping")_ _+ self.assertEqual(g(l), "sized")_ _+ self.assertEqual(g(s), "sized")_ _+ self.assertEqual(g(f), "sized")_ _+ self.assertEqual(g(t), "sized")_ _+ g.register(c.ChainMap, lambda obj: "chainmap")_ _+ self.assertEqual(g(d), "mutablemapping") # irrelevant ABCs_ _registered_ _+ self.assertEqual(g(l), "sized")_ _+ self.assertEqual(g(s), "sized")_ _+ self.assertEqual(g(f), "sized")_ _+ self.assertEqual(g(t), "sized")_ _+ g.register(c.MutableSequence, lambda obj: "mutablesequence")_ _+ self.assertEqual(g(d), "mutablemapping")_ _+ self.assertEqual(g(l), "mutablesequence")_ _+ self.assertEqual(g(s), "sized")_ _+ self.assertEqual(g(f), "sized")_ _+ self.assertEqual(g(t), "sized")_ _+ g.register(c.MutableSet, lambda obj: "mutableset")_ _+ self.assertEqual(g(d), "mutablemapping")_ _+ self.assertEqual(g(l), "mutablesequence")_ _+ self.assertEqual(g(s), "mutableset")_ _+ self.assertEqual(g(f), "sized")_ _+ self.assertEqual(g(t), "sized")_ _+ g.register(c.Mapping, lambda obj: "mapping")_ _+ self.assertEqual(g(d), "mutablemapping") # not specific enough_ _+ self.assertEqual(g(l), "mutablesequence")_ _+ self.assertEqual(g(s), "mutableset")_ _+ self.assertEqual(g(f), "sized")_ _+ self.assertEqual(g(t), "sized")_ _+ g.register(c.Sequence, lambda obj: "sequence")_ _+ self.assertEqual(g(d), "mutablemapping")_ _+ self.assertEqual(g(l), "mutablesequence")_ _+ self.assertEqual(g(s), "mutableset")_ _+ self.assertEqual(g(f), "sized")_ _+ self.assertEqual(g(t), "sequence")_ _+ g.register(c.Set, lambda obj: "set")_ _+ self.assertEqual(g(d), "mutablemapping")_ _+ self.assertEqual(g(l), "mutablesequence")_ _+ self.assertEqual(g(s), "mutableset")_ _+ self.assertEqual(g(f), "set")_ _+ self.assertEqual(g(t), "sequence")_ _+ g.register(dict, lambda obj: "dict")_ _+ self.assertEqual(g(d), "dict")_ _+ self.assertEqual(g(l), "mutablesequence")_ _+ self.assertEqual(g(s), "mutableset")_ _+ self.assertEqual(g(f), "set")_ _+ self.assertEqual(g(t), "sequence")_ _+ g.register(list, lambda obj: "list")_ _+ self.assertEqual(g(d), "dict")_ _+ self.assertEqual(g(l), "list")_ _+ self.assertEqual(g(s), "mutableset")_ _+ self.assertEqual(g(f), "set")_ _+ self.assertEqual(g(t), "sequence")_ _+ g.register(set, lambda obj: "concrete-set")_ _+ self.assertEqual(g(d), "dict")_ _+ self.assertEqual(g(l), "list")_ _+ self.assertEqual(g(s), "concrete-set")_ _+ self.assertEqual(g(f), "set")_ _+ self.assertEqual(g(t), "sequence")_ _+ g.register(frozenset, lambda obj: "frozen-set")_ _+ self.assertEqual(g(d), "dict")_ _+ self.assertEqual(g(l), "list")_ _+ self.assertEqual(g(s), "concrete-set")_ _+ self.assertEqual(g(f), "frozen-set")_ _+ self.assertEqual(g(t), "sequence")_ _+ g.register(tuple, lambda obj: "tuple")_ _+ self.assertEqual(g(d), "dict")_ _+ self.assertEqual(g(l), "list")_ _+ self.assertEqual(g(s), "concrete-set")_ _+ self.assertEqual(g(f), "frozen-set")_ _+ self.assertEqual(g(t), "tuple")_ _+_ _+ def testmroconflicts(self):_ _+ c = collections_ _+_ _+ @functools.singledispatch_ _+ def g(arg):_ _+ return "base"_ _+_ _+ class O(c.Sized):_ _+ def _len_(self):_ _+ return 0_ _+_ _+ o = O()_ _+ self.assertEqual(g(o), "base")_ _+ g.register(c.Iterable, lambda arg: "iterable")_ _+ g.register(c.Container, lambda arg: "container")_ _+ g.register(c.Sized, lambda arg: "sized")_ _+ g.register(c.Set, lambda arg: "set")_ _+ self.assertEqual(g(o), "sized")_ _+ c.Iterable.register(O)_ _+ self.assertEqual(g(o), "sized") # because it's explicitly in_ __mro__ _+ c.Container.register(O)_ _+ self.assertEqual(g(o), "sized") # see above: Sized is in _mro__ _+_ _+ class P:_ _+ pass_ _+_ _+ p = P()_ _+ self.assertEqual(g(p), "base")_ _+ c.Iterable.register(P)_ _+ self.assertEqual(g(p), "iterable")_ _+ c.Container.register(P)_ _+ with self.assertRaises(RuntimeError) as re:_ _+ g(p)_ _+ self.assertEqual(_ _+ str(re),_ _+ ("Ambiguous dispatch: <class 'collections.abc.Container'> " + "or <class 'collections.abc.Iterable'>"), + ) + + class Q(c.Sized): + def len(self): + return 0 + + q = Q() + self.assertEqual(g(q), "sized") + c.Iterable.register(Q) + self.assertEqual(g(q), "sized") # because it's explicitly in mro + c.Set.register(Q) + self.assertEqual(g(q), "set") # because c.Set is a subclass of + # c.Sized which is explicitly in + # mro + + def testcacheinvalidation(self): + from collections import UserDict + class TracingDict(UserDict): + def init(self, *args, **kwargs): + super(TracingDict, self).init(*args, **kwargs) + self.setops = [] + self.getops = [] + def getitem(self, key): + result = self.data[key] + self.getops.append(key) + return result + def setitem(self, key, value): + self.setops.append(key) + self.data[key] = value + def clear(self): + self.data.clear() + origwkd = functools.WeakKeyDictionary + td = TracingDict() + functools.WeakKeyDictionary = lambda: td + c = collections + @functools.singledispatch + def g(arg): + return "base" + d = {} + l = [] + self.assertEqual(len(td), 0) + self.assertEqual(g(d), "base") + self.assertEqual(len(td), 1) + self.assertEqual(td.getops, []) + self.assertEqual(td.setops, [dict]) + self.assertEqual(td.data[dict], g.registry[object]) + self.assertEqual(g(l), "base") + self.assertEqual(len(td), 2) + self.assertEqual(td.getops, []) + self.assertEqual(td.setops, [dict, list]) + self.assertEqual(td.data[dict], g.registry[object]) + self.assertEqual(td.data[list], g.registry[object]) + self.assertEqual(td.data[dict], td.data[list]) + self.assertEqual(g(l), "base") + self.assertEqual(g(d), "base") + self.assertEqual(td.getops, [list, dict]) + self.assertEqual(td.setops, [dict, list]) + g.register(list, lambda arg: "list") + self.assertEqual(td.getops, [list, dict]) + self.assertEqual(len(td), 0) + self.assertEqual(g(d), "base") + self.assertEqual(len(td), 1) + self.assertEqual(td.getops, [list, dict]) + self.assertEqual(td.setops, [dict, list, dict]) + self.assertEqual(td.data[dict], + functools.findimpl(dict, g.registry)) + self.assertEqual(g(l), "list") + self.assertEqual(len(td), 2) + self.assertEqual(td.getops, [list, dict]) + self.assertEqual(td.setops, [dict, list, dict, list]) + self.assertEqual(td.data[list], + functools.findimpl(list, g.registry)) + class X: + pass + c.MutableMapping.register(X) # Will not invalidate the cache, + # not using ABCs yet. + self.assertEqual(g(d), "base") + self.assertEqual(g(l), "list") + self.assertEqual(td.getops, [list, dict, dict, list]) + self.assertEqual(td.setops, [dict, list, dict, list]) + g.register(c.Sized, lambda arg: "sized") + self.assertEqual(len(td), 0) + self.assertEqual(g(d), "sized") + self.assertEqual(len(td), 1) + self.assertEqual(td.getops, [list, dict, dict, list]) + self.assertEqual(td.setops, [dict, list, dict, list, dict]) + self.assertEqual(g(l), "list") + self.assertEqual(len(td), 2) + self.assertEqual(td.getops, [list, dict, dict, list]) + self.assertEqual(td.setops, [dict, list, dict, list, dict, list]) + self.assertEqual(g(l), "list") + self.assertEqual(g(d), "sized") + self.assertEqual(td.getops, [list, dict, dict, list, list, dict]) + self.assertEqual(td.setops, [dict, list, dict, list, dict, list]) + g.dispatch(list) + g.dispatch(dict) + self.assertEqual(td.getops, [list, dict, dict, list, list, dict, + list, dict]) + self.assertEqual(td.setops, [dict, list, dict, list, dict, list]) + c.MutableSet.register(X) # Will invalidate the cache. + self.assertEqual(len(td), 2) # Stale cache. + self.assertEqual(g(l), "list") + self.assertEqual(len(td), 1) + g.register(c.MutableMapping, lambda arg: "mutablemapping") + self.assertEqual(len(td), 0) + self.assertEqual(g(d), "mutablemapping") + self.assertEqual(len(td), 1) + self.assertEqual(g(l), "list") + self.assertEqual(len(td), 2) + g.register(dict, lambda arg: "dict") + self.assertEqual(g(d), "dict") + self.assertEqual(g(l), "list") + g.clearcache() + self.assertEqual(len(td), 0) + functools.WeakKeyDictionary = origwkd + + def testmain(verbose=None): testclasses = ( TestPartialC, @@ -846,6 +1211,7 @@ TestWraps, TestReduce, TestLRU, + TestSingleDispatch, ) support.rununittest(*testclasses) diff --git a/Misc/NEWS b/Misc/NEWS --- a/Misc/NEWS +++ b/Misc/NEWS @@ -344,6 +344,9 @@ the default for linking if LDSHARED is not also overriden. This restores Distutils behavior introduced in 3.2.3 and inadvertently dropped in 3.3.0. +- Implement PEP 443 "Single-dispatch generic functions". + + Tests ----- diff --git a/Modules/Setup.dist b/Modules/Setup.dist --- a/Modules/Setup.dist +++ b/Modules/Setup.dist @@ -116,6 +116,7 @@ operator operator.c # operator.add() and similar goodies collections collectionsmodule.c # Container types itertools itertoolsmodule.c # Functions creating iterators for efficient looping +atexit atexitmodule.c # Register functions to be run at interpreter-shutdown # access to ISO C locale support locale localemodule.c # -lintl @@ -170,7 +171,6 @@ #weakref weakref.c # basic weak reference support #testcapi testcapimodule.c # Python C API test module #random randommodule.c # Random number generator -#atexit atexitmodule.c # Register functions to be run at interpreter-shutdown #elementtree -I$(srcdir)/Modules/expat -DHAVEEXPATCONFIGH -DUSEPYEXPATCAPI elementtree.c # elementtree accelerator #pickle pickle.c # pickle accelerator #datetime datetimemodule.c # datetime accelerator -- Repository URL: http://hg.python.org/cpython
Python-checkins mailing list Python-checkins at python.org http://mail.python.org/mailman/listinfo/python-checkins -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20130605/a0c2b1ac/attachment-0001.html>
- Previous message: [Python-Dev] PyPI upload error
- Next message: [Python-Dev] [Python-checkins] cpython: Add reference implementation for PEP 443
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]