@@ -96,26 +96,23 @@ int _ethtool_etherinfo_init(etherinfo_py *self, PyObject *args, PyObject *kwds)
9696
9797 The return value is a *borrowed reference* (or NULL)
9898*/
99- static PyNetlinkIPaddress * get_last_ipv4_address (etherinfo_py * self )
99+ static PyNetlinkIPaddress * get_last_ipv4_address (PyObject * addrlist )
100100{
101101 Py_ssize_t size ;
102- PyObject * list ;
103102
104- assert (self );
105- list = self -> ethinfo -> ipv4_addresses ;
106- if (!list ) {
103+ if (!addrlist ) {
107104 return NULL ;
108105 }
109106
110- if (!PyList_Check (list )) {
107+ if (!PyList_Check (addrlist )) {
111108 return NULL ;
112109 }
113110
114- size = PyList_Size (list );
111+ size = PyList_Size (addrlist );
115112 if (size > 0 ) {
116- PyObject * item = PyList_GetItem (list , size - 1 );
113+ PyNetlinkIPaddress * item = ( PyNetlinkIPaddress * ) PyList_GetItem (addrlist , size - 1 );
117114 if (Py_TYPE (item ) == & ethtool_netlink_ip_address_Type ) {
118- return ( PyNetlinkIPaddress * ) item ;
115+ return item ;
119116 }
120117 }
121118
@@ -134,6 +131,7 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
134131{
135132 char * attr = PyString_AsString (attr_o );
136133 PyNetlinkIPaddress * py_addr ;
134+ PyObject * addrlist = NULL ;
137135
138136 if ( !self || !self -> ethinfo ) {
139137 PyErr_SetString (PyExc_AttributeError , "No data available" );
@@ -151,9 +149,9 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
151149 Py_INCREF (self -> ethinfo -> hwaddress );
152150 return self -> ethinfo -> hwaddress ;
153151 } else if ( strcmp (attr , "ipv4_address" ) == 0 ) {
154- get_etherinfo_address (self , NLQRY_ADDR4 );
152+ addrlist = get_etherinfo_address (self , NLQRY_ADDR4 );
155153 /* For compatiblity with old approach, return last IPv4 address: */
156- py_addr = get_last_ipv4_address (self );
154+ py_addr = get_last_ipv4_address (addrlist );
157155 if (py_addr ) {
158156 if (py_addr -> local ) {
159157 Py_INCREF (py_addr -> local );
@@ -162,15 +160,15 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
162160 }
163161 Py_RETURN_NONE ;
164162 } else if ( strcmp (attr , "ipv4_netmask" ) == 0 ) {
165- get_etherinfo_address (self , NLQRY_ADDR4 );
166- py_addr = get_last_ipv4_address (self );
163+ addrlist = get_etherinfo_address (self , NLQRY_ADDR4 );
164+ py_addr = get_last_ipv4_address (addrlist );
167165 if (py_addr ) {
168166 return PyInt_FromLong (py_addr -> prefixlen );
169167 }
170168 return PyInt_FromLong (0 );
171169 } else if ( strcmp (attr , "ipv4_broadcast" ) == 0 ) {
172- get_etherinfo_address (self , NLQRY_ADDR4 );
173- py_addr = get_last_ipv4_address (self );
170+ addrlist = get_etherinfo_address (self , NLQRY_ADDR4 );
171+ py_addr = get_last_ipv4_address (addrlist );
174172 if (py_addr ) {
175173 if (py_addr -> ipv4_broadcast ) {
176174 Py_INCREF (py_addr -> ipv4_broadcast );
@@ -210,15 +208,14 @@ int _ethtool_etherinfo_setter(etherinfo_py *self, PyObject *attr_o, PyObject *va
210208PyObject * _ethtool_etherinfo_str (etherinfo_py * self )
211209{
212210 PyObject * ret = NULL ;
211+ PyObject * ipv4addrs = NULL , * ipv6addrs = NULL ;
213212
214213 if ( !self || !self -> ethinfo ) {
215214 PyErr_SetString (PyExc_AttributeError , "No data available" );
216215 return NULL ;
217216 }
218217
219218 get_etherinfo_link (self );
220- get_etherinfo_address (self , NLQRY_ADDR4 );
221- get_etherinfo_address (self , NLQRY_ADDR6 );
222219
223220 ret = PyString_FromFormat ("Device %s:\n" , self -> ethinfo -> device );
224221 if ( self -> ethinfo -> hwaddress ) {
@@ -227,10 +224,11 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
227224 PyString_ConcatAndDel (& ret , PyString_FromString ("\n" ));
228225 }
229226
230- if ( self -> ethinfo -> ipv4_addresses ) {
227+ ipv4addrs = get_etherinfo_address (self , NLQRY_ADDR4 );
228+ if ( ipv4addrs ) {
231229 Py_ssize_t 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 );
230+ for (i = 0 ; i < PyList_Size (ipv4addrs ); i ++ ) {
231+ PyNetlinkIPaddress * py_addr = (PyNetlinkIPaddress * )PyList_GetItem (ipv4addrs , i );
234232 PyObject * tmp = PyString_FromFormat ("\tIPv4 address: " );
235233 PyString_Concat (& tmp , py_addr -> local );
236234 PyString_ConcatAndDel (& tmp , PyString_FromFormat ("/%d" , py_addr -> prefixlen ));
@@ -244,10 +242,11 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
244242 }
245243 }
246244
247- if ( self -> ethinfo -> ipv6_addresses ) {
245+ ipv6addrs = get_etherinfo_address (self , NLQRY_ADDR6 );
246+ if ( ipv6addrs ) {
248247 Py_ssize_t 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 );
248+ for (i = 0 ; i < PyList_Size (ipv6addrs ); i ++ ) {
249+ PyNetlinkIPaddress * py_addr = (PyNetlinkIPaddress * )PyList_GetItem (ipv6addrs , i );
251250 PyObject * tmp = PyString_FromFormat ("\tIPv6 address: [" );
252251 PyString_Concat (& tmp , py_addr -> scope );
253252 PyString_ConcatAndDel (& tmp , PyString_FromString ("] " ));
@@ -270,20 +269,12 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
270269 * @return Returns a Python tuple list of NetlinkIP4Address objects
271270 */
272271static PyObject * _ethtool_etherinfo_get_ipv4_addresses (etherinfo_py * self , PyObject * notused ) {
273- PyObject * ret ;
274-
275272 if ( !self || !self -> ethinfo ) {
276273 PyErr_SetString (PyExc_AttributeError , "No data available" );
277274 return NULL ;
278275 }
279276
280- get_etherinfo_address (self , NLQRY_ADDR4 );
281-
282- /* Transfer ownership of reference: */
283- ret = self -> ethinfo -> ipv4_addresses ;
284- self -> ethinfo -> ipv4_addresses = NULL ;
285-
286- return ret ;
277+ return get_etherinfo_address (self , NLQRY_ADDR4 );
287278}
288279
289280
@@ -296,20 +287,12 @@ static PyObject *_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObj
296287 * @return Returns a Python tuple list of NetlinkIP6Address objects
297288 */
298289static PyObject * _ethtool_etherinfo_get_ipv6_addresses (etherinfo_py * self , PyObject * notused ) {
299- PyObject * ret ;
300-
301290 if ( !self || !self -> ethinfo ) {
302291 PyErr_SetString (PyExc_AttributeError , "No data available" );
303292 return NULL ;
304293 }
305294
306- get_etherinfo_address (self , NLQRY_ADDR6 );
307-
308- /* Transfer ownership of reference: */
309- ret = self -> ethinfo -> ipv6_addresses ;
310- self -> ethinfo -> ipv6_addresses = NULL ;
311-
312- return ret ;
295+ return get_etherinfo_address (self , NLQRY_ADDR6 );
313296}
314297
315298
0 commit comments