Skip to content

Commit 873f0d0

Browse files
authored
[3.13] gh-100218: correctly set errno when socket.if_{nametoindex,indextoname} raise OSError (GH-140905) (#141285)
Previously, socket.if_nametoindex() and socket.if_indextoname() could raise an `OSError` with a `None` errno. Now, the errno from libc is propagated. (cherry picked from commit 3ce2d57)
1 parent 052a539 commit 873f0d0

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
@@ -1167,7 +1167,10 @@ def testInterfaceNameIndex(self):
11671167
@unittest.skipUnless(hasattr(socket, 'if_indextoname'),
11681168
'socket.if_indextoname() not available.')
11691169
def testInvalidInterfaceIndexToName(self):
1170-
self.assertRaises(OSError, socket.if_indextoname, 0)
1170+
with self.assertRaises(OSError) as cm:
1171+
socket.if_indextoname(0)
1172+
self.assertIsNotNone(cm.exception.errno)
1173+
11711174
self.assertRaises(OverflowError, socket.if_indextoname, -1)
11721175
self.assertRaises(OverflowError, socket.if_indextoname, 2**1000)
11731176
self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF')
@@ -1186,8 +1189,11 @@ def testInvalidInterfaceIndexToName(self):
11861189
@unittest.skipUnless(hasattr(socket, 'if_nametoindex'),
11871190
'socket.if_nametoindex() not available.')
11881191
def testInvalidInterfaceNameToIndex(self):
1192+
with self.assertRaises(OSError) as cm:
1193+
socket.if_nametoindex("_DEADBEEF")
1194+
self.assertIsNotNone(cm.exception.errno)
1195+
11891196
self.assertRaises(TypeError, socket.if_nametoindex, 0)
1190-
self.assertRaises(OSError, socket.if_nametoindex, '_DEADBEEF')
11911197

11921198
@unittest.skipUnless(hasattr(sys, 'getrefcount'),
11931199
'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
@@ -7114,10 +7114,10 @@ _socket_socket_if_nametoindex_impl(PySocketSockObject *self, PyObject *oname)
71147114
unsigned long index;
71157115
#endif
71167116

7117+
errno = ENODEV; // in case 'if_nametoindex' does not set errno
71177118
index = if_nametoindex(PyBytes_AS_STRING(oname));
71187119
if (index == 0) {
7119-
/* if_nametoindex() doesn't set errno */
7120-
PyErr_SetString(PyExc_OSError, "no interface with this name");
7120+
PyErr_SetFromErrno(PyExc_OSError);
71217121
return NULL;
71227122
}
71237123

@@ -7144,6 +7144,7 @@ socket_if_indextoname(PyObject *self, PyObject *arg)
71447144
return NULL;
71457145
}
71467146

7147+
errno = ENXIO; // in case 'if_indextoname' does not set errno
71477148
char name[IF_NAMESIZE + 1];
71487149
if (if_indextoname(index, name) == NULL) {
71497150
PyErr_SetFromErrno(PyExc_OSError);

0 commit comments

Comments
 (0)