Skip to content

Commit 1e7b24a

Browse files
Alexpuxlazka
authored andcommitted
Detect winsock2 and setup _socket module on MINGW
Co-authored-by: Алексей <alexey.pawlow@gmail.com>
1 parent c9ed19b commit 1e7b24a

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

Misc/config_mingw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ ac_cv_func_alarm=ignore
1010
# files to ignore
1111
ac_cv_file__dev_ptmx=ignore #NOTE: under MSYS environment device exist
1212
ac_cv_file__dev_ptc=no
13+
14+
# force detection of winsock2 functionality - require wxp or newer
15+
ac_cv_func_getpeername=yes

Modules/socketmodule.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ shutdown(how) -- shut down traffic in one or both directions\n\
274274
# endif
275275

276276
/* Macros based on the IPPROTO enum, see: https://bugs.python.org/issue29515 */
277-
#ifdef MS_WINDOWS
277+
#ifdef _MSC_VER
278278
#define IPPROTO_ICMP IPPROTO_ICMP
279279
#define IPPROTO_IGMP IPPROTO_IGMP
280280
#define IPPROTO_GGP IPPROTO_GGP
@@ -305,7 +305,7 @@ shutdown(how) -- shut down traffic in one or both directions\n\
305305
#define IPPROTO_PGM IPPROTO_PGM // WinSock2 only
306306
#define IPPROTO_L2TP IPPROTO_L2TP // WinSock2 only
307307
#define IPPROTO_SCTP IPPROTO_SCTP // WinSock2 only
308-
#endif /* MS_WINDOWS */
308+
#endif /* _MSC_VER */
309309

310310
/* Provides the IsWindows7SP1OrGreater() function */
311311
#include <versionhelpers.h>
@@ -404,6 +404,10 @@ remove_unusable_flags(PyObject *m)
404404
/* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
405405
* EAI_* constants are defined in (the already included) ws2tcpip.h.
406406
*/
407+
#elif defined(__MINGW32__)
408+
/* Do not include addrinfo.h as minimum supported version is
409+
* _WIN32_WINNT >= WindowsXP(0x0501)
410+
*/
407411
#else
408412
# include "addrinfo.h"
409413
#endif

configure.ac

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5325,18 +5325,33 @@ if test $ac_cv_header_time_altzone = yes; then
53255325
AC_DEFINE(HAVE_ALTZONE, 1, [Define this if your time.h defines altzone.])
53265326
fi
53275327

5328+
AC_CHECK_HEADERS([ws2tcpip.h])
53285329
AC_CACHE_CHECK([for addrinfo], [ac_cv_struct_addrinfo],
5329-
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <netdb.h>]], [[struct addrinfo a]])],
5330+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
5331+
#ifdef HAVE_WS2TCPIP_H
5332+
# include <ws2tcpip.h>
5333+
#else
5334+
# include <netdb.h>
5335+
#endif]],
5336+
[[struct addrinfo a]])],
53305337
[ac_cv_struct_addrinfo=yes],
53315338
[ac_cv_struct_addrinfo=no]))
53325339
if test $ac_cv_struct_addrinfo = yes; then
5333-
AC_DEFINE(HAVE_ADDRINFO, 1, [struct addrinfo (netdb.h)])
5340+
AC_DEFINE(HAVE_ADDRINFO, 1, [struct addrinfo])
53345341
fi
53355342

53365343
AC_CACHE_CHECK([for sockaddr_storage], [ac_cv_struct_sockaddr_storage],
53375344
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
5338-
# include <sys/types.h>
5339-
# include <sys/socket.h>]], [[struct sockaddr_storage s]])],
5345+
#ifdef HAVE_WS2TCPIP_H
5346+
#include <ws2tcpip.h>
5347+
#endif
5348+
#ifdef HAVE_SYS_TYPES_H
5349+
#include <sys/types.h>
5350+
#endif
5351+
#ifdef HAVE_SYS_SOCKET_H
5352+
#include <sys/socket.h>
5353+
#endif]],
5354+
[[struct sockaddr_storage s]])],
53405355
[ac_cv_struct_sockaddr_storage=yes],
53415356
[ac_cv_struct_sockaddr_storage=no]))
53425357
if test $ac_cv_struct_sockaddr_storage = yes; then
@@ -6417,7 +6432,10 @@ fi
64176432

64186433
AC_CHECK_TYPE(socklen_t,,
64196434
AC_DEFINE(socklen_t,int,
6420-
[Define to `int' if <sys/socket.h> does not define.]),[
6435+
[Define to `int' if <sys/socket.h> or <ws2tcpip.h> does not define.]),[
6436+
#ifdef HAVE_WS2TCPIP_H
6437+
#include <ws2tcpip.h>
6438+
#endif
64216439
#ifdef HAVE_SYS_TYPES_H
64226440
#include <sys/types.h>
64236441
#endif

pyconfig.h.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
/* Define to 1 if you have the `acosh' function. */
6161
#undef HAVE_ACOSH
6262

63-
/* struct addrinfo (netdb.h) */
63+
/* struct addrinfo */
6464
#undef HAVE_ADDRINFO
6565

6666
/* Define to 1 if you have the `alarm' function. */
@@ -1839,7 +1839,7 @@
18391839
/* Define to `unsigned int' if <sys/types.h> does not define. */
18401840
#undef size_t
18411841

1842-
/* Define to `int' if <sys/socket.h> does not define. */
1842+
/* Define to `int' if <sys/socket.h> or <ws2tcpip.h> does not define. */
18431843
#undef socklen_t
18441844

18451845
/* Define to `int' if <sys/types.h> doesn't define. */

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,9 @@ def detect_simple_extensions(self):
970970
# grp(3)
971971
self.addext(Extension('grp', ['grpmodule.c']))
972972

973-
self.addext(Extension('_socket', ['socketmodule.c']))
973+
self.addext(Extension(
974+
'_socket', ['socketmodule.c'],
975+
libraries=(['ws2_32', 'iphlpapi'] if MS_WINDOWS else None)))
974976
self.addext(Extension('spwd', ['spwdmodule.c']))
975977

976978
# select(2); not on ancient System V

0 commit comments

Comments
 (0)