[pypy-svn] r49680 - in pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Wed Dec 12 18:21:59 CET 2007


Author: cfbolz
Date: Wed Dec 12 18:21:58 2007
New Revision: 49680

Modified:
   pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/interp_classobj.py
   pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/test/test_classobj.py
Log:
hash


Modified: pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/interp_classobj.py
==============================================================================
--- pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/interp_classobj.py	(original)
+++ pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/interp_classobj.py	Wed Dec 12 18:21:58 2007
@@ -462,6 +462,25 @@
                     return space.wrap(-1)
                 return space.wrap(0)
         return space.w_NotImplemented
+
+    def descr_hash(self, space):
+        w_func = self.getattr(space, space.wrap('__hash__'), False)
+        if w_func is None:
+            w_eq =  self.getattr(space, space.wrap('__eq__'), False)
+            w_cmp =  self.getattr(space, space.wrap('__cmp__'), False)
+            if w_eq is not None or w_cmp is not None:
+                raise OperationError(space.w_TypeError,
+                                     space.wrap("unhashable instance"))
+            else:
+                return space.wrap(hash(self))
+        w_ret = space.call_function(w_func)
+        if not space.is_true(space.isinstance(w_ret, space.w_int)):
+            raise OperationError(
+                space.w_TypeError,
+                space.wrap("__hash__ must return int"))
+        return w_ret
+
+
 rawdict = {}
 
 # unary operations
@@ -531,6 +550,8 @@
                              unwrap_spec=['self', ObjSpace]),
     __cmp__ = interp2app(W_InstanceObject.descr_cmp,
                          unwrap_spec=['self', ObjSpace, W_Root]),
+    __hash__ = interp2app(W_InstanceObject.descr_hash,
+                          unwrap_spec=['self', ObjSpace]),
     **rawdict
 )
 

Modified: pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/test/test_classobj.py
==============================================================================
--- pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/test/test_classobj.py	(original)
+++ pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/test/test_classobj.py	Wed Dec 12 18:21:58 2007
@@ -457,9 +457,11 @@
                 return (1, 2)
         assert cmp(A(), 1) == -1
         class A:
+            __metaclass__ = nclassobj
             def __cmp__(self, other):
                 return 1
         class B:
+            __metaclass__ = nclassobj
             pass
 
         a = A()
@@ -468,13 +470,44 @@
         assert cmp(b, a) == -1
 
         class A:
+            __metaclass__ = nclassobj
             def __cmp__(self, other):
                 return 1L
         a = A()
         assert cmp(a, b) == 1
 
         class A:
+            __metaclass__ = nclassobj
             def __cmp__(self, other):
                 return "hello?"
         a = A()
         raises(TypeError, cmp, a, b)
+
+    def test_hash(self):
+        class A:
+            __metaclass__ = nclassobj
+            pass
+        hash(A()) # does not crash
+        class A:
+            def __hash__(self):
+                return "hello?"
+        a = A()
+        raises(TypeError, hash, a)
+        class A:
+            __metaclass__ = nclassobj
+            def __hash__(self):
+                return 1
+        a = A()
+        assert hash(a) == 1
+        class A:
+            __metaclass__ = nclassobj
+            def __cmp__(self, other):
+                return 1
+        a = A()
+        raises(TypeError, hash, a)
+        class A:
+            __metaclass__ = nclassobj
+            def __eq__(self, other):
+                return 1
+        a = A()
+        raises(TypeError, hash, a)



More information about the Pypy-commit mailing list