Commits

Ronald Oussoren committed 5d92157

Implement ordering for selectors

This adds very limited support for comparing selectors with
'<', '<=', '>' and '>=' to make it possible to sort a list of
selectors, which is needed to avoid crashing pydoc on a Cocoa class.

  • Participants
  • Parent commits 8830356

Comments (0)

Files changed (1)

pyobjc-core/Modules/objc/selector.m

     return rval;
 }
 
-static PyObject* objcsel_richcompare(PyObject* a, PyObject* b, int op)
+static PyObject*
+objcsel_richcompare(PyObject* a, PyObject* b, int op)
 {
     if (op == Py_EQ || op == Py_NE) {
         if (PyObjCNativeSelector_Check(a) && PyObjCNativeSelector_Check(b)) {
         }
 
     } else {
-        PyErr_SetString(PyExc_TypeError, "Cannot use '<', '<=', '>=' and '>' with objc.selector");
-        return NULL;
+        if (PyObjCSelector_Check(a) && PyObjCSelector_Check(b)) {
+            SEL sel_a = PyObjCSelector_GET_SELECTOR(a);
+            SEL sel_b = PyObjCSelector_GET_SELECTOR(b);
+
+            int r = strcmp(sel_getName(sel_a), sel_getName(sel_b));
+            switch (op) {
+            case Py_LT: return PyBool_FromLong(r < 0);
+            case Py_LE: return PyBool_FromLong(r <= 0);
+            case Py_GT: return PyBool_FromLong(r > 0);
+            case Py_GE: return PyBool_FromLong(r >= 0);
+            }
+        }
+
+        Py_INCREF(Py_NotImplemented);
+        return Py_NotImplemented;
     }
 }
 
             }
         }
     } else {
-        PyErr_SetString(PyExc_TypeError, "Cannot use '<', '<=', '>=' and '>' with objc.selector");
-        return NULL;
+        if (PyObjCSelector_Check(a) && PyObjCSelector_Check(b)) {
+            SEL sel_a = PyObjCSelector_GET_SELECTOR(a);
+            SEL sel_b = PyObjCSelector_GET_SELECTOR(b);
+
+            int r = strcmp(sel_getName(sel_a), sel_getName(sel_b));
+            switch (op) {
+            case Py_LT: return PyBool_FromLong(r < 0);
+            case Py_LE: return PyBool_FromLong(r <= 0);
+            case Py_GT: return PyBool_FromLong(r > 0);
+            case Py_GE: return PyBool_FromLong(r >= 0);
+            }
+        }
+        Py_INCREF(Py_NotImplemented);
+        return Py_NotImplemented;
     }
 }