Author: Antonio Cuni <[email protected]>
Branch: py3k
Changeset: r53681:f370393a8403
Date: 2012-03-15 14:23 +0100
http://bitbucket.org/pypy/pypy/changeset/f370393a8403/

Log:    fix test_descriptor.test_hash: objects which define __eq__ are
        unhashable by default. The code which does the same in CPython is
        completely different (because it relies on slots), I hope that this
        is equivalent in all the corner cases

diff --git a/pypy/objspace/std/typeobject.py b/pypy/objspace/std/typeobject.py
--- a/pypy/objspace/std/typeobject.py
+++ b/pypy/objspace/std/typeobject.py
@@ -766,6 +766,7 @@
     w_self.dict_w.setdefault('__doc__', w_self.w_doc)
     if w_self.is_heaptype():
         ensure_module_attr(w_self)
+    ensure_hash(w_self)
     w_self.mro_w = []      # temporarily
     compute_mro(w_self)
 
@@ -788,6 +789,12 @@
             if w_name is not None:
                 w_self.dict_w['__module__'] = w_name
 
+def ensure_hash(w_self):
+    # if we define __eq__ but not __hash__, we force __hash__ to be None to
+    # prevent inheriting it
+    if '__eq__' in w_self.dict_w and '__hash__' not in w_self.dict_w:
+        w_self.dict_w['__hash__'] = w_self.space.w_None
+
 def compute_mro(w_self):
     if w_self.is_heaptype():
         space = w_self.space
diff --git a/pypy/objspace/test/test_descriptor.py 
b/pypy/objspace/test/test_descriptor.py
--- a/pypy/objspace/test/test_descriptor.py
+++ b/pypy/objspace/test/test_descriptor.py
@@ -101,15 +101,12 @@
 
     def test_hash(self): 
         class A(object):
-            pass 
+            pass
         hash(A()) 
 
-        # as in CPython, for new-style classes we don't check if
-        # __eq__ is overridden without __hash__ being overridden,
-        # and so hash(B()) always just works (but gives a slightly
-        # useless result).
         class B(object):
-            def __eq__(self, other): pass 
+            def __eq__(self, other): pass
+        assert B.__hash__ is None
         raises(TypeError, "hash(B())") # because we define __eq__ but not 
__hash__
 
         class E(object):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to