https://github.com/python/cpython/commit/cb54488523049a5698fc9d9a104402b739ffb4ae
commit: cb54488523049a5698fc9d9a104402b739ffb4ae
branch: main
author: A5rocks <[email protected]>
committer: StanFromIreland <[email protected]>
date: 2026-09-03T20:49:40+01:00
summary:

gh-156439: Gate `socket.if_{nametoindex, indextoname}` on respective OS 
capability (#156708)

Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Malcolm Smith <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-08-31-09-01-22.gh-issue-156439.j9WHCc.rst
M Modules/clinic/socketmodule.c.h
M Modules/socketmodule.c
M configure
M configure.ac
M pyconfig.h.in

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 00000000000000..056b54fd12aeda
--- /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 f89b91b9b99753..dd5a906dda1a38 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 70d3738b176cda..82899572f80255 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 a2ca40de07d54e..7e4844263a3491 100755
--- a/configure
+++ b/configure
@@ -20804,12 +20804,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
diff --git a/configure.ac b/configure.ac
index e1333230fefa68..8345c97349b738 100644
--- a/configure.ac
+++ b/configure.ac
@@ -5528,7 +5528,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 c2a36afe334763..e798a061ab6d09 100644
--- a/pyconfig.h.in
+++ b/pyconfig.h.in
@@ -751,9 +751,15 @@
 /* Define to 1 if you have the <iconv.h> 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
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to