Skip to content

Commit

Permalink
gh-102255: Improve build support for Windows API partitions (GH-102256)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
maxbachmann authored Mar 9, 2023
1 parent ca066bd commit c6858d1
Show file tree
Hide file tree
Showing 36 changed files with 633 additions and 100 deletions.
8 changes: 8 additions & 0 deletions Include/internal/pycore_fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ extern int _Py_add_relfile(wchar_t *dirname,
extern size_t _Py_find_basename(const wchar_t *filename);
PyAPI_FUNC(wchar_t *) _Py_normpath(wchar_t *path, Py_ssize_t size);

// The Windows Games API family does not provide these functions
// so provide our own implementations. Remove them in case they get added
// to the Games API family
#if defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP)
#include <winerror.h>

extern HRESULT PathCchSkipRoot(const wchar_t *pszPath, const wchar_t **ppszRootEnd);
#endif /* defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP) */

// Macros to protect CRT calls against instant termination when passed an
// invalid parameter (bpo-23524). IPH stands for Invalid Parameter Handler.
Expand Down
8 changes: 5 additions & 3 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -3084,11 +3084,13 @@ def test_device_encoding(self):
class PidTests(unittest.TestCase):
@unittest.skipUnless(hasattr(os, 'getppid'), "test needs os.getppid")
def test_getppid(self):
p = subprocess.Popen([sys.executable, '-c',
p = subprocess.Popen([sys._base_executable, '-c',
'import os; print(os.getppid())'],
stdout=subprocess.PIPE)
stdout, _ = p.communicate()
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, error = p.communicate()
# We are the parent of our subprocess
self.assertEqual(error, b'')
self.assertEqual(int(stdout), os.getpid())

def check_waitpid(self, code, exitcode, callback=None):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve build support for the Xbox. Patch by Max Bachmann.
6 changes: 3 additions & 3 deletions Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
_PyIO_State *state = get_io_state(module);
{
PyObject *RawIO_class = (PyObject *)state->PyFileIO_Type;
#ifdef MS_WINDOWS
#ifdef HAVE_WINDOWS_CONSOLE_IO
const PyConfig *config = _Py_GetConfig();
if (!config->legacy_windows_stdio && _PyIO_get_console_type(path_or_fd) != '\0') {
RawIO_class = (PyObject *)&PyWindowsConsoleIO_Type;
Expand Down Expand Up @@ -660,7 +660,7 @@ static PyTypeObject* static_types[] = {

// PyRawIOBase_Type(PyIOBase_Type) subclasses
&_PyBytesIOBuffer_Type,
#ifdef MS_WINDOWS
#ifdef HAVE_WINDOWS_CONSOLE_IO
&PyWindowsConsoleIO_Type,
#endif
};
Expand Down Expand Up @@ -718,7 +718,7 @@ PyInit__io(void)
}

// Set type base classes
#ifdef MS_WINDOWS
#ifdef HAVE_WINDOWS_CONSOLE_IO
PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type;
#endif

Expand Down
6 changes: 3 additions & 3 deletions Modules/_io/_iomodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ extern PyType_Spec fileio_spec;
extern PyType_Spec stringio_spec;
extern PyType_Spec textiowrapper_spec;

#ifdef MS_WINDOWS
#ifdef HAVE_WINDOWS_CONSOLE_IO
extern PyTypeObject PyWindowsConsoleIO_Type;
#endif /* MS_WINDOWS */
#endif /* HAVE_WINDOWS_CONSOLE_IO */

/* These functions are used as METH_NOARGS methods, are normally called
* with args=NULL, and return a new reference.
Expand Down Expand Up @@ -178,7 +178,7 @@ find_io_state_by_def(PyTypeObject *type)

extern _PyIO_State *_PyIO_get_module_state(void);

#ifdef MS_WINDOWS
#ifdef HAVE_WINDOWS_CONSOLE_IO
extern char _PyIO_get_console_type(PyObject *);
#endif

Expand Down
42 changes: 21 additions & 21 deletions Modules/_io/clinic/winconsoleio.c.h

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

2 changes: 2 additions & 0 deletions Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
#ifdef MS_WINDOWS
/* can simulate truncate with Win32 API functions; see file_truncate */
#define HAVE_FTRUNCATE
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif

Expand Down
6 changes: 4 additions & 2 deletions Modules/_io/winconsoleio.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH
#include "pycore_object.h" // _PyObject_GC_UNTRACK()

#ifdef MS_WINDOWS
#ifdef HAVE_WINDOWS_CONSOLE_IO

#include "structmember.h" // PyMemberDef
#ifdef HAVE_SYS_TYPES_H
Expand All @@ -22,7 +22,9 @@
#endif
#include <stddef.h> /* For offsetof */

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <fcntl.h>

Expand Down Expand Up @@ -1177,4 +1179,4 @@ PyTypeObject PyWindowsConsoleIO_Type = {
0, /* tp_finalize */
};

#endif /* MS_WINDOWS */
#endif /* HAVE_WINDOWS_CONSOLE_IO */
8 changes: 5 additions & 3 deletions Modules/_localemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ This software comes with no warranty. Use at your own risk.
#endif

#if defined(MS_WINDOWS)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif

Expand Down Expand Up @@ -457,12 +459,12 @@ _locale__getdefaultlocale_impl(PyObject *module)

PyOS_snprintf(encoding, sizeof(encoding), "cp%u", GetACP());

if (GetLocaleInfo(LOCALE_USER_DEFAULT,
if (GetLocaleInfoA(LOCALE_USER_DEFAULT,
LOCALE_SISO639LANGNAME,
locale, sizeof(locale))) {
Py_ssize_t i = strlen(locale);
locale[i++] = '_';
if (GetLocaleInfo(LOCALE_USER_DEFAULT,
if (GetLocaleInfoA(LOCALE_USER_DEFAULT,
LOCALE_SISO3166CTRYNAME,
locale+i, (int)(sizeof(locale)-i)))
return Py_BuildValue("ss", locale, encoding);
Expand All @@ -474,7 +476,7 @@ _locale__getdefaultlocale_impl(PyObject *module)

locale[0] = '0';
locale[1] = 'x';
if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE,
if (GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE,
locale+2, sizeof(locale)-2)) {
return Py_BuildValue("ss", locale, encoding);
}
Expand Down
4 changes: 3 additions & 1 deletion Modules/_multiprocessing/multiprocessing.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*/

#ifdef MS_WINDOWS
# define WIN32_LEAN_AND_MEAN
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# include <winsock2.h>
# include <process.h> /* getpid() */
Expand Down
6 changes: 6 additions & 0 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ enum {
#define FLOAT FLOAT_
#define INT INT_
#define LONG LONG_

/* This can already be defined on Windows to set the character set
the Windows header files treat as default */
#ifdef UNICODE
#undef UNICODE
#endif
#endif

/* Pickle opcodes. These must be kept updated with pickle.py.
Expand Down
6 changes: 5 additions & 1 deletion Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
# include <process.h> // getpid()
#endif

#ifdef MS_WINDOWS
# include <windows.h>
#endif

/* Period parameters -- These are all magic. Don't change. */
#define N 624
#define M 397
Expand Down Expand Up @@ -259,7 +263,7 @@ random_seed_time_pid(RandomObject *self)
key[0] = (uint32_t)(now & 0xffffffffU);
key[1] = (uint32_t)(now >> 32);

#ifdef MS_WINDOWS_NON_DESKTOP
#if defined(MS_WINDOWS) && !defined(MS_WINDOWS_DESKTOP) && !defined(MS_WINDOWS_SYSTEM)
key[2] = (uint32_t)GetCurrentProcessId();
#elif defined(HAVE_GETPID)
key[2] = (uint32_t)getpid();
Expand Down
4 changes: 4 additions & 0 deletions Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
/* Include symbols from _socket module */
#include "socketmodule.h"

#ifdef MS_WINDOWS
# include <wincrypt.h>
#endif

#include "_ssl.h"

/* Redefined below for Windows debug builds after important #includes */
Expand Down
13 changes: 13 additions & 0 deletions Modules/_winapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@
#include "structmember.h" // PyMemberDef


#ifndef WINDOWS_LEAN_AND_MEAN
#define WINDOWS_LEAN_AND_MEAN
#endif
#include "windows.h"
#include <winioctl.h>
#include <crtdbg.h>
#include "winreparse.h"

Expand All @@ -63,6 +66,14 @@

#define T_HANDLE T_POINTER

// winbase.h limits the STARTF_* flags to the desktop API as of 10.0.19041.
#ifndef STARTF_USESHOWWINDOW
#define STARTF_USESHOWWINDOW 0x00000001
#endif
#ifndef STARTF_USESTDHANDLES
#define STARTF_USESTDHANDLES 0x00000100
#endif

typedef struct {
PyTypeObject *overlapped_type;
} WinApiState;
Expand Down Expand Up @@ -1201,8 +1212,10 @@ _winapi_ExitProcess_impl(PyObject *module, UINT ExitCode)
/*[clinic end generated code: output=a387deb651175301 input=4f05466a9406c558]*/
{
#if defined(Py_DEBUG)
#ifdef MS_WINDOWS_DESKTOP
SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT|
SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX);
#endif
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
#endif

Expand Down
Loading

0 comments on commit c6858d1

Please sign in to comment.