[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 3ce2d57b2f)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2025-11-09 14:11:43 +01:00 committed by GitHub
parent 432432bfc8
commit d6b4f4b10f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 4 deletions

View file

@ -1174,7 +1174,10 @@ def testInterfaceNameIndex(self):
'socket.if_indextoname() not available.')
@support.skip_android_selinux('if_indextoname')
def testInvalidInterfaceIndexToName(self):
self.assertRaises(OSError, socket.if_indextoname, 0)
with self.assertRaises(OSError) as cm:
socket.if_indextoname(0)
self.assertIsNotNone(cm.exception.errno)
self.assertRaises(ValueError, socket.if_indextoname, -1)
self.assertRaises(OverflowError, socket.if_indextoname, 2**1000)
self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF')
@ -1194,8 +1197,11 @@ def testInvalidInterfaceIndexToName(self):
'socket.if_nametoindex() not available.')
@support.skip_android_selinux('if_nametoindex')
def testInvalidInterfaceNameToIndex(self):
with self.assertRaises(OSError) as cm:
socket.if_nametoindex("_DEADBEEF")
self.assertIsNotNone(cm.exception.errno)
self.assertRaises(TypeError, socket.if_nametoindex, 0)
self.assertRaises(OSError, socket.if_nametoindex, '_DEADBEEF')
@unittest.skipUnless(hasattr(sys, 'getrefcount'),
'test needs sys.getrefcount()')