cpython: a03fb2fc3ed8 (original) (raw)

Mercurial > cpython

changeset 69150:a03fb2fc3ed8

Issue #11707: Fast C version of functools.cmp_to_key() [#11707]

Raymond Hettinger python@rcn.com
date Tue, 05 Apr 2011 02:33:54 -0700
parents d14eac872a46
children 76ed6a061ebe
files Lib/functools.py Lib/test/test_functools.py Misc/NEWS Modules/_functoolsmodule.c
diffstat 4 files changed, 235 insertions(+), 2 deletions(-)[+] [-] Lib/functools.py 7 Lib/test/test_functools.py 66 Misc/NEWS 3 Modules/_functoolsmodule.c 161

line wrap: on

line diff

--- a/Lib/functools.py +++ b/Lib/functools.py @@ -97,7 +97,7 @@ def cmp_to_key(mycmp): """Convert a cmp= function into a key= function""" class K(object): slots = ['obj']

@@ -115,6 +115,11 @@ def cmp_to_key(mycmp): raise TypeError('hash not implemented') return K +try:

+except ImportError:

+ _CacheInfo = namedtuple("CacheInfo", "hits misses maxsize currsize") def lru_cache(maxsize=100):

--- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -435,18 +435,81 @@ class TestReduce(unittest.TestCase): self.assertEqual(self.func(add, d), "".join(d.keys())) class TestCmpToKey(unittest.TestCase): + def test_cmp_to_key(self):

+

+

+

+

+

+ def test_hash(self): def mycmp(x, y): return y - x key = functools.cmp_to_key(mycmp) k = key(10)

class TestTotalOrdering(unittest.TestCase): @@ -655,6 +718,7 @@ class TestLRU(unittest.TestCase): def test_main(verbose=None): test_classes = (

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -97,6 +97,9 @@ Library

--- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -330,6 +330,165 @@ static PyTypeObject partial_type = { }; +/* cmp_to_key ***************************************************************/ + +typedef struct {

+} keyobject; + +static void +keyobject_dealloc(keyobject *ko) +{

+} + +static int +keyobject_traverse(keyobject *ko, visitproc visit, void *arg) +{

+} + +static PyMemberDef keyobject_members[] = {

+}; + +static PyObject * +keyobject_call(keyobject *ko, PyObject *args, PyObject *kw); + +static PyObject * +keyobject_richcompare(PyObject *ko, PyObject *other, int op); + +static PyTypeObject keyobject_type = {

+}; + +static PyObject * +keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds) +{

+

+} + +static PyObject * +keyobject_richcompare(PyObject *ko, PyObject *other, int op) +{

+

+

+

+} + +static PyObject * +functools_cmp_to_key(PyObject *self, PyObject *args, PyObject *kwds){

+

+} + +PyDoc_STRVAR(functools_cmp_to_key_doc, +"Convert a cmp= function into a key= function."); + /* reduce (used to be a builtin) ********************************************/ static PyObject * @@ -413,6 +572,8 @@ PyDoc_STRVAR(module_doc, static PyMethodDef module_methods[] = { {"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},