[pypy-svn] r74914 - in pypy/trunk/pypy/module/cpyext: . include test

agaynor at codespeak.net agaynor at codespeak.net
Sun May 30 15:58:52 CEST 2010


Author: agaynor
Date: Sun May 30 15:58:51 2010
New Revision: 74914

Added:
   pypy/trunk/pypy/module/cpyext/test/comparisons.c
Modified:
   pypy/trunk/pypy/module/cpyext/include/object.h
   pypy/trunk/pypy/module/cpyext/slotdefs.py
   pypy/trunk/pypy/module/cpyext/test/test_typeobject.py
Log:
Implement richcmp_eq for cpyext.

Modified: pypy/trunk/pypy/module/cpyext/include/object.h
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/object.h	(original)
+++ pypy/trunk/pypy/module/cpyext/include/object.h	Sun May 30 15:58:51 2010
@@ -70,6 +70,9 @@
 #define Py_NotImplemented (&_Py_NotImplementedStruct)
 
 /* Rich comparison opcodes */
+/*
+    XXX: Also defined in slotdefs.py
+*/
 #define Py_LT 0
 #define Py_LE 1
 #define Py_EQ 2

Modified: pypy/trunk/pypy/module/cpyext/slotdefs.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/slotdefs.py	(original)
+++ pypy/trunk/pypy/module/cpyext/slotdefs.py	Sun May 30 15:58:51 2010
@@ -5,7 +5,7 @@
 from pypy.module.cpyext.typeobjectdefs import (
     unaryfunc, wrapperfunc, ternaryfunc, PyTypeObjectPtr, binaryfunc,
     getattrfunc, setattrofunc, lenfunc, ssizeargfunc, ssizessizeargfunc,
-    ssizeobjargproc, iternextfunc, initproc)
+    ssizeobjargproc, iternextfunc, initproc, richcmpfunc)
 from pypy.module.cpyext.pyobject import from_ref
 from pypy.module.cpyext.pyerrors import PyErr_Occurred
 from pypy.module.cpyext.state import State
@@ -15,6 +15,15 @@
 from pypy.tool.sourcetools import func_with_new_name
 
 
+# XXX: Also defined in object.h
+Py_LT = 0
+Py_LE = 1
+Py_EQ = 2
+Py_NE = 3
+Py_GT = 4
+Py_GE = 5
+
+
 def check_num_args(space, ob, n):
     from pypy.module.cpyext.tupleobject import PyTuple_CheckExact, \
             PyTuple_GET_SIZE
@@ -124,6 +133,14 @@
         raise OperationError(space.w_StopIteration, space.w_None)
     return w_res
 
+def richcmp_eq(space, w_self, w_args, func):
+    func_target = rffi.cast(richcmpfunc, func)
+    check_num_args(space, w_args, 1)
+    args_w = space.fixedview(w_args)
+    other_w = args_w[0]
+    return generic_cpy_call(space, func_target,
+        w_self, other_w, rffi.cast(rffi.INT_real, Py_EQ))
+
 @cpython_api([PyTypeObjectPtr, PyObject, PyObject], PyObject, external=False)
 def slot_tp_new(space, type, w_args, w_kwds):
     from pypy.module.cpyext.tupleobject import PyTuple_Check

Added: pypy/trunk/pypy/module/cpyext/test/comparisons.c
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/cpyext/test/comparisons.c	Sun May 30 15:58:51 2010
@@ -0,0 +1,75 @@
+#include "Python.h"
+
+typedef struct CmpObject {
+    PyObject_HEAD
+} CmpObject;
+
+
+static PyObject* cmp_richcmp(PyObject *self, PyObject *other, int opid) {
+    if (opid != Py_EQ || !PyInt_CheckExact(other)) {
+        Py_INCREF(Py_NotImplemented);
+        return Py_NotImplemented;
+    }
+    long val = PyLong_AsLong(other);
+    return PyBool_FromLong(val == 3);
+}
+
+
+PyTypeObject CmpType = {
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "comparisons.CmpType",                          /* tp_name */
+    sizeof(CmpObject),                              /* tp_basicsize */
+    0,                                              /* tp_itemsize */
+    0,                                              /* tp_dealloc */
+    0,                                              /* tp_print */
+    0,                                              /* tp_getattr */
+    0,                                              /* tp_setattr */
+    0,                                              /* tp_compare */
+    0,                                              /* tp_repr */
+    0,                                              /* tp_as_number */
+    0,                                              /* tp_as_sequence */
+    0,                                              /* tp_as_mapping */
+    0,                                              /* tp_hash */
+    0,                                              /* tp_call */
+    0,                                              /* tp_str */
+    0,                                              /* tp_getattro */
+    0,                                              /* tp_setattro */
+    0,                                              /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,         /* tp_flags */
+    0,                                              /* tp_doc */
+    (traverseproc)0,                                /* tp_traverse */
+    0,                                              /* tp_clear */
+    (richcmpfunc)cmp_richcmp,                       /* tp_richcompare */
+    0,                                              /* tp_weaklistoffset */
+    0,                                              /* tp_iter */
+    0,                                              /* tp_iternext */
+    0,                                              /* tp_methods */
+    0,                                              /* tp_members */
+    0,                                              /* tp_getset */
+    0,                                              /* tp_base */
+    0,                                              /* tp_dict */
+    0,                                              /* tp_descr_get */
+    0,                                              /* tp_descr_set */
+    0,                                              /* tp_dictoffset */
+    0,                                              /* tp_init */
+    0,                                              /* tp_alloc */
+    0,                                              /* tp_new */
+    0                                               /* tp_free */
+};
+
+
+void initcomparisons(void)
+{
+    PyObject *m, *d;
+
+    if (PyType_Ready(&CmpType) < 0)
+        return;
+    m = Py_InitModule("comparisons", NULL);
+    if (m == NULL)
+        return;
+    d = PyModule_GetDict(m);
+    if (d == NULL)
+        return;
+    if (PyDict_SetItemString(d, "CmpType", (PyObject *)&CmpType) < 0)
+        return;
+}

Modified: pypy/trunk/pypy/module/cpyext/test/test_typeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_typeobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_typeobject.py	Sun May 30 15:58:51 2010
@@ -176,6 +176,11 @@
     def test_init_error(self):
         module = self.import_module("foo")
         raises(ValueError, module.InitErrType)
+    
+    def test_cmps(self):
+        module = self.import_module("comparisons")
+        cmpr = module.CmpType()
+        assert cmpr == 3
 
 
 class TestTypes(BaseApiTest):



More information about the Pypy-commit mailing list