]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/python/network.c
python: Do not use any GNU-style initialisers for structs
[people/ms/libloc.git] / src / python / network.c
index ae7b2985e95044a02e98348c239ddd195764b7a1..c74baaed5d9e3c213be5104e4c5b12746fc96d82 100644 (file)
 #include "locationmodule.h"
 #include "network.h"
 
+PyObject* new_network(PyTypeObject* type, struct loc_network* network) {
+       NetworkObject* self = (NetworkObject*)type->tp_alloc(type, 0);
+       if (self) {
+               self->network = loc_network_ref(network);
+       }
+
+       return (PyObject*)self;
+}
+
 static PyObject* Network_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
        NetworkObject* self = (NetworkObject*)type->tp_alloc(type, 0);
 
@@ -62,6 +71,15 @@ static PyObject* Network_repr(NetworkObject* self) {
        return obj;
 }
 
+static PyObject* Network_str(NetworkObject* self) {
+       char* network = loc_network_str(self->network);
+
+       PyObject* obj = PyUnicode_FromString(network);
+       free(network);
+
+       return obj;
+}
+
 static PyObject* Network_get_country_code(NetworkObject* self) {
        const char* country_code = loc_network_get_country_code(self->network);
 
@@ -74,7 +92,8 @@ static int Network_set_country_code(NetworkObject* self, PyObject* value) {
        int r = loc_network_set_country_code(self->network, country_code);
        if (r) {
                if (r == -EINVAL)
-                       PyErr_SetString(PyExc_ValueError, "Invalid country code");
+                       PyErr_Format(PyExc_ValueError,
+                               "Invalid country code: %s", country_code);
 
                return -1;
        }
@@ -127,13 +146,14 @@ static struct PyGetSetDef Network_getsetters[] = {
 
 PyTypeObject NetworkType = {
        PyVarObject_HEAD_INIT(NULL, 0)
-       tp_name:                "location.Network",
-       tp_basicsize:           sizeof(NetworkObject),
-       tp_flags:               Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
-       tp_new:                 Network_new,
-       tp_dealloc:             (destructor)Network_dealloc,
-       tp_init:                (initproc)Network_init,
-       tp_doc:                 "Network object",
-       tp_getset:              Network_getsetters,
-       tp_repr:                (reprfunc)Network_repr,
+       .tp_name =               "location.Network",
+       .tp_basicsize =          sizeof(NetworkObject),
+       .tp_flags =              Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+       .tp_new =                Network_new,
+       .tp_dealloc =            (destructor)Network_dealloc,
+       .tp_init =               (initproc)Network_init,
+       .tp_doc =                "Network object",
+       .tp_getset =             Network_getsetters,
+       .tp_repr =               (reprfunc)Network_repr,
+       .tp_str =                (reprfunc)Network_str,
 };