Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 5 additions & 5 deletions Modules/clinic/socketmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is mismatched, but I also don't see why we need HAVE_IF_NAMETOINDEX? I understand this was @mhsmith 's suggestion, can you please clarify why? From my understanding, it's not necessary, the NET_IFINDEX/_PyLong_NetIfindex_Converter definitions are only needed by if_indextoname.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used in the function body:

cpython/Modules/socketmodule.c

Lines 7418 to 7423 in e0afadb

static PyObject *
_socket_if_nametoindex_impl(PyObject *module, PyObject *oname)
/*[clinic end generated code: output=289a411614f30244 input=6125dc20683560cf]*/
{
#ifdef MS_WINDOWS
NET_IFINDEX index;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless that's something that's defined in the Windows headers?

@StanFromIreland StanFromIreland Sep 3, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless that's something that's defined in the Windows headers?

From what I can tell, it's defined by Iphlpapi.h on Windows (see docs for if_indextoname). As such, it's conditional on #ifdef MS_WINDOWS. Adding HAVE_IF_NAMETOINDEX makes no difference here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, reverted. Sorry about that.


/*[python input]
class NET_IFINDEX_converter(CConverter):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
6 changes: 6 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -748,9 +748,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

Expand Down
Loading