python: Implement comparison of ASes
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 14:25:34 +0000 (14:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 14:25:34 +0000 (14:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libloc.sym
src/python/as.c

index 47e8f69..25fe5f5 100644 (file)
@@ -1,6 +1,7 @@
 LIBLOC_PRIVATE {
 global:
        # AS
+       loc_as_cmp;
        loc_as_get_name;
        loc_as_get_number;
        loc_as_new;
index 3c74f9e..ca0af47 100644 (file)
@@ -80,6 +80,29 @@ static PyObject* AS_get_name(ASObject* self) {
        return PyUnicode_FromString(name);
 }
 
+static PyObject* AS_richcompare(ASObject* self, ASObject* other, int op) {
+       int r = loc_as_cmp(self->as, other->as);
+
+       switch (op) {
+               case Py_EQ:
+                       if (r == 0)
+                               Py_RETURN_TRUE;
+
+                       Py_RETURN_FALSE;
+
+               case Py_LT:
+                       if (r < 0)
+                               Py_RETURN_TRUE;
+
+                       Py_RETURN_FALSE;
+
+               default:
+                       break;
+       }
+
+       Py_RETURN_NOTIMPLEMENTED;
+}
+
 static struct PyGetSetDef AS_getsetters[] = {
        {
                "name",
@@ -109,4 +132,5 @@ PyTypeObject ASType = {
        tp_doc:                 "AS object",
        tp_getset:              AS_getsetters,
        tp_repr:                (reprfunc)AS_repr,
+       tp_richcompare:         (richcmpfunc)AS_richcompare,
 };