diff --git a/Misc/NEWS.d/next/Library/2026-08-31-09-01-22.gh-issue-156439.j9WHCc.rst b/Misc/NEWS.d/next/Library/2026-08-31-09-01-22.gh-issue-156439.j9WHCc.rst new file mode 100644 index 000000000000000..75ed60cc6ce6ab1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-31-09-01-22.gh-issue-156439.j9WHCc.rst @@ -0,0 +1,3 @@ +:func:`socket.if_nametoindex` and :func:`socket.if_indextoname` are now +available on platforms that provide them but not ``if_nameindex()``, such +as Android before API level 24. diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index f89b91b9b997532..dd5a906dda1a384 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -2181,7 +2181,7 @@ _socket_if_nameindex(PyObject *module, PyObject *Py_UNUSED(ignored)) #endif /* (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)) */ -#if (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)) +#if (defined(HAVE_IF_NAMETOINDEX) || defined(MS_WINDOWS)) PyDoc_STRVAR(_socket_if_nametoindex__doc__, "if_nametoindex($module, oname, /)\n" @@ -2213,9 +2213,9 @@ _socket_if_nametoindex(PyObject *module, PyObject *arg) return return_value; } -#endif /* (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)) */ +#endif /* (defined(HAVE_IF_NAMETOINDEX) || defined(MS_WINDOWS)) */ -#if (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)) +#if (defined(HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS)) PyDoc_STRVAR(_socket_if_indextoname__doc__, "if_indextoname($module, if_index, /)\n" @@ -2244,7 +2244,7 @@ _socket_if_indextoname(PyObject *module, PyObject *arg) return return_value; } -#endif /* (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)) */ +#endif /* (defined(HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS)) */ #if defined(CMSG_LEN) @@ -2478,4 +2478,4 @@ _socket_CMSG_SPACE(PyObject *module, PyObject *arg) #ifndef _SOCKET_CMSG_SPACE_METHODDEF #define _SOCKET_CMSG_SPACE_METHODDEF #endif /* !defined(_SOCKET_CMSG_SPACE_METHODDEF) */ -/*[clinic end generated code: output=acc30d6fdeb54e90 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=aa9082b592c39fa5 input=a9049054013a1b77]*/ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 70d3738b176cda2..82899572f80255a 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -631,14 +631,14 @@ _PyLong_##NAME##_Converter(PyObject *obj, void *ptr) \ return 1; \ } -#if defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS) +#if defined(HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS) # ifdef MS_WINDOWS UNSIGNED_INT_CONVERTER(NetIfindex, NET_IFINDEX) # else # define _PyLong_NetIfindex_Converter _PyLong_UnsignedInt_Converter # define NET_IFINDEX unsigned int # endif -#endif // defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS) +#endif // defined(HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS) /*[python input] class NET_IFINDEX_converter(CConverter): @@ -7403,6 +7403,9 @@ _socket_if_nameindex_impl(PyObject *module) #endif } +#endif // defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS) + +#if defined(HAVE_IF_NAMETOINDEX) || defined(MS_WINDOWS) /*[clinic input] _socket.if_nametoindex @@ -7432,6 +7435,9 @@ _socket_if_nametoindex_impl(PyObject *module, PyObject *oname) return PyLong_FromUnsignedLong(index); } +#endif // defined(HAVE_IF_NAMETOINDEX) || defined(MS_WINDOWS) + +#if defined(HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS) /*[clinic input] @permit_long_summary @@ -7456,7 +7462,7 @@ _socket_if_indextoname_impl(PyObject *module, NET_IFINDEX index) return PyUnicode_DecodeFSDefault(name); } -#endif // defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS) +#endif // defined(HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS) #ifdef CMSG_LEN diff --git a/configure b/configure index 6b560fe6841b722..01cd53a585c961e 100755 --- a/configure +++ b/configure @@ -20743,12 +20743,24 @@ if test "x$ac_cv_func_grantpt" = xyes then : printf "%s\n" "#define HAVE_GRANTPT 1" >>confdefs.h +fi +ac_fn_c_check_func "$LINENO" "if_indextoname" "ac_cv_func_if_indextoname" +if test "x$ac_cv_func_if_indextoname" = xyes +then : + printf "%s\n" "#define HAVE_IF_INDEXTONAME 1" >>confdefs.h + fi ac_fn_c_check_func "$LINENO" "if_nameindex" "ac_cv_func_if_nameindex" if test "x$ac_cv_func_if_nameindex" = xyes then : printf "%s\n" "#define HAVE_IF_NAMEINDEX 1" >>confdefs.h +fi +ac_fn_c_check_func "$LINENO" "if_nametoindex" "ac_cv_func_if_nametoindex" +if test "x$ac_cv_func_if_nametoindex" = xyes +then : + printf "%s\n" "#define HAVE_IF_NAMETOINDEX 1" >>confdefs.h + fi ac_fn_c_check_func "$LINENO" "initgroups" "ac_cv_func_initgroups" if test "x$ac_cv_func_initgroups" = xyes @@ -40179,4 +40191,3 @@ if test "$ac_cv_header_stdatomic_h" != "yes"; then { printf "%s\n" "$as_me:${as_lineno-$LINENO}: Your compiler or platform does have a working C11 stdatomic.h. A future version of Python may require stdatomic.h." >&5 printf "%s\n" "$as_me: Your compiler or platform does have a working C11 stdatomic.h. A future version of Python may require stdatomic.h." >&6;} fi - diff --git a/configure.ac b/configure.ac index 476f13c82bbb2b9..5b29e62079319eb 100644 --- a/configure.ac +++ b/configure.ac @@ -5507,7 +5507,8 @@ AC_CHECK_FUNCS([ \ getgrnam_r getgrouplist gethostname getitimer getloadavg getlogin getlogin_r \ getpeername getpgid getpid getppid getpriority _getpty \ getpwent getpwnam_r getpwuid getpwuid_r getresgid getresuid getrusage getsid getspent \ - getspnam gettid getuid getwd grantpt if_nameindex initgroups kill killpg lchown linkat \ + getspnam gettid getuid getwd grantpt if_indextoname if_nameindex \ + if_nametoindex initgroups kill killpg lchown linkat \ lockf lstat lutimes madvise mbrtowc memrchr mkdirat mkfifo mkfifoat \ mknod mknodat mktime mmap mremap nice openat opendir pathconf pause \ pidfd_open pidfd_getfd pidfd_send_signal pipe \ diff --git a/pyconfig.h.in b/pyconfig.h.in index 64b5c52790b4581..6c110a553a6100d 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -748,9 +748,15 @@ /* Define to 1 if you have the header file. */ #undef HAVE_ICONV_H +/* Define to 1 if you have the 'if_indextoname' function. */ +#undef HAVE_IF_INDEXTONAME + /* Define to 1 if you have the 'if_nameindex' function. */ #undef HAVE_IF_NAMEINDEX +/* Define to 1 if you have the 'if_nametoindex' function. */ +#undef HAVE_IF_NAMETOINDEX + /* Define if you have the 'inet_aton' function. */ #undef HAVE_INET_ATON @@ -2279,4 +2285,3 @@ #endif #endif /*Py_PYCONFIG_H*/ -