Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r1252:0ac3dee78264
Date: 2013-05-16 17:02 +0200
http://bitbucket.org/cffi/cffi/changeset/0ac3dee78264/

Log:    Test and fix: two pointers of different types can compare equal, but
        they still had different hashes

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -1654,10 +1654,7 @@
 
 static long cdata_hash(CDataObject *cd)
 {
-    long h = _Py_HashPointer(cd->c_type) ^ _Py_HashPointer(cd->c_data);
-    if (h == -1)
-        h = -2;
-    return h;
+    return _Py_HashPointer(cd->c_data);
 }
 
 static Py_ssize_t
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -376,8 +376,9 @@
     BInt = new_primitive_type("int")
     BFloat = new_primitive_type("float")
     for i in range(1, 20):
-        if (hash(cast(BChar, chr(i))) !=
-            hash(cast(BInt, i))):
+        x1 = cast(BChar, chr(i))
+        x2 = cast(BInt, i)
+        if hash(x1) != hash(x2):
             break
     else:
         raise AssertionError("hashes are equal")
@@ -2734,6 +2735,14 @@
     assert x.__name__ == '<cdata>'
     assert hasattr(x, '__doc__')
 
+def test_different_types_of_ptr_equality():
+    BVoidP = new_pointer_type(new_void_type())
+    BIntP = new_pointer_type(new_primitive_type("int"))
+    x = cast(BVoidP, 12345)
+    assert x == cast(BIntP, 12345)
+    assert x != cast(BIntP, 12344)
+    assert hash(x) == hash(cast(BIntP, 12345))
+
 def test_version():
     # this test is here mostly for PyPy
     assert __version__ == "0.6"
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to