mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
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.
This commit is contained in:
parent
0c77e7c23b
commit
3ce2d57b2f
3 changed files with 14 additions and 4 deletions
|
|
@ -1176,7 +1176,10 @@ def testInterfaceNameIndex(self):
|
||||||
'socket.if_indextoname() not available.')
|
'socket.if_indextoname() not available.')
|
||||||
@support.skip_android_selinux('if_indextoname')
|
@support.skip_android_selinux('if_indextoname')
|
||||||
def testInvalidInterfaceIndexToName(self):
|
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(ValueError, socket.if_indextoname, -1)
|
||||||
self.assertRaises(OverflowError, socket.if_indextoname, 2**1000)
|
self.assertRaises(OverflowError, socket.if_indextoname, 2**1000)
|
||||||
self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF')
|
self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF')
|
||||||
|
|
@ -1196,8 +1199,11 @@ def testInvalidInterfaceIndexToName(self):
|
||||||
'socket.if_nametoindex() not available.')
|
'socket.if_nametoindex() not available.')
|
||||||
@support.skip_android_selinux('if_nametoindex')
|
@support.skip_android_selinux('if_nametoindex')
|
||||||
def testInvalidInterfaceNameToIndex(self):
|
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(TypeError, socket.if_nametoindex, 0)
|
||||||
self.assertRaises(OSError, socket.if_nametoindex, '_DEADBEEF')
|
|
||||||
|
|
||||||
@unittest.skipUnless(hasattr(sys, 'getrefcount'),
|
@unittest.skipUnless(hasattr(sys, 'getrefcount'),
|
||||||
'test needs sys.getrefcount()')
|
'test needs sys.getrefcount()')
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
Correctly set :attr:`~OSError.errno` when :func:`socket.if_nametoindex` or
|
||||||
|
:func:`socket.if_indextoname` raise an :exc:`OSError`. Patch by Bénédikt
|
||||||
|
Tran.
|
||||||
|
|
@ -7294,10 +7294,10 @@ _socket_if_nametoindex_impl(PyObject *module, PyObject *oname)
|
||||||
unsigned long index;
|
unsigned long index;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
errno = ENODEV; // in case 'if_nametoindex' does not set errno
|
||||||
index = if_nametoindex(PyBytes_AS_STRING(oname));
|
index = if_nametoindex(PyBytes_AS_STRING(oname));
|
||||||
if (index == 0) {
|
if (index == 0) {
|
||||||
/* if_nametoindex() doesn't set errno */
|
PyErr_SetFromErrno(PyExc_OSError);
|
||||||
PyErr_SetString(PyExc_OSError, "no interface with this name");
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -7317,6 +7317,7 @@ static PyObject *
|
||||||
_socket_if_indextoname_impl(PyObject *module, NET_IFINDEX index)
|
_socket_if_indextoname_impl(PyObject *module, NET_IFINDEX index)
|
||||||
/*[clinic end generated code: output=e48bc324993052e0 input=c93f753d0cf6d7d1]*/
|
/*[clinic end generated code: output=e48bc324993052e0 input=c93f753d0cf6d7d1]*/
|
||||||
{
|
{
|
||||||
|
errno = ENXIO; // in case 'if_indextoname' does not set errno
|
||||||
char name[IF_NAMESIZE + 1];
|
char name[IF_NAMESIZE + 1];
|
||||||
if (if_indextoname(index, name) == NULL) {
|
if (if_indextoname(index, name) == NULL) {
|
||||||
PyErr_SetFromErrno(PyExc_OSError);
|
PyErr_SetFromErrno(PyExc_OSError);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue