diff options
Diffstat (limited to 'python-ethtool')
-rw-r--r-- | python-ethtool/etherinfo.c | 23 | ||||
-rw-r--r-- | python-ethtool/etherinfo_obj.c | 9 | ||||
-rw-r--r-- | python-ethtool/etherinfo_struct.h | 2 |
3 files changed, 11 insertions, 23 deletions
diff --git a/python-ethtool/etherinfo.c b/python-ethtool/etherinfo.c index 6f7778c..37e86e3 100644 --- a/python-ethtool/etherinfo.c +++ b/python-ethtool/etherinfo.c @@ -42,20 +42,6 @@ */ /** - * Simple macro which makes sure the destination string is freed if used earlier. - * - * @param dst Destination pointer - * @param src Source pointer - * - */ -#define SET_STR_VALUE(dst, src) { \ - if( dst ) { \ - free(dst); \ - }; \ - dst = strdup(src); \ - } - -/** * Frees the memory used by struct etherinfo * * @param ptr Pointer to a struct etherninfo element @@ -68,9 +54,7 @@ void free_etherinfo(struct etherinfo *ptr) free(ptr->device); - if( ptr->hwaddress ) { - free(ptr->hwaddress); - } + Py_XDECREF(ptr->hwaddress); Py_XDECREF(ptr->ipv4_addresses); Py_XDECREF(ptr->ipv6_addresses); @@ -97,7 +81,10 @@ static void callback_nl_link(struct nl_object *obj, void *arg) memset(&hwaddr, 0, 130); nl_addr2str(rtnl_link_get_addr(link), hwaddr, sizeof(hwaddr)); - SET_STR_VALUE(ethi->hwaddress, hwaddr); + if( ethi->hwaddress ) { + Py_XDECREF(ethi->hwaddress); + } + ethi->hwaddress = PyString_FromFormat("%s", hwaddr); } diff --git a/python-ethtool/etherinfo_obj.c b/python-ethtool/etherinfo_obj.c index a251b56..6acc571 100644 --- a/python-ethtool/etherinfo_obj.c +++ b/python-ethtool/etherinfo_obj.c @@ -148,7 +148,8 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o) return RETURN_STRING(self->data->ethinfo->device); } else if( strcmp(attr, "mac_address") == 0 ) { get_etherinfo(self->data, NLQRY_LINK); - return RETURN_STRING(self->data->ethinfo->hwaddress); + Py_INCREF(self->data->ethinfo->hwaddress); + return self->data->ethinfo->hwaddress; } else if( strcmp(attr, "ipv4_address") == 0 ) { get_etherinfo(self->data, NLQRY_ADDR); /* For compatiblity with old approach, return last IPv4 address: */ @@ -220,9 +221,9 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self) ret = PyString_FromFormat("Device %s:\n", self->data->ethinfo->device); if( self->data->ethinfo->hwaddress ) { - PyObject *tmp = PyString_FromFormat("\tMAC address: %s\n", self->data->ethinfo->hwaddress); - PyString_Concat(&ret, tmp); - Py_DECREF(tmp); + PyString_ConcatAndDel(&ret, PyString_FromString("\tMAC address: ")); + PyString_Concat(&ret, self->data->ethinfo->hwaddress); + PyString_ConcatAndDel(&ret, PyString_FromString("\n")); } if( self->data->ethinfo->ipv4_addresses ) { diff --git a/python-ethtool/etherinfo_struct.h b/python-ethtool/etherinfo_struct.h index 72e4962..e5e6ecd 100644 --- a/python-ethtool/etherinfo_struct.h +++ b/python-ethtool/etherinfo_struct.h @@ -33,7 +33,7 @@ struct etherinfo { char *device; /**< Device name */ int index; /**< NETLINK index reference */ - char *hwaddress; /**< HW address / MAC address of device */ + PyObject *hwaddress; /**< string: HW address / MAC address of device */ PyObject *ipv4_addresses; /**< list of PyNetlinkIPv4Address instances */ PyObject *ipv6_addresses; /**< list of PyNetlinkIPv6Addresses instances */ }; |