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.
This commit is contained in:
Max Bachmann 2023-03-09 22:09:12 +01:00 committed by GitHub
parent ca066bdbed
commit c6858d1e7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 633 additions and 100 deletions

View file

@ -163,6 +163,7 @@ static char *GetPythonImport (HINSTANCE hModule)
return NULL;
}
#ifdef Py_ENABLE_SHARED
/* Load python3.dll before loading any extension module that might refer
to it. That way, we can be sure that always the python3.dll corresponding
to this python DLL is loaded, not a python3.dll that might be on the path
@ -216,6 +217,7 @@ _Py_CheckPython3(void)
return hPython3 != NULL;
#undef MAXPATHLEN
}
#endif /* Py_ENABLE_SHARED */
dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
const char *shortname,
@ -224,7 +226,9 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
dl_funcptr p;
char funcname[258], *import_python;
#ifdef Py_ENABLE_SHARED
_Py_CheckPython3();
#endif /* Py_ENABLE_SHARED */
wchar_t *wpathname = PyUnicode_AsWideCharString(pathname, NULL);
if (wpathname == NULL)
@ -234,10 +238,12 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
{
HINSTANCE hDLL = NULL;
#ifdef MS_WINDOWS_DESKTOP
unsigned int old_mode;
/* Don't display a message box when Python can't load a DLL */
old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
#endif
/* bpo-36085: We use LoadLibraryEx with restricted search paths
to avoid DLL preloading attacks and enable use of the
@ -250,8 +256,10 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
Py_END_ALLOW_THREADS
PyMem_Free(wpathname);
#ifdef MS_WINDOWS_DESKTOP
/* restore old error mode settings */
SetErrorMode(old_mode);
#endif
if (hDLL==NULL){
PyObject *message;

View file

@ -8,7 +8,11 @@
#ifdef MS_WINDOWS
# include <malloc.h>
# include <windows.h>
# include <pathcch.h> // PathCchCombineEx
# if defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP)
# define PATHCCH_ALLOW_LONG_PATHS 0x01
# else
# include <pathcch.h> // PathCchCombineEx
# endif
extern int winerror_to_errno(int);
#endif
@ -77,7 +81,8 @@ _Py_device_encoding(int fd)
if (!valid)
Py_RETURN_NONE;
#if defined(MS_WINDOWS)
#ifdef MS_WINDOWS
#ifdef HAVE_WINDOWS_CONSOLE_IO
UINT cp;
if (fd == 0)
cp = GetConsoleCP();
@ -92,6 +97,9 @@ _Py_device_encoding(int fd)
}
return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
#else
Py_RETURN_NONE;
#endif /* HAVE_WINDOWS_CONSOLE_IO */
#else
if (_PyRuntime.preconfig.utf8_mode) {
_Py_DECLARE_STR(utf_8, "utf-8");
@ -1270,6 +1278,13 @@ _Py_stat(PyObject *path, struct stat *statbuf)
#endif
}
#ifdef MS_WINDOWS
// For some Windows API partitions, SetHandleInformation() is declared
// but none of the handle flags are defined.
#ifndef HANDLE_FLAG_INHERIT
#define HANDLE_FLAG_INHERIT 0x00000001
#endif
#endif
/* This function MUST be kept async-signal-safe on POSIX when raise=0. */
static int
@ -2096,6 +2111,72 @@ _Py_abspath(const wchar_t *path, wchar_t **abspath_p)
#endif
}
// The Windows Games API family implements the PathCch* APIs in the Xbox OS,
// but does not expose them yet. Load them dynamically until
// 1) they are officially exposed
// 2) we stop supporting older versions of the GDK which do not expose them
#if defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP)
HRESULT
PathCchSkipRoot(const wchar_t *path, const wchar_t **rootEnd)
{
static int initialized = 0;
typedef HRESULT(__stdcall *PPathCchSkipRoot) (PCWSTR pszPath,
PCWSTR *ppszRootEnd);
static PPathCchSkipRoot _PathCchSkipRoot;
if (initialized == 0) {
HMODULE pathapi = LoadLibraryExW(L"api-ms-win-core-path-l1-1-0.dll", NULL,
LOAD_LIBRARY_SEARCH_SYSTEM32);
if (pathapi) {
_PathCchSkipRoot = (PPathCchSkipRoot)GetProcAddress(
pathapi, "PathCchSkipRoot");
}
else {
_PathCchSkipRoot = NULL;
}
initialized = 1;
}
if (!_PathCchSkipRoot) {
return E_NOINTERFACE;
}
return _PathCchSkipRoot(path, rootEnd);
}
static HRESULT
PathCchCombineEx(wchar_t *buffer, size_t bufsize, const wchar_t *dirname,
const wchar_t *relfile, unsigned long flags)
{
static int initialized = 0;
typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut,
size_t cchPathOut,
PCWSTR pszPathIn,
PCWSTR pszMore,
unsigned long dwFlags);
static PPathCchCombineEx _PathCchCombineEx;
if (initialized == 0) {
HMODULE pathapi = LoadLibraryExW(L"api-ms-win-core-path-l1-1-0.dll", NULL,
LOAD_LIBRARY_SEARCH_SYSTEM32);
if (pathapi) {
_PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(
pathapi, "PathCchCombineEx");
}
else {
_PathCchCombineEx = NULL;
}
initialized = 1;
}
if (!_PathCchCombineEx) {
return E_NOINTERFACE;
}
return _PathCchCombineEx(buffer, bufsize, dirname, relfile, flags);
}
#endif /* defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP) */
// The caller must ensure "buffer" is big enough.
static int
@ -2491,12 +2572,12 @@ _Py_get_blocking(int fd)
success = GetNamedPipeHandleStateW(handle, &mode,
NULL, NULL, NULL, NULL, 0);
Py_END_ALLOW_THREADS
if (!success) {
PyErr_SetFromWindowsErr(0);
return -1;
}
return !(mode & PIPE_NOWAIT);
}

View file

@ -2289,7 +2289,7 @@ create_stdio(const PyConfig *config, PyObject* io,
raw = Py_NewRef(buf);
}
#ifdef MS_WINDOWS
#ifdef HAVE_WINDOWS_CONSOLE_IO
/* Windows console IO is always UTF-8 encoded */
PyTypeObject *winconsoleio_type = (PyTypeObject *)_PyImport_GetModuleAttr(
&_Py_ID(_io), &_Py_ID(_WindowsConsoleIO));

View file

@ -1490,6 +1490,9 @@ static PyStructSequence_Desc windows_version_desc = {
static PyObject *
_sys_getwindowsversion_from_kernel32()
{
#ifndef MS_WINDOWS_DESKTOP
return NULL;
#else
HANDLE hKernel32;
wchar_t kernel32_path[MAX_PATH];
LPVOID verblock;
@ -1523,6 +1526,7 @@ _sys_getwindowsversion_from_kernel32()
realBuild = HIWORD(ffi->dwProductVersionLS);
PyMem_RawFree(verblock);
return Py_BuildValue("(kkk)", realMajor, realMinor, realBuild);
#endif /* !MS_WINDOWS_DESKTOP */
}
/* Disable deprecation warnings about GetVersionEx as the result is