[pypy-svn] r74932 - in pypy/trunk/pypy/module/cpyext: . test

agaynor at codespeak.net agaynor at codespeak.net
Sun May 30 20:51:06 CEST 2010


Author: agaynor
Date: Sun May 30 20:51:05 2010
New Revision: 74932

Modified:
   pypy/trunk/pypy/module/cpyext/slotdefs.py
   pypy/trunk/pypy/module/cpyext/test/comparisons.c
   pypy/trunk/pypy/module/cpyext/test/test_typeobject.py
Log:
Implemented hashfunc wrapper for cpyext

Modified: pypy/trunk/pypy/module/cpyext/slotdefs.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/slotdefs.py	(original)
+++ pypy/trunk/pypy/module/cpyext/slotdefs.py	Sun May 30 20:51:05 2010
@@ -5,7 +5,7 @@
 from pypy.module.cpyext.typeobjectdefs import (
     unaryfunc, wrapperfunc, ternaryfunc, PyTypeObjectPtr, binaryfunc,
     getattrfunc, setattrofunc, lenfunc, ssizeargfunc, ssizessizeargfunc,
-    ssizeobjargproc, iternextfunc, initproc, richcmpfunc)
+    ssizeobjargproc, iternextfunc, initproc, richcmpfunc, hashfunc)
 from pypy.module.cpyext.pyobject import from_ref
 from pypy.module.cpyext.pyerrors import PyErr_Occurred
 from pypy.module.cpyext.state import State
@@ -133,6 +133,11 @@
         raise OperationError(space.w_StopIteration, space.w_None)
     return w_res
 
+def wrap_hashfunc(space, w_self, w_args, func):
+    func_target = rffi.cast(hashfunc, func)
+    check_num_args(space, w_args, 0)
+    return space.wrap(generic_cpy_call(space, func_target, w_self))
+
 def get_richcmp_func(OP_CONST):
     def inner(space, w_self, w_args, func):
         func_target = rffi.cast(richcmpfunc, func)

Modified: pypy/trunk/pypy/module/cpyext/test/comparisons.c
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/comparisons.c	(original)
+++ pypy/trunk/pypy/module/cpyext/test/comparisons.c	Sun May 30 20:51:05 2010
@@ -19,6 +19,10 @@
     }
 }
 
+static long cmp_hashfunc(PyObject *self) {
+    return 3;
+}
+
 
 PyTypeObject CmpType = {
     PyVarObject_HEAD_INIT(NULL, 0)
@@ -34,7 +38,7 @@
     0,                                              /* tp_as_number */
     0,                                              /* tp_as_sequence */
     0,                                              /* tp_as_mapping */
-    0,                                              /* tp_hash */
+    cmp_hashfunc,                                   /* tp_hash */
     0,                                              /* tp_call */
     0,                                              /* tp_str */
     0,                                              /* tp_getattro */

Modified: pypy/trunk/pypy/module/cpyext/test/test_typeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_typeobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_typeobject.py	Sun May 30 20:51:05 2010
@@ -182,6 +182,15 @@
         cmpr = module.CmpType()
         assert cmpr == 3
         assert cmpr != 42
+    
+    def test_hash(self):
+        module = self.import_module("comparisons")
+        cmpr = module.CmpType()
+        assert hash(cmpr) == 3
+        d = {}
+        d[cmpr] = 72
+        assert d[cmpr] == 72
+        assert d[3] == 72
 
 
 class TestTypes(BaseApiTest):



More information about the Pypy-commit mailing list