From 50595d1858ba50d7c2ef4cbfdd7e23ce4965da62 Mon Sep 17 00:00:00 2001 From: Graham Dumpleton Date: Thu, 15 Aug 2013 21:03:06 +0800 Subject: [PATCH] Implement proxying of hash function for C implementation of object proxy. --- src/_wrappers.c | 14 +++++++++++++- tests/test_object_proxy.py | 10 +++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/_wrappers.c b/src/_wrappers.c index a174737..6dcc34c 100644 --- a/src/_wrappers.c +++ b/src/_wrappers.c @@ -190,6 +190,18 @@ static void WraptObjectProxy_dealloc(WraptObjectProxyObject *self) /* ------------------------------------------------------------------------- */ +static long WraptObjectProxy_hash(WraptObjectProxyObject *self) +{ + if (!self->wrapped) { + PyErr_SetString(PyExc_ValueError, "wrapper has not been initialised"); + return -1; + } + + return PyObject_Hash(self->wrapped); +} + +/* ------------------------------------------------------------------------- */ + static PyObject *WraptObjectProxy_call( WraptObjectProxyObject *self, PyObject *args, PyObject *kwds) { @@ -509,7 +521,7 @@ PyTypeObject WraptObjectProxy_Type = { 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ - 0, /*tp_hash*/ + (hashfunc)WraptObjectProxy_hash, /*tp_hash*/ (ternaryfunc)WraptObjectProxy_call, /*tp_call*/ 0, /*tp_str*/ (getattrofunc)WraptObjectProxy_getattro, /*tp_getattro*/ diff --git a/tests/test_object_proxy.py b/tests/test_object_proxy.py index d4d3ced..ce3bea0 100644 --- a/tests/test_object_proxy.py +++ b/tests/test_object_proxy.py @@ -620,7 +620,6 @@ class TestIterObjectProxy(unittest.TestCase): class TestContextManagerObjectProxy(unittest.TestCase): def test_context_manager(self): - class Class(object): def __enter__(self): return self @@ -634,5 +633,14 @@ class TestContextManagerObjectProxy(unittest.TestCase): with wrapper: pass +class TestEqualityObjectProxy(unittest.TestCase): + + def test_object_hash(self): + def function1(*args, **kwargs): + return args, kwargs + function2 = wrapt.ObjectProxy(function1) + + self.assertEqual(hash(function2), hash(function1)) + if __name__ == '__main__': unittest.main()