From e3036b284d41977f240592ebb4148e4283176682 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Tue, 17 Dec 2013 21:59:17 +0100 Subject: Kick out struct etherinfo_obj_data This simplifies and clarifies the object/struct relations a bit better. Signed-off-by: David Sommerseth --- python-ethtool/etherinfo.c | 4 +-- python-ethtool/etherinfo_obj.c | 56 ++++++++++++++++++--------------------- python-ethtool/etherinfo_struct.h | 13 ++------- python-ethtool/ethtool.c | 22 +++++---------- 4 files changed, 37 insertions(+), 58 deletions(-) diff --git a/python-ethtool/etherinfo.c b/python-ethtool/etherinfo.c index 509ae08..438fc27 100644 --- a/python-ethtool/etherinfo.c +++ b/python-ethtool/etherinfo.c @@ -153,10 +153,10 @@ int get_etherinfo(etherinfo_py *self, nlQuery query) int ret = 0; - if( !self || !self->data || !self->data->ethinfo ) { + if( !self || !self->ethinfo ) { return 0; } - ethinf = self->data->ethinfo; + ethinf = self->ethinfo; /* Open a NETLINK connection on-the-fly */ if( !open_netlink(self) ) { diff --git a/python-ethtool/etherinfo_obj.c b/python-ethtool/etherinfo_obj.c index 97a06be..26a63f2 100644 --- a/python-ethtool/etherinfo_obj.c +++ b/python-ethtool/etherinfo_obj.c @@ -40,13 +40,9 @@ extern PyTypeObject ethtool_etherinfoIPv6Type; */ void _ethtool_etherinfo_dealloc(etherinfo_py *self) { - if( self->data ) { - close_netlink(self); - - if( self->data->ethinfo ) { - free_etherinfo(self->data->ethinfo); - } - free(self->data); + close_netlink(self); + if( self->ethinfo ) { + free_etherinfo(self->ethinfo); } self->ob_type->tp_free((PyObject*)self); } @@ -88,7 +84,7 @@ int _ethtool_etherinfo_init(etherinfo_py *self, PyObject *args, PyObject *kwds) PyErr_SetString(PyExc_AttributeError, "Invalid data pointer to constructor"); return -1; } - self->data = (struct etherinfo_obj_data *) PyCObject_AsVoidPtr(ethinf_ptr); + self->ethinfo = (struct etherinfo *) PyCObject_AsVoidPtr(ethinf_ptr); return 0; } @@ -106,7 +102,7 @@ static PyNetlinkIPaddress * get_last_ipv4_address(etherinfo_py *self) PyObject *list; assert(self); - list = self->data->ethinfo->ipv4_addresses; + list = self->ethinfo->ipv4_addresses; if (!list) { return NULL; } @@ -139,21 +135,21 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o) char *attr = PyString_AsString(attr_o); PyNetlinkIPaddress *py_addr; - if( !self || !self->data ) { + if( !self || !self->ethinfo ) { PyErr_SetString(PyExc_AttributeError, "No data available"); return NULL; } if( strcmp(attr, "device") == 0 ) { - if( self->data->ethinfo->device ) { - return PyString_FromString(self->data->ethinfo->device); + if( self->ethinfo->device ) { + return PyString_FromString(self->ethinfo->device); } else { return Py_INCREF(Py_None), Py_None; } } else if( strcmp(attr, "mac_address") == 0 ) { get_etherinfo(self, NLQRY_LINK); - Py_INCREF(self->data->ethinfo->hwaddress); - return self->data->ethinfo->hwaddress; + Py_INCREF(self->ethinfo->hwaddress); + return self->ethinfo->hwaddress; } else if( strcmp(attr, "ipv4_address") == 0 ) { get_etherinfo(self, NLQRY_ADDR4); /* For compatiblity with old approach, return last IPv4 address: */ @@ -215,7 +211,7 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self) { PyObject *ret = NULL; - if( !self || !self->data || !self->data->ethinfo ) { + if( !self || !self->ethinfo ) { PyErr_SetString(PyExc_AttributeError, "No data available"); return NULL; } @@ -224,17 +220,17 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self) get_etherinfo(self, NLQRY_ADDR4); get_etherinfo(self, NLQRY_ADDR6); - ret = PyString_FromFormat("Device %s:\n", self->data->ethinfo->device); - if( self->data->ethinfo->hwaddress ) { + ret = PyString_FromFormat("Device %s:\n", self->ethinfo->device); + if( self->ethinfo->hwaddress ) { PyString_ConcatAndDel(&ret, PyString_FromString("\tMAC address: ")); - PyString_Concat(&ret, self->data->ethinfo->hwaddress); + PyString_Concat(&ret, self->ethinfo->hwaddress); PyString_ConcatAndDel(&ret, PyString_FromString("\n")); } - if( self->data->ethinfo->ipv4_addresses ) { + if( self->ethinfo->ipv4_addresses ) { Py_ssize_t i; - for (i = 0; i < PyList_Size(self->data->ethinfo->ipv4_addresses); i++) { - PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(self->data->ethinfo->ipv4_addresses, i); + for (i = 0; i < PyList_Size(self->ethinfo->ipv4_addresses); i++) { + PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(self->ethinfo->ipv4_addresses, i); PyObject *tmp = PyString_FromFormat("\tIPv4 address: "); PyString_Concat(&tmp, py_addr->local); PyString_ConcatAndDel(&tmp, PyString_FromFormat("/%d", py_addr->prefixlen)); @@ -248,10 +244,10 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self) } } - if( self->data->ethinfo->ipv6_addresses ) { + if( self->ethinfo->ipv6_addresses ) { Py_ssize_t i; - for (i = 0; i < PyList_Size(self->data->ethinfo->ipv6_addresses); i++) { - PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(self->data->ethinfo->ipv6_addresses, i); + for (i = 0; i < PyList_Size(self->ethinfo->ipv6_addresses); i++) { + PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(self->ethinfo->ipv6_addresses, i); PyObject *tmp = PyString_FromFormat("\tIPv6 address: ["); PyString_Concat(&tmp, py_addr->scope); PyString_ConcatAndDel(&tmp, PyString_FromString("] ")); @@ -276,7 +272,7 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self) static PyObject *_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObject *notused) { PyObject *ret; - if( !self || !self->data ) { + if( !self || !self->ethinfo ) { PyErr_SetString(PyExc_AttributeError, "No data available"); return NULL; } @@ -284,8 +280,8 @@ static PyObject *_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObj get_etherinfo(self, NLQRY_ADDR4); /* Transfer ownership of reference: */ - ret = self->data->ethinfo->ipv4_addresses; - self->data->ethinfo->ipv4_addresses = NULL; + ret = self->ethinfo->ipv4_addresses; + self->ethinfo->ipv4_addresses = NULL; return ret; } @@ -302,7 +298,7 @@ static PyObject *_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObj static PyObject *_ethtool_etherinfo_get_ipv6_addresses(etherinfo_py *self, PyObject *notused) { PyObject *ret; - if( !self || !self->data ) { + if( !self || !self->ethinfo ) { PyErr_SetString(PyExc_AttributeError, "No data available"); return NULL; } @@ -310,8 +306,8 @@ static PyObject *_ethtool_etherinfo_get_ipv6_addresses(etherinfo_py *self, PyObj get_etherinfo(self, NLQRY_ADDR6); /* Transfer ownership of reference: */ - ret = self->data->ethinfo->ipv6_addresses; - self->data->ethinfo->ipv6_addresses = NULL; + ret = self->ethinfo->ipv6_addresses; + self->ethinfo->ipv6_addresses = NULL; return ret; } diff --git a/python-ethtool/etherinfo_struct.h b/python-ethtool/etherinfo_struct.h index 31c71c0..cc7b32f 100644 --- a/python-ethtool/etherinfo_struct.h +++ b/python-ethtool/etherinfo_struct.h @@ -51,21 +51,12 @@ typedef struct PyNetlinkIPaddress { extern PyTypeObject ethtool_netlink_ip_address_Type; /** - * Contains the internal data structure of the - * ethtool.etherinfo object. - * - */ -struct etherinfo_obj_data { - struct etherinfo *ethinfo; /**< Contains info about our current interface */ -}; - -/** - * A Python object of struct etherinfo_obj_data + * The Python object containing information about a single interface * */ typedef struct { PyObject_HEAD - struct etherinfo_obj_data *data; /* IPv4 and IPv6 address information, only one element used */ + struct etherinfo *ethinfo; /**< Information about the interface configuration */ unsigned short nlc_active; /**< Is this instance using NETLINK? */ } etherinfo_py; diff --git a/python-ethtool/ethtool.c b/python-ethtool/ethtool.c index 4aa07bc..7d2a243 100644 --- a/python-ethtool/ethtool.c +++ b/python-ethtool/ethtool.c @@ -268,18 +268,11 @@ static PyObject *get_interfaces_info(PyObject *self __unused, PyObject *args) { devlist = PyList_New(0); for( i = 0; i < fetch_devs_len; i++ ) { - struct etherinfo_obj_data *objdata = NULL; + struct etherinfo *ethinfo = NULL; - /* Allocate memory for data structures for each device */ - objdata = calloc(1, sizeof(struct etherinfo_obj_data)); - if( !objdata ) { - PyErr_SetString(PyExc_OSError, strerror(errno)); - free(fetch_devs); - return NULL; - } - - objdata->ethinfo = calloc(1, sizeof(struct etherinfo)); - if( !objdata->ethinfo ) { + /* Allocate memory for data structures for each interface */ + ethinfo = calloc(1, sizeof(struct etherinfo)); + if( !ethinfo ) { PyErr_SetString(PyExc_OSError, strerror(errno)); free(fetch_devs); return NULL; @@ -288,11 +281,11 @@ static PyObject *get_interfaces_info(PyObject *self __unused, PyObject *args) { /* Store the device name and a reference to the NETLINK connection for * objects to use when quering for device info */ - objdata->ethinfo->device = strdup(fetch_devs[i]); - objdata->ethinfo->index = -1; + ethinfo->device = strdup(fetch_devs[i]); + ethinfo->index = -1; /* Instantiate a new etherinfo object with the device information */ - ethinf_py = PyCObject_FromVoidPtr(objdata, NULL); + ethinf_py = PyCObject_FromVoidPtr(ethinfo, NULL); if( ethinf_py ) { /* Prepare the argument list for the object constructor */ PyObject *args = PyTuple_New(1); @@ -307,7 +300,6 @@ static PyObject *get_interfaces_info(PyObject *self __unused, PyObject *args) { Py_DECREF(args); } } - free(fetch_devs); return devlist; -- cgit