[pypy-commit] creflect default: equality on cdata's

arigo noreply at buildbot.pypy.org
Fri Dec 5 16:56:02 CET 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r167:6aee42c289d3
Date: 2014-12-05 16:56 +0100
http://bitbucket.org/cffi/creflect/changeset/6aee42c289d3/

Log:	equality on cdata's

diff --git a/zeffir/cdata.c b/zeffir/cdata.c
--- a/zeffir/cdata.c
+++ b/zeffir/cdata.c
@@ -1050,6 +1050,52 @@
     }
 }
 
+//1769
+static PyObject *cdata_richcompare(PyObject *v, PyObject *w, int op)
+{
+    int res;
+    PyObject *pyres;
+    char *v_cdata, *w_cdata;
+
+    assert(CData_Check(v));
+    if (!CData_Check(w)) {
+        pyres = Py_NotImplemented;
+        goto done;
+    }
+
+    if ((op != Py_EQ && op != Py_NE) &&
+        ((((CDataObject *)v)->c_type->ct_flags & CT_PRIMITIVE_ANY) ||
+         (((CDataObject *)w)->c_type->ct_flags & CT_PRIMITIVE_ANY)))
+        goto Error;
+
+    v_cdata = ((CDataObject *)v)->c_data;
+    w_cdata = ((CDataObject *)w)->c_data;
+
+    switch (op) {
+    case Py_EQ: res = (v_cdata == w_cdata); break;
+    case Py_NE: res = (v_cdata != w_cdata); break;
+    case Py_LT: res = (v_cdata <  w_cdata); break;
+    case Py_LE: res = (v_cdata <= w_cdata); break;
+    case Py_GT: res = (v_cdata >  w_cdata); break;
+    case Py_GE: res = (v_cdata >= w_cdata); break;
+    default: res = -1;
+    }
+    pyres = res ? Py_True : Py_False;
+ done:
+    Py_INCREF(pyres);
+    return pyres;
+
+ Error:
+    PyErr_SetString(PyExc_TypeError,
+                    "cannot do comparison on a primitive cdata");
+    return NULL;
+}
+
+static long cdata_hash(CDataObject *cd)
+{
+    return _Py_HashPointer(cd->c_data);
+}
+
 //1815
 static Py_ssize_t cdata_length(CDataObject *cd)
 {
@@ -1363,7 +1409,7 @@
     0,//&CData_as_number,                           /* tp_as_number */
     0,                                          /* tp_as_sequence */
     &CData_as_mapping,                          /* tp_as_mapping */
-    0,//(hashfunc)cdata_hash,                       /* tp_hash */
+    (hashfunc)cdata_hash,                       /* tp_hash */
     0,//(ternaryfunc)cdata_call,                    /* tp_call */
     0,                                          /* tp_str */
     (getattrofunc)cdata_getattro,               /* tp_getattro */
@@ -1373,7 +1419,7 @@
     0,                                          /* tp_doc */
     0,                                          /* tp_traverse */
     0,                                          /* tp_clear */
-    0,//cdata_richcompare,                          /* tp_richcompare */
+    cdata_richcompare,                          /* tp_richcompare */
     offsetof(CDataObject, c_weakreflist),       /* tp_weaklistoffset */
     0,//(getiterfunc)cdata_iter,                    /* tp_iter */
     0,                                          /* tp_iternext */
diff --git a/zeffir/test/test_global.py b/zeffir/test/test_global.py
--- a/zeffir/test/test_global.py
+++ b/zeffir/test/test_global.py
@@ -24,3 +24,9 @@
     q = ffi.new("short *", -555)
     lib.myglobptr = q
     assert lib.myglobptr[0] == -555
+
+def test_eq():
+    ffi, lib = support.compile_and_open('global')
+    assert lib.myglobptr is not lib.myglobptr
+    assert lib.myglobptr == lib.myglobptr
+    assert hash(lib.myglobptr) == hash(lib.myglobptr)


More information about the pypy-commit mailing list