Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd6b4f4b

Browse files
[3.14]gh-100218: correctly seterrno whensocket.if_{nametoindex,indextoname} raiseOSError (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 raisean `OSError` with a `None` errno. Now, the errno from libc is propagated.(cherry picked from commit3ce2d57)Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent432432b commitd6b4f4b

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
deftestInvalidInterfaceIndexToName(self):
1177-
self.assertRaises(OSError,socket.if_indextoname,0)
1177+
withself.assertRaises(OSError)ascm:
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
deftestInvalidInterfaceNameToIndex(self):
1200+
withself.assertRaises(OSError)ascm:
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 longindex;
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
returnNULL;
72857285
}
72867286

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp