summaryrefslogtreecommitdiffstats
path: root/Modules/socketmodule.c
diff options
context:
space:
mode:
authorMax Bachmann <oss@maxbachmann.de>2023-03-09 21:09:12 (GMT)
committerGitHub <noreply@github.com>2023-03-09 21:09:12 (GMT)
commitc6858d1e7f4cd3184d5ddea4025ad5dfc7596546 (patch)
tree6c5261397eca871567bb29b31cfe02c92f6d3b58 /Modules/socketmodule.c
parentca066bdbed85094a9c4d9930823ce3587807db48 (diff)
downloadcpython-c6858d1e7f4cd3184d5ddea4025ad5dfc7596546.zip
cpython-c6858d1e7f4cd3184d5ddea4025ad5dfc7596546.tar.gz
cpython-c6858d1e7f4cd3184d5ddea4025ad5dfc7596546.tar.bz2
gh-102255: Improve build support for Windows API partitions (GH-102256)
Add `MS_WINDOWS_DESKTOP`, `MS_WINDOWS_APPS`, `MS_WINDOWS_SYSTEM` and `MS_WINDOWS_GAMES` preprocessor definitions to allow switching off functionality missing from particular API partitions ("partitions" are used in Windows to identify overlapping subsets of APIs). CPython only officially supports `MS_WINDOWS_DESKTOP` and `MS_WINDOWS_SYSTEM` (APPS is included by normal desktop builds, but APPS without DESKTOP is not covered). Other configurations are a convenience for people building their own runtimes. `MS_WINDOWS_GAMES` is for the Xbox subset of the Windows API, which is also available on client OS, but is restricted compared to `MS_WINDOWS_DESKTOP`. These restrictions may change over time, as they relate to the build headers rather than the OS support, and so we assume that Xbox builds will use the latest available version of the GDK.
Diffstat (limited to 'Modules/socketmodule.c')
-rw-r--r--Modules/socketmodule.c60
1 files changed, 39 insertions, 21 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 43a0cc0..b792775 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -270,7 +270,7 @@ shutdown(how) -- shut down traffic in one or both directions\n\
# include <fcntl.h>
-#else
+#else /* MS_WINDOWS */
/* MS_WINDOWS includes */
# ifdef HAVE_FCNTL_H
@@ -281,7 +281,6 @@ shutdown(how) -- shut down traffic in one or both directions\n\
# include <Rpc.h>
/* Macros based on the IPPROTO enum, see: https://bugs.python.org/issue29515 */
-#ifdef MS_WINDOWS
#define IPPROTO_ICMP IPPROTO_ICMP
#define IPPROTO_IGMP IPPROTO_IGMP
#define IPPROTO_GGP IPPROTO_GGP
@@ -312,7 +311,6 @@ shutdown(how) -- shut down traffic in one or both directions\n\
#define IPPROTO_PGM IPPROTO_PGM // WinSock2 only
#define IPPROTO_L2TP IPPROTO_L2TP // WinSock2 only
#define IPPROTO_SCTP IPPROTO_SCTP // WinSock2 only
-#endif /* MS_WINDOWS */
/* Provides the IsWindows7SP1OrGreater() function */
#include <versionhelpers.h>
@@ -348,13 +346,18 @@ remove_unusable_flags(PyObject *m)
{
PyObject *dict;
OSVERSIONINFOEX info;
- DWORDLONG dwlConditionMask;
dict = PyModule_GetDict(m);
if (dict == NULL) {
return -1;
}
-
+#ifndef MS_WINDOWS_DESKTOP
+ info.dwOSVersionInfoSize = sizeof(info);
+ if (!GetVersionExW((OSVERSIONINFOW*) &info)) {
+ PyErr_SetFromWindowsErr(0);
+ return -1;
+ }
+#else
/* set to Windows 10, except BuildNumber. */
memset(&info, 0, sizeof(info));
info.dwOSVersionInfoSize = sizeof(info);
@@ -362,19 +365,30 @@ remove_unusable_flags(PyObject *m)
info.dwMinorVersion = 0;
/* set Condition Mask */
- dwlConditionMask = 0;
+ DWORDLONG dwlConditionMask = 0;
VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
VER_SET_CONDITION(dwlConditionMask, VER_BUILDNUMBER, VER_GREATER_EQUAL);
+#endif
for (int i=0; i<sizeof(win_runtime_flags)/sizeof(FlagRuntimeInfo); i++) {
+#ifdef MS_WINDOWS_DESKTOP
info.dwBuildNumber = win_runtime_flags[i].build_number;
/* greater than or equal to the specified version?
Compatibility Mode will not cheat VerifyVersionInfo(...) */
- if (VerifyVersionInfo(
- &info,
- VER_MAJORVERSION|VER_MINORVERSION|VER_BUILDNUMBER,
- dwlConditionMask)) {
+ BOOL isSupported = VerifyVersionInfo(
+ &info,
+ VER_MAJORVERSION|VER_MINORVERSION|VER_BUILDNUMBER,
+ dwlConditionMask);
+#else
+ /* note in this case 'info' is the actual OS version, whereas above
+ it is the version to compare against. */
+ BOOL isSupported = info.dwMajorVersion > 10 ||
+ (info.dwMajorVersion == 10 && info.dwMinorVersion > 0) ||
+ (info.dwMajorVersion == 10 && info.dwMinorVersion == 0 &&
+ info.dwBuildNumber >= win_runtime_flags[i].build_number);
+#endif
+ if (isSupported) {
break;
}
else {
@@ -497,14 +511,14 @@ remove_unusable_flags(PyObject *m)
#endif
#endif
-#ifdef MS_WINDOWS
+#ifdef MS_WINDOWS_DESKTOP
#define sockaddr_rc SOCKADDR_BTH_REDEF
#define USE_BLUETOOTH 1
#define AF_BLUETOOTH AF_BTH
#define BTPROTO_RFCOMM BTHPROTO_RFCOMM
#define _BT_RC_MEMB(sa, memb) ((sa)->memb)
-#endif
+#endif /* MS_WINDOWS_DESKTOP */
/* Convert "sock_addr_t *" to "struct sockaddr *". */
#define SAS2SA(x) (&((x)->sa))
@@ -2869,11 +2883,16 @@ sock_accept(PySocketSockObject *s, PyObject *Py_UNUSED(ignored))
newfd = ctx.result;
#ifdef MS_WINDOWS
+#if defined(MS_WINDOWS_APP) || defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
+#ifndef HANDLE_FLAG_INHERIT
+#define HANDLE_FLAG_INHERIT 0x00000001
+#endif
if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
PyErr_SetFromWindowsErr(0);
SOCKETCLOSE(newfd);
goto finally;
}
+#endif
#else
#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
@@ -5434,11 +5453,6 @@ sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto,
proto = 0;
}
#ifdef MS_WINDOWS
- /* Windows implementation */
-#ifndef WSA_FLAG_NO_HANDLE_INHERIT
-#define WSA_FLAG_NO_HANDLE_INHERIT 0x80
-#endif
-
Py_BEGIN_ALLOW_THREADS
fd = WSASocketW(family, type, proto,
NULL, 0,
@@ -6150,8 +6164,9 @@ socket_dup(PyObject *self, PyObject *fdobj)
#endif
fd = PyLong_AsSocket_t(fdobj);
- if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
+ if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) {
return NULL;
+ }
#ifdef MS_WINDOWS
if (WSADuplicateSocketW(fd, GetCurrentProcessId(), &info))
@@ -6160,8 +6175,9 @@ socket_dup(PyObject *self, PyObject *fdobj)
newfd = WSASocketW(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
FROM_PROTOCOL_INFO,
&info, 0, WSA_FLAG_OVERLAPPED);
- if (newfd == INVALID_SOCKET)
+ if (newfd == INVALID_SOCKET) {
return set_error();
+ }
if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
closesocket(newfd);
@@ -6171,13 +6187,15 @@ socket_dup(PyObject *self, PyObject *fdobj)
#else
/* On UNIX, dup can be used to duplicate the file descriptor of a socket */
newfd = _Py_dup(fd);
- if (newfd == INVALID_SOCKET)
+ if (newfd == INVALID_SOCKET) {
return NULL;
+ }
#endif
newfdobj = PyLong_FromSocket_t(newfd);
- if (newfdobj == NULL)
+ if (newfdobj == NULL) {
SOCKETCLOSE(newfd);
+ }
return newfdobj;
}