[pypy-commit] pypy cpyext-hash_notimpl: failing test, start to implement check for PyObject_HashNotImplemented (last Numpy test failure)

mattip pypy.commits at gmail.com
Wed Jul 5 09:31:39 EDT 2017


Author: Matti Picus <matti.picus at gmail.com>
Branch: cpyext-hash_notimpl
Changeset: r91708:d3c446f2be53
Date: 2017-07-05 09:30 -0400
http://bitbucket.org/pypy/pypy/changeset/d3c446f2be53/

Log:	failing test, start to implement check for
	PyObject_HashNotImplemented (last Numpy test failure)

diff --git a/pypy/module/cpyext/test/test_typeobject.py b/pypy/module/cpyext/test/test_typeobject.py
--- a/pypy/module/cpyext/test/test_typeobject.py
+++ b/pypy/module/cpyext/test/test_typeobject.py
@@ -1347,3 +1347,30 @@
         Bsize = module.get_basicsize(B)
         assert Asize == Bsize
         assert Asize > basesize
+
+
+class AppTestHashable(AppTestCpythonExtensionBase):
+    def test_unhashable(self):
+        module = self.import_extension('foo', [
+           ("new_obj", "METH_NOARGS",
+            '''
+                PyObject *obj;
+                obj = PyObject_New(PyObject, &Foo_Type);
+                return obj;
+            '''
+            )], prologue='''
+            static PyTypeObject Foo_Type = {
+                PyVarObject_HEAD_INIT(NULL, 0)
+                "foo.foo",
+            };
+            ''', more_init = '''
+                Foo_Type.tp_flags = Py_TPFLAGS_DEFAULT;
+                Foo_Type.tp_hash = PyObject_HashNotImplemented;
+                if (PyType_Ready(&Foo_Type) < 0) INITERROR;
+            ''')
+        obj = module.new_obj()
+        raises(TypeError, hash, obj)
+        assert type(obj).__dict__['__hash__'] is None
+        # this is equivalent to
+        from collections import Hashable
+        assert not isinstance(obj, Hashable)
diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -338,13 +338,22 @@
                 setattr(struct, slot_names[1], slot_func_helper)
 
 def add_operators(space, dict_w, pto):
-    # XXX support PyObject_HashNotImplemented
+    from pypy.module.cpyext.object import PyObject_HashNotImplemented
+    hash_not_impl = PyObject_HashNotImplemented.api_func.get_llhelper(space)
     for method_name, slot_names, wrapper_func, wrapper_func_kwds, doc in slotdefs_for_wrappers:
         if method_name in dict_w:
             continue
         offset = [rffi.offsetof(lltype.typeOf(pto).TO, slot_names[0])]
         if len(slot_names) == 1:
             func = getattr(pto, slot_names[0])
+            if slot_names[0] == 'c_tp_hash':
+                # XXX if it is hash_not_impl, do not assign to dict_w
+                # name = rffi.charp2str(pto.c_tp_name)
+                # if 'foo' in name:
+                #    import pdb;pdb.set_trace()
+                if hash_not_impl == func:
+                    # XXX never reached
+                    continue
         else:
             assert len(slot_names) == 2
             struct = getattr(pto, slot_names[0])


More information about the pypy-commit mailing list