Skip to content

Commit d4bc3a5

Browse files
author
David Sommerseth
committed
Make the device string a python object as well
This will simplify to "pythonize" struct etherinfo further. Signed-off-by: David Sommerseth <davids@redhat.com>
1 parent 5070463 commit d4bc3a5

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

python-ethtool/etherinfo.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ void free_etherinfo(struct etherinfo *ptr)
5252
return;
5353
}
5454

55-
free(ptr->device);
56-
55+
Py_XDECREF(ptr->device);
5756
Py_XDECREF(ptr->hwaddress);
5857

5958
free(ptr);
@@ -145,7 +144,7 @@ static int _set_device_index(struct etherinfo *ethinf)
145144
return 0;
146145
}
147146

148-
link = rtnl_link_get_by_name(link_cache, ethinf->device);
147+
link = rtnl_link_get_by_name(link_cache, PyString_AsString(ethinf->device));
149148
if( !link ) {
150149
nl_cache_free(link_cache);
151150
return 0;
@@ -185,7 +184,7 @@ int get_etherinfo_link(etherinfo_py *self)
185184
if( !open_netlink(self) ) {
186185
PyErr_Format(PyExc_RuntimeError,
187186
"Could not open a NETLINK connection for %s",
188-
ethinf->device);
187+
PyString_AsString(ethinf->device));
189188
return 0;
190189
}
191190

@@ -235,7 +234,7 @@ PyObject * get_etherinfo_address(etherinfo_py *self, nlQuery query)
235234
if( !open_netlink(self) ) {
236235
PyErr_Format(PyExc_RuntimeError,
237236
"Could not open a NETLINK connection for %s",
238-
ethinf->device);
237+
PyString_AsString(ethinf->device));
239238
return NULL;
240239
}
241240

python-ethtool/etherinfo_obj.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
138138

139139
if( strcmp(attr, "device") == 0 ) {
140140
if( self->ethinfo->device ) {
141-
return PyString_FromString(self->ethinfo->device);
141+
Py_INCREF(self->ethinfo->device);
142+
return self->ethinfo->device;
142143
} else {
143144
return Py_INCREF(Py_None), Py_None;
144145
}
@@ -215,7 +216,10 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
215216

216217
get_etherinfo_link(self);
217218

218-
ret = PyString_FromFormat("Device %s:\n", self->ethinfo->device);
219+
ret = PyString_FromFormat("Device ");
220+
PyString_Concat(&ret, self->ethinfo->device);
221+
PyString_ConcatAndDel(&ret, PyString_FromString(":\n"));
222+
219223
if( self->ethinfo->hwaddress ) {
220224
PyString_ConcatAndDel(&ret, PyString_FromString("\tMAC address: "));
221225
PyString_Concat(&ret, self->ethinfo->hwaddress);

python-ethtool/etherinfo_struct.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*
3434
*/
3535
struct etherinfo {
36-
char *device; /**< Device name */
36+
PyObject *device; /**< Device name */
3737
int index; /**< NETLINK index reference */
3838
PyObject *hwaddress; /**< string: HW address / MAC address of device */
3939
};

python-ethtool/ethtool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ static PyObject *get_interfaces_info(PyObject *self __unused, PyObject *args) {
281281
/* Store the device name and a reference to the NETLINK connection for
282282
* objects to use when quering for device info
283283
*/
284-
ethinfo->device = strdup(fetch_devs[i]);
284+
ethinfo->device = PyString_FromString(fetch_devs[i]);
285285
ethinfo->index = -1;
286286

287287
/* Instantiate a new etherinfo object with the device information */

0 commit comments

Comments
 (0)