#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);
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);
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;
}
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,
};