]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/python/as.c
python: Do not use any GNU-style initialisers for structs
[people/ms/libloc.git] / src / python / as.c
index a28727d6f36d1de1aed3f8249e28a0b5b845a33a..e3b716bc11fc71682ce4bf9980450e83f0b9f45d 100644 (file)
@@ -18,7 +18,6 @@
 
 #include <loc/libloc.h>
 #include <loc/as.h>
-#include <loc/stringpool.h>
 
 #include "locationmodule.h"
 #include "as.h"
@@ -33,17 +32,7 @@ PyObject* new_as(PyTypeObject* type, struct loc_as* as) {
 }
 
 static PyObject* AS_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
-       // Create stringpool
-       struct loc_stringpool* pool;
-       int r = loc_stringpool_new(loc_ctx, &pool);
-       if (r)
-               return NULL;
-
        ASObject* self = (ASObject*)type->tp_alloc(type, 0);
-       if (self) {
-               self->ctx = loc_ref(loc_ctx);
-               self->pool = pool;
-       }
 
        return (PyObject*)self;
 }
@@ -52,12 +41,6 @@ static void AS_dealloc(ASObject* self) {
        if (self->as)
                loc_as_unref(self->as);
 
-       if (self->pool)
-               loc_stringpool_unref(self->pool);
-
-       if (self->ctx)
-               loc_unref(self->ctx);
-
        Py_TYPE(self)->tp_free((PyObject* )self);
 }
 
@@ -68,13 +51,33 @@ static int AS_init(ASObject* self, PyObject* args, PyObject* kwargs) {
                return -1;
 
        // Create the AS object
-       int r = loc_as_new(self->ctx, self->pool, &self->as, number);
+       int r = loc_as_new(loc_ctx, &self->as, number);
        if (r)
                return -1;
 
        return 0;
 }
 
+static PyObject* AS_repr(ASObject* self) {
+       uint32_t number = loc_as_get_number(self->as);
+       const char* name = loc_as_get_name(self->as);
+
+       if (name)
+               return PyUnicode_FromFormat("<AS %d (%s)>", number, name);
+
+       return PyUnicode_FromFormat("<AS %d>", number);
+}
+
+static PyObject* AS_str(ASObject* self) {
+       uint32_t number = loc_as_get_number(self->as);
+       const char* name = loc_as_get_name(self->as);
+
+       if (name)
+               return PyUnicode_FromFormat("AS%d (%s)", number, name);
+
+       return PyUnicode_FromFormat("AS%d", number);
+}
+
 static PyObject* AS_get_number(ASObject* self) {
        uint32_t number = loc_as_get_number(self->as);
 
@@ -87,11 +90,46 @@ static PyObject* AS_get_name(ASObject* self) {
        return PyUnicode_FromString(name);
 }
 
+static int AS_set_name(ASObject* self, PyObject* value) {
+       const char* name = PyUnicode_AsUTF8(value);
+
+       int r = loc_as_set_name(self->as, name);
+       if (r) {
+               PyErr_Format(PyExc_ValueError, "Could not set name: %s", name);
+               return r;
+       }
+
+       return 0;
+}
+
+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",
                (getter)AS_get_name,
-               NULL,
+               (setter)AS_set_name,
                NULL,
                NULL,
        },
@@ -107,12 +145,15 @@ static struct PyGetSetDef AS_getsetters[] = {
 
 PyTypeObject ASType = {
        PyVarObject_HEAD_INIT(NULL, 0)
-       tp_name:                "location.AS",
-       tp_basicsize:           sizeof(ASObject),
-       tp_flags:               Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
-       tp_new:                 AS_new,
-       tp_dealloc:             (destructor)AS_dealloc,
-       tp_init:                (initproc)AS_init,
-       tp_doc:                 "AS object",
-       tp_getset:              AS_getsetters,
+       .tp_name =               "location.AS",
+       .tp_basicsize =          sizeof(ASObject),
+       .tp_flags =              Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+       .tp_new =                AS_new,
+       .tp_dealloc =            (destructor)AS_dealloc,
+       .tp_init =               (initproc)AS_init,
+       .tp_doc =                "AS object",
+       .tp_getset =             AS_getsetters,
+       .tp_repr =               (reprfunc)AS_repr,
+       .tp_str =                (reprfunc)AS_str,
+       .tp_richcompare =        (richcmpfunc)AS_richcompare,
 };