Skip to content

Commit d6b4f4b

Browse files
[3.14] gh-100218: correctly set errno when socket.if_{nametoindex,indextoname} raise OSError (GH-140905) (#141284)
gh-100218: correctly set `errno` when `socket.if_{nametoindex,indextoname}` raise `OSError` (GH-140905) 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) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 432432b commit d6b4f4b

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
@@ -1174,7 +1174,10 @@ def testInterfaceNameIndex(self):
11741174
'socket.if_indextoname() not available.')
11751175
@support.skip_android_selinux('if_indextoname')
11761176
def testInvalidInterfaceIndexToName(self):
1177-
self.assertRaises(OSError, socket.if_indextoname, 0)
1177+
with self.assertRaises(OSError) as cm:
1178+
socket.if_indextoname(0)
1179+
self.assertIsNotNone(cm.exception.errno)
1180+
11781181
self.assertRaises(ValueError, socket.if_indextoname, -1)
11791182
self.assertRaises(OverflowError, socket.if_indextoname, 2**1000)
11801183
self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF')
@@ -1194,8 +1197,11 @@ def testInvalidInterfaceIndexToName(self):
11941197
'socket.if_nametoindex() not available.')
11951198
@support.skip_android_selinux('if_nametoindex')
11961199
def testInvalidInterfaceNameToIndex(self):
1200+
with self.assertRaises(OSError) as cm:
1201+
socket.if_nametoindex("_DEADBEEF")
1202+
self.assertIsNotNone(cm.exception.errno)
1203+
11971204
self.assertRaises(TypeError, socket.if_nametoindex, 0)
1198-
self.assertRaises(OSError, socket.if_nametoindex, '_DEADBEEF')
11991205

12001206
@unittest.skipUnless(hasattr(sys, 'getrefcount'),
12011207
'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
@@ -7277,10 +7277,10 @@ _socket_if_nametoindex_impl(PyObject *module, PyObject *oname)
72777277
unsigned long index;
72787278
#endif
72797279

7280+
errno = ENODEV; // in case 'if_nametoindex' does not set errno
72807281
index = if_nametoindex(PyBytes_AS_STRING(oname));
72817282
if (index == 0) {
7282-
/* if_nametoindex() doesn't set errno */
7283-
PyErr_SetString(PyExc_OSError, "no interface with this name");
7283+
PyErr_SetFromErrno(PyExc_OSError);
72847284
return NULL;
72857285
}
72867286

@@ -7300,6 +7300,7 @@ static PyObject *
73007300
_socket_if_indextoname_impl(PyObject *module, NET_IFINDEX index)
73017301
/*[clinic end generated code: output=e48bc324993052e0 input=c93f753d0cf6d7d1]*/
73027302
{
7303+
errno = ENXIO; // in case 'if_indextoname' does not set errno
73037304
char name[IF_NAMESIZE + 1];
73047305
if (if_indextoname(index, name) == NULL) {
73057306
PyErr_SetFromErrno(PyExc_OSError);

0 commit comments

Comments
 (0)