Skip to content

Commit e3036b2

Browse files
author
David Sommerseth
committed
Kick out struct etherinfo_obj_data
This simplifies and clarifies the object/struct relations a bit better. Signed-off-by: David Sommerseth <davids@redhat.com>
1 parent 4c301d9 commit e3036b2

File tree

4 files changed

+37
-58
lines changed

4 files changed

+37
-58
lines changed

python-ethtool/etherinfo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ int get_etherinfo(etherinfo_py *self, nlQuery query)
153153

154154
int ret = 0;
155155

156-
if( !self || !self->data || !self->data->ethinfo ) {
156+
if( !self || !self->ethinfo ) {
157157
return 0;
158158
}
159-
ethinf = self->data->ethinfo;
159+
ethinf = self->ethinfo;
160160

161161
/* Open a NETLINK connection on-the-fly */
162162
if( !open_netlink(self) ) {

python-ethtool/etherinfo_obj.c

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,9 @@ extern PyTypeObject ethtool_etherinfoIPv6Type;
4040
*/
4141
void _ethtool_etherinfo_dealloc(etherinfo_py *self)
4242
{
43-
if( self->data ) {
44-
close_netlink(self);
45-
46-
if( self->data->ethinfo ) {
47-
free_etherinfo(self->data->ethinfo);
48-
}
49-
free(self->data);
43+
close_netlink(self);
44+
if( self->ethinfo ) {
45+
free_etherinfo(self->ethinfo);
5046
}
5147
self->ob_type->tp_free((PyObject*)self);
5248
}
@@ -88,7 +84,7 @@ int _ethtool_etherinfo_init(etherinfo_py *self, PyObject *args, PyObject *kwds)
8884
PyErr_SetString(PyExc_AttributeError, "Invalid data pointer to constructor");
8985
return -1;
9086
}
91-
self->data = (struct etherinfo_obj_data *) PyCObject_AsVoidPtr(ethinf_ptr);
87+
self->ethinfo = (struct etherinfo *) PyCObject_AsVoidPtr(ethinf_ptr);
9288
return 0;
9389
}
9490

@@ -106,7 +102,7 @@ static PyNetlinkIPaddress * get_last_ipv4_address(etherinfo_py *self)
106102
PyObject *list;
107103

108104
assert(self);
109-
list = self->data->ethinfo->ipv4_addresses;
105+
list = self->ethinfo->ipv4_addresses;
110106
if (!list) {
111107
return NULL;
112108
}
@@ -139,21 +135,21 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
139135
char *attr = PyString_AsString(attr_o);
140136
PyNetlinkIPaddress *py_addr;
141137

142-
if( !self || !self->data ) {
138+
if( !self || !self->ethinfo ) {
143139
PyErr_SetString(PyExc_AttributeError, "No data available");
144140
return NULL;
145141
}
146142

147143
if( strcmp(attr, "device") == 0 ) {
148-
if( self->data->ethinfo->device ) {
149-
return PyString_FromString(self->data->ethinfo->device);
144+
if( self->ethinfo->device ) {
145+
return PyString_FromString(self->ethinfo->device);
150146
} else {
151147
return Py_INCREF(Py_None), Py_None;
152148
}
153149
} else if( strcmp(attr, "mac_address") == 0 ) {
154150
get_etherinfo(self, NLQRY_LINK);
155-
Py_INCREF(self->data->ethinfo->hwaddress);
156-
return self->data->ethinfo->hwaddress;
151+
Py_INCREF(self->ethinfo->hwaddress);
152+
return self->ethinfo->hwaddress;
157153
} else if( strcmp(attr, "ipv4_address") == 0 ) {
158154
get_etherinfo(self, NLQRY_ADDR4);
159155
/* For compatiblity with old approach, return last IPv4 address: */
@@ -215,7 +211,7 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
215211
{
216212
PyObject *ret = NULL;
217213

218-
if( !self || !self->data || !self->data->ethinfo ) {
214+
if( !self || !self->ethinfo ) {
219215
PyErr_SetString(PyExc_AttributeError, "No data available");
220216
return NULL;
221217
}
@@ -224,17 +220,17 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
224220
get_etherinfo(self, NLQRY_ADDR4);
225221
get_etherinfo(self, NLQRY_ADDR6);
226222

227-
ret = PyString_FromFormat("Device %s:\n", self->data->ethinfo->device);
228-
if( self->data->ethinfo->hwaddress ) {
223+
ret = PyString_FromFormat("Device %s:\n", self->ethinfo->device);
224+
if( self->ethinfo->hwaddress ) {
229225
PyString_ConcatAndDel(&ret, PyString_FromString("\tMAC address: "));
230-
PyString_Concat(&ret, self->data->ethinfo->hwaddress);
226+
PyString_Concat(&ret, self->ethinfo->hwaddress);
231227
PyString_ConcatAndDel(&ret, PyString_FromString("\n"));
232228
}
233229

234-
if( self->data->ethinfo->ipv4_addresses ) {
230+
if( self->ethinfo->ipv4_addresses ) {
235231
Py_ssize_t i;
236-
for (i = 0; i < PyList_Size(self->data->ethinfo->ipv4_addresses); i++) {
237-
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(self->data->ethinfo->ipv4_addresses, i);
232+
for (i = 0; i < PyList_Size(self->ethinfo->ipv4_addresses); i++) {
233+
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(self->ethinfo->ipv4_addresses, i);
238234
PyObject *tmp = PyString_FromFormat("\tIPv4 address: ");
239235
PyString_Concat(&tmp, py_addr->local);
240236
PyString_ConcatAndDel(&tmp, PyString_FromFormat("/%d", py_addr->prefixlen));
@@ -248,10 +244,10 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
248244
}
249245
}
250246

251-
if( self->data->ethinfo->ipv6_addresses ) {
247+
if( self->ethinfo->ipv6_addresses ) {
252248
Py_ssize_t i;
253-
for (i = 0; i < PyList_Size(self->data->ethinfo->ipv6_addresses); i++) {
254-
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(self->data->ethinfo->ipv6_addresses, i);
249+
for (i = 0; i < PyList_Size(self->ethinfo->ipv6_addresses); i++) {
250+
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(self->ethinfo->ipv6_addresses, i);
255251
PyObject *tmp = PyString_FromFormat("\tIPv6 address: [");
256252
PyString_Concat(&tmp, py_addr->scope);
257253
PyString_ConcatAndDel(&tmp, PyString_FromString("] "));
@@ -276,16 +272,16 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
276272
static PyObject *_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObject *notused) {
277273
PyObject *ret;
278274

279-
if( !self || !self->data ) {
275+
if( !self || !self->ethinfo ) {
280276
PyErr_SetString(PyExc_AttributeError, "No data available");
281277
return NULL;
282278
}
283279

284280
get_etherinfo(self, NLQRY_ADDR4);
285281

286282
/* Transfer ownership of reference: */
287-
ret = self->data->ethinfo->ipv4_addresses;
288-
self->data->ethinfo->ipv4_addresses = NULL;
283+
ret = self->ethinfo->ipv4_addresses;
284+
self->ethinfo->ipv4_addresses = NULL;
289285

290286
return ret;
291287
}
@@ -302,16 +298,16 @@ static PyObject *_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObj
302298
static PyObject *_ethtool_etherinfo_get_ipv6_addresses(etherinfo_py *self, PyObject *notused) {
303299
PyObject *ret;
304300

305-
if( !self || !self->data ) {
301+
if( !self || !self->ethinfo ) {
306302
PyErr_SetString(PyExc_AttributeError, "No data available");
307303
return NULL;
308304
}
309305

310306
get_etherinfo(self, NLQRY_ADDR6);
311307

312308
/* Transfer ownership of reference: */
313-
ret = self->data->ethinfo->ipv6_addresses;
314-
self->data->ethinfo->ipv6_addresses = NULL;
309+
ret = self->ethinfo->ipv6_addresses;
310+
self->ethinfo->ipv6_addresses = NULL;
315311

316312
return ret;
317313
}

python-ethtool/etherinfo_struct.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,12 @@ typedef struct PyNetlinkIPaddress {
5151
extern PyTypeObject ethtool_netlink_ip_address_Type;
5252

5353
/**
54-
* Contains the internal data structure of the
55-
* ethtool.etherinfo object.
56-
*
57-
*/
58-
struct etherinfo_obj_data {
59-
struct etherinfo *ethinfo; /**< Contains info about our current interface */
60-
};
61-
62-
/**
63-
* A Python object of struct etherinfo_obj_data
54+
* The Python object containing information about a single interface
6455
*
6556
*/
6657
typedef struct {
6758
PyObject_HEAD
68-
struct etherinfo_obj_data *data; /* IPv4 and IPv6 address information, only one element used */
59+
struct etherinfo *ethinfo; /**< Information about the interface configuration */
6960
unsigned short nlc_active; /**< Is this instance using NETLINK? */
7061
} etherinfo_py;
7162

python-ethtool/ethtool.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,11 @@ static PyObject *get_interfaces_info(PyObject *self __unused, PyObject *args) {
268268

269269
devlist = PyList_New(0);
270270
for( i = 0; i < fetch_devs_len; i++ ) {
271-
struct etherinfo_obj_data *objdata = NULL;
271+
struct etherinfo *ethinfo = NULL;
272272

273-
/* Allocate memory for data structures for each device */
274-
objdata = calloc(1, sizeof(struct etherinfo_obj_data));
275-
if( !objdata ) {
276-
PyErr_SetString(PyExc_OSError, strerror(errno));
277-
free(fetch_devs);
278-
return NULL;
279-
}
280-
281-
objdata->ethinfo = calloc(1, sizeof(struct etherinfo));
282-
if( !objdata->ethinfo ) {
273+
/* Allocate memory for data structures for each interface */
274+
ethinfo = calloc(1, sizeof(struct etherinfo));
275+
if( !ethinfo ) {
283276
PyErr_SetString(PyExc_OSError, strerror(errno));
284277
free(fetch_devs);
285278
return NULL;
@@ -288,11 +281,11 @@ static PyObject *get_interfaces_info(PyObject *self __unused, PyObject *args) {
288281
/* Store the device name and a reference to the NETLINK connection for
289282
* objects to use when quering for device info
290283
*/
291-
objdata->ethinfo->device = strdup(fetch_devs[i]);
292-
objdata->ethinfo->index = -1;
284+
ethinfo->device = strdup(fetch_devs[i]);
285+
ethinfo->index = -1;
293286

294287
/* Instantiate a new etherinfo object with the device information */
295-
ethinf_py = PyCObject_FromVoidPtr(objdata, NULL);
288+
ethinf_py = PyCObject_FromVoidPtr(ethinfo, NULL);
296289
if( ethinf_py ) {
297290
/* Prepare the argument list for the object constructor */
298291
PyObject *args = PyTuple_New(1);
@@ -307,7 +300,6 @@ static PyObject *get_interfaces_info(PyObject *self __unused, PyObject *args) {
307300
Py_DECREF(args);
308301
}
309302
}
310-
311303
free(fetch_devs);
312304

313305
return devlist;

0 commit comments

Comments
 (0)