[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

@ -7277,10 +7277,10 @@ _socket_if_nametoindex_impl(PyObject *module, PyObject *oname)
unsigned long index;
#endif
errno = ENODEV; // in case 'if_nametoindex' does not set errno
index = if_nametoindex(PyBytes_AS_STRING(oname));
if (index == 0) {
/* if_nametoindex() doesn't set errno */
PyErr_SetString(PyExc_OSError, "no interface with this name");
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
@ -7300,6 +7300,7 @@ static PyObject *
_socket_if_indextoname_impl(PyObject *module, NET_IFINDEX index)
/*[clinic end generated code: output=e48bc324993052e0 input=c93f753d0cf6d7d1]*/
{
errno = ENXIO; // in case 'if_indextoname' does not set errno
char name[IF_NAMESIZE + 1];
if (if_indextoname(index, name) == NULL) {
PyErr_SetFromErrno(PyExc_OSError);