Implement proxying of hash function for C implementation of object proxy.

This commit is contained in:
Graham Dumpleton
2013-08-15 21:03:06 +08:00
parent d3bdc70a50
commit 50595d1858
2 changed files with 22 additions and 2 deletions

View File

@@ -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*/

View File

@@ -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()