Skip to content

Commit 00e7601

Browse files
committed
Fix exceptions handling
1 parent 0f36da1 commit 00e7601

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

python-ethtool/etherinfo.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ PyObject * get_etherinfo_address(PyEtherInfo *self, nlQuery query)
238238
return NULL;
239239
}
240240

241-
if( _set_device_index(self) != 1) {
242-
return NULL;
243-
}
241+
if(!_set_device_index(self)) {
242+
return NULL;
243+
}
244244

245245
/* Query the for requested info via NETLINK */
246246
/* Extract IP address information */

python-ethtool/etherinfo_obj.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,12 @@ static PyObject *get_ipv4_addr(PyObject *obj, void *info)
236236
return py_addr->local;
237237
}
238238
}
239-
Py_RETURN_NONE;
239+
240+
if (PyErr_Occurred()) {
241+
return NULL;
242+
} else {
243+
Py_RETURN_NONE;
244+
}
240245
}
241246

242247
static PyObject *get_ipv4_mask(PyObject *obj, void *info)
@@ -250,7 +255,12 @@ static PyObject *get_ipv4_mask(PyObject *obj, void *info)
250255
if (py_addr) {
251256
return PyInt_FromLong(py_addr->prefixlen);
252257
}
253-
return PyInt_FromLong(0);
258+
259+
if (PyErr_Occurred()) {
260+
return NULL;
261+
} else {
262+
return PyInt_FromLong(0);
263+
}
254264
}
255265

256266
static PyObject *get_ipv4_bcast(PyObject *obj, void *info)
@@ -267,7 +277,12 @@ static PyObject *get_ipv4_bcast(PyObject *obj, void *info)
267277
return py_addr->ipv4_broadcast;
268278
}
269279
}
270-
Py_RETURN_NONE;
280+
281+
if (PyErr_Occurred()) {
282+
return NULL;
283+
} else {
284+
Py_RETURN_NONE;
285+
}
271286
}
272287

273288

tests/test_ethtool.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ def test_get_interface_info_invalid(self):
206206
self.assertEqual(len(eis), 1)
207207
ei = eis[0]
208208
self.assertEqual(ei.device, INVALID_DEVICE_NAME)
209-
self.assertEqual(ei.ipv4_netmask, 0)
210-
self.assertEqual(ei.ipv4_address, None)
211-
self.assertEqual(ei.ipv4_broadcast, None)
209+
self.assertRaisesIOError(getattr, (ei, 'ipv4_address'), '[Errno 19] No such device')
210+
self.assertRaisesIOError(getattr, (ei, 'ipv4_netmask'), '[Errno 19] No such device')
211+
self.assertRaisesIOError(getattr, (ei, 'ipv4_broadcast'), '[Errno 19] No such device')
212212
self.assertRaisesIOError(getattr, (ei, 'mac_address'), '[Errno 19] No such device')
213213

214214
def test_get_interface_info_active(self):

0 commit comments

Comments
 (0)