Skip to content

Commit a05f17c

Browse files
author
David Sommerseth
committed
Make the internal MAC address a Python string object
Signed-off-by: David Sommerseth <davids@redhat.com>
1 parent 3236cbd commit a05f17c

File tree

3 files changed

+11
-23
lines changed

3 files changed

+11
-23
lines changed

python-ethtool/etherinfo.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,6 @@
4141
*
4242
*/
4343

44-
/**
45-
* Simple macro which makes sure the destination string is freed if used earlier.
46-
*
47-
* @param dst Destination pointer
48-
* @param src Source pointer
49-
*
50-
*/
51-
#define SET_STR_VALUE(dst, src) { \
52-
if( dst ) { \
53-
free(dst); \
54-
}; \
55-
dst = strdup(src); \
56-
}
57-
5844
/**
5945
* Frees the memory used by struct etherinfo
6046
*
@@ -68,9 +54,7 @@ void free_etherinfo(struct etherinfo *ptr)
6854

6955
free(ptr->device);
7056

71-
if( ptr->hwaddress ) {
72-
free(ptr->hwaddress);
73-
}
57+
Py_XDECREF(ptr->hwaddress);
7458
Py_XDECREF(ptr->ipv4_addresses);
7559
Py_XDECREF(ptr->ipv6_addresses);
7660

@@ -97,7 +81,10 @@ static void callback_nl_link(struct nl_object *obj, void *arg)
9781

9882
memset(&hwaddr, 0, 130);
9983
nl_addr2str(rtnl_link_get_addr(link), hwaddr, sizeof(hwaddr));
100-
SET_STR_VALUE(ethi->hwaddress, hwaddr);
84+
if( ethi->hwaddress ) {
85+
Py_XDECREF(ethi->hwaddress);
86+
}
87+
ethi->hwaddress = PyString_FromFormat("%s", hwaddr);
10188
}
10289

10390

python-ethtool/etherinfo_obj.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
148148
return RETURN_STRING(self->data->ethinfo->device);
149149
} else if( strcmp(attr, "mac_address") == 0 ) {
150150
get_etherinfo(self->data, NLQRY_LINK);
151-
return RETURN_STRING(self->data->ethinfo->hwaddress);
151+
Py_INCREF(self->data->ethinfo->hwaddress);
152+
return self->data->ethinfo->hwaddress;
152153
} else if( strcmp(attr, "ipv4_address") == 0 ) {
153154
get_etherinfo(self->data, NLQRY_ADDR);
154155
/* For compatiblity with old approach, return last IPv4 address: */
@@ -220,9 +221,9 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
220221

221222
ret = PyString_FromFormat("Device %s:\n", self->data->ethinfo->device);
222223
if( self->data->ethinfo->hwaddress ) {
223-
PyObject *tmp = PyString_FromFormat("\tMAC address: %s\n", self->data->ethinfo->hwaddress);
224-
PyString_Concat(&ret, tmp);
225-
Py_DECREF(tmp);
224+
PyString_ConcatAndDel(&ret, PyString_FromString("\tMAC address: "));
225+
PyString_Concat(&ret, self->data->ethinfo->hwaddress);
226+
PyString_ConcatAndDel(&ret, PyString_FromString("\n"));
226227
}
227228

228229
if( self->data->ethinfo->ipv4_addresses ) {

python-ethtool/etherinfo_struct.h

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

0 commit comments

Comments
 (0)