Skip to content

Commit 3ce2d57

Browse files
authored
gh-100218: correctly set errno when socket.if_{nametoindex,indextoname} raise OSError (#140905)
Previously, socket.if_nametoindex() and socket.if_indextoname() could raise an `OSError` with a `None` errno. Now, the errno from libc is propagated.
1 parent 0c77e7c commit 3ce2d57

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Lib/test/test_socket.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,10 @@ def testInterfaceNameIndex(self):
11761176
'socket.if_indextoname() not available.')
11771177
@support.skip_android_selinux('if_indextoname')
11781178
def testInvalidInterfaceIndexToName(self):
1179-
self.assertRaises(OSError, socket.if_indextoname, 0)
1179+
with self.assertRaises(OSError) as cm:
1180+
socket.if_indextoname(0)
1181+
self.assertIsNotNone(cm.exception.errno)
1182+
11801183
self.assertRaises(ValueError, socket.if_indextoname, -1)
11811184
self.assertRaises(OverflowError, socket.if_indextoname, 2**1000)
11821185
self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF')
@@ -1196,8 +1199,11 @@ def testInvalidInterfaceIndexToName(self):
11961199
'socket.if_nametoindex() not available.')
11971200
@support.skip_android_selinux('if_nametoindex')
11981201
def testInvalidInterfaceNameToIndex(self):
1202+
with self.assertRaises(OSError) as cm:
1203+
socket.if_nametoindex("_DEADBEEF")
1204+
self.assertIsNotNone(cm.exception.errno)
1205+
11991206
self.assertRaises(TypeError, socket.if_nametoindex, 0)
1200-
self.assertRaises(OSError, socket.if_nametoindex, '_DEADBEEF')
12011207

12021208
@unittest.skipUnless(hasattr(sys, 'getrefcount'),
12031209
'test needs sys.getrefcount()')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Correctly set :attr:`~OSError.errno` when :func:`socket.if_nametoindex` or
2+
:func:`socket.if_indextoname` raise an :exc:`OSError`. Patch by Bénédikt
3+
Tran.

Modules/socketmodule.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7294,10 +7294,10 @@ _socket_if_nametoindex_impl(PyObject *module, PyObject *oname)
72947294
unsigned long index;
72957295
#endif
72967296

7297+
errno = ENODEV; // in case 'if_nametoindex' does not set errno
72977298
index = if_nametoindex(PyBytes_AS_STRING(oname));
72987299
if (index == 0) {
7299-
/* if_nametoindex() doesn't set errno */
7300-
PyErr_SetString(PyExc_OSError, "no interface with this name");
7300+
PyErr_SetFromErrno(PyExc_OSError);
73017301
return NULL;
73027302
}
73037303

@@ -7317,6 +7317,7 @@ static PyObject *
73177317
_socket_if_indextoname_impl(PyObject *module, NET_IFINDEX index)
73187318
/*[clinic end generated code: output=e48bc324993052e0 input=c93f753d0cf6d7d1]*/
73197319
{
7320+
errno = ENXIO; // in case 'if_indextoname' does not set errno
73207321
char name[IF_NAMESIZE + 1];
73217322
if (if_indextoname(index, name) == NULL) {
73227323
PyErr_SetFromErrno(PyExc_OSError);

0 commit comments

Comments
 (0)