Skip to content

Commit b9e0539

Browse files
committed
Port etherinfo __str__ function
1 parent 16bfe19 commit b9e0539

File tree

3 files changed

+41
-54
lines changed

3 files changed

+41
-54
lines changed

python-ethtool/etherinfo_obj.c

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static PyNetlinkIPaddress * get_last_ipv4_address(PyObject *addrlist)
8888
PyObject *_ethtool_etherinfo_str(PyEtherInfo *self)
8989
{
9090
PyObject *ret = NULL;
91-
PyObject *ipv4addrs = NULL, *ipv6addrs = NULL;
91+
PyObject *ipv4addrs = NULL, *ipv6addrs = NULL;
9292

9393
if( !self ) {
9494
PyErr_SetString(PyExc_AttributeError, "No data available");
@@ -97,56 +97,46 @@ PyObject *_ethtool_etherinfo_str(PyEtherInfo *self)
9797

9898
get_etherinfo_link(self);
9999

100-
ret = PyBytes_FromFormat("Device ");
101-
PyBytes_Concat(&ret, self->device);
102-
PyBytes_ConcatAndDel(&ret, PyBytes_FromString(":\n"));
100+
ret = PyStr_FromFormat("Device %s:\n", PyStr_AsString(self->device));
103101

104102
if( self->hwaddress ) {
105-
PyBytes_ConcatAndDel(&ret, PyBytes_FromString("\tMAC address: "));
106-
PyBytes_Concat(&ret, self->hwaddress);
107-
PyBytes_ConcatAndDel(&ret, PyBytes_FromString("\n"));
103+
ret = PyStr_Concat(ret,
104+
PyStr_FromFormat("\tMAC address: %s\n",
105+
PyStr_AsString(self->hwaddress)));
108106
}
109107

110108
ipv4addrs = get_etherinfo_address(self, NLQRY_ADDR4);
111109
if( ipv4addrs ) {
112-
Py_ssize_t i;
113-
for (i = 0; i < PyList_Size(ipv4addrs); i++) {
114-
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(ipv4addrs, i);
115-
PyObject *tmp = PyBytes_FromFormat("\tIPv4 address: ");
116-
PyBytes_Concat(&tmp, py_addr->local);
117-
PyBytes_ConcatAndDel(&tmp, PyBytes_FromFormat("/%d", py_addr->prefixlen));
118-
if (py_addr->ipv4_broadcast ) {
119-
PyBytes_ConcatAndDel(&tmp,
120-
PyBytes_FromString(" Broadcast: "));
121-
PyBytes_Concat(&tmp, py_addr->ipv4_broadcast);
122-
}
123-
PyBytes_ConcatAndDel(&tmp, PyBytes_FromString("\n"));
124-
PyBytes_ConcatAndDel(&ret, tmp);
125-
}
110+
Py_ssize_t i;
111+
for (i = 0; i < PyList_Size(ipv4addrs); i++) {
112+
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(ipv4addrs, i);
113+
PyObject *tmp = PyStr_FromFormat("\tIPv4 address: ");
114+
tmp = PyStr_Concat(tmp, py_addr->local);
115+
tmp = PyStr_Concat(tmp, PyStr_FromFormat("/%d", py_addr->prefixlen));
116+
if (py_addr->ipv4_broadcast ) {
117+
tmp = PyStr_Concat(tmp,
118+
PyStr_FromFormat("\tBroadcast: %s\n",
119+
PyStr_AsString(py_addr->ipv4_broadcast)));
120+
} else {
121+
tmp = PyStr_Concat(tmp, PyStr_FromFormat("\n"));
122+
}
123+
124+
ret = PyStr_Concat(ret, tmp);
125+
}
126126
}
127127

128128
ipv6addrs = get_etherinfo_address(self, NLQRY_ADDR6);
129129
if( ipv6addrs ) {
130-
Py_ssize_t i;
131-
for (i = 0; i < PyList_Size(ipv6addrs); i++) {
132-
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(ipv6addrs, i);
133-
PyObject *tmp = PyBytes_FromFormat("\tIPv6 address: [");
134-
PyBytes_Concat(&tmp, py_addr->scope);
135-
PyBytes_ConcatAndDel(&tmp, PyBytes_FromString("] "));
136-
PyBytes_Concat(&tmp, py_addr->local);
137-
PyBytes_ConcatAndDel(&tmp, PyBytes_FromFormat("/%d", py_addr->prefixlen));
138-
PyBytes_ConcatAndDel(&tmp, PyBytes_FromString("\n"));
139-
PyBytes_ConcatAndDel(&ret, tmp);
140-
}
141-
}
142-
143-
#if PY_MAJOR_VERSION >= 3
144-
{
145-
PyObject *bytestr = ret;
146-
ret = PyUnicode_FromString(PyBytes_AsString(bytestr));
147-
Py_DECREF(bytestr);
130+
Py_ssize_t i;
131+
for (i = 0; i < PyList_Size(ipv6addrs); i++) {
132+
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(ipv6addrs, i);
133+
PyObject *tmp = PyStr_FromFormat("\tIPv6 address: [%s] %s/%d\n",
134+
PyStr_AsString(py_addr->scope),
135+
PyStr_AsString(py_addr->local),
136+
py_addr->prefixlen);
137+
ret = PyStr_Concat(ret, tmp);
138+
}
148139
}
149-
#endif
150140

151141
return ret;
152142
}

python-ethtool/include/py3c/compat.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,16 @@ SOFTWARE.
9595
#define PyStr_InternFromString PyString_InternFromString
9696
#define PyStr_Decode PyString_Decode
9797

98-
//
99-
// Suppress warning about unused function
100-
//
101-
// static PyObject *PyStr_Concat(PyObject *left, PyObject *right) {
102-
// PyObject *str = left;
103-
// Py_INCREF(left); // reference to old left will be stolen
104-
// PyString_Concat(&str, right);
105-
// if (str) {
106-
// return str;
107-
// } else {
108-
// return NULL;
109-
// }
110-
// }
98+
static PyObject *PyStr_Concat(PyObject *left, PyObject *right) {
99+
PyObject *str = left;
100+
Py_INCREF(left); // reference to old left will be stolen
101+
PyString_Concat(&str, right);
102+
if (str) {
103+
return str;
104+
} else {
105+
return NULL;
106+
}
107+
}
111108

112109
#define PyStr_AsUTF8String(str) (Py_INCREF(str), (str))
113110
#define PyStr_AsUTF8 PyString_AsString

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def libraries(self, value):
131131
'python-ethtool/etherinfo_obj.c',
132132
'python-ethtool/netlink.c',
133133
'python-ethtool/netlink-address.c'],
134-
extra_compile_args=['-fno-strict-aliasing'],
134+
extra_compile_args=['-fno-strict-aliasing', '-Wno-unused-function'],
135135
define_macros = [('VERSION', '"%s"' % version)],
136136
pkg = 'libnl-3.0',
137137
extra_libraries = ['nl-route-3'],

0 commit comments

Comments
 (0)