gh-102336: Remove code specifically for handling Windows 7 (GH-102337)

This commit is contained in:
Max Bachmann 2023-03-01 01:31:21 +01:00 committed by GitHub
parent 360ef843d8
commit 938e36f824
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 159 deletions

View file

@ -0,0 +1 @@
Cleanup Windows 7 specific special handling. Patch by Max Bachmann.

View file

@ -63,23 +63,6 @@
#define T_HANDLE T_POINTER #define T_HANDLE T_POINTER
/* Grab CancelIoEx dynamically from kernel32 */
static int has_CancelIoEx = -1;
static BOOL (CALLBACK *Py_CancelIoEx)(HANDLE, LPOVERLAPPED);
static int
check_CancelIoEx()
{
if (has_CancelIoEx == -1)
{
HINSTANCE hKernel32 = GetModuleHandle("KERNEL32");
* (FARPROC *) &Py_CancelIoEx = GetProcAddress(hKernel32,
"CancelIoEx");
has_CancelIoEx = (Py_CancelIoEx != NULL);
}
return has_CancelIoEx;
}
typedef struct { typedef struct {
PyTypeObject *overlapped_type; PyTypeObject *overlapped_type;
} WinApiState; } WinApiState;
@ -134,8 +117,7 @@ overlapped_dealloc(OverlappedObject *self)
PyObject_GC_UnTrack(self); PyObject_GC_UnTrack(self);
if (self->pending) { if (self->pending) {
if (check_CancelIoEx() && if (CancelIoEx(self->handle, &self->overlapped) &&
Py_CancelIoEx(self->handle, &self->overlapped) &&
GetOverlappedResult(self->handle, &self->overlapped, &bytes, TRUE)) GetOverlappedResult(self->handle, &self->overlapped, &bytes, TRUE))
{ {
/* The operation is no longer pending -- nothing to do. */ /* The operation is no longer pending -- nothing to do. */
@ -306,10 +288,7 @@ _winapi_Overlapped_cancel_impl(OverlappedObject *self)
if (self->pending) { if (self->pending) {
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
if (check_CancelIoEx()) CancelIoEx(self->handle, &self->overlapped);
res = Py_CancelIoEx(self->handle, &self->overlapped);
else
res = CancelIo(self->handle);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
} }
@ -655,8 +634,10 @@ _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
cleanup: cleanup:
ret = GetLastError(); ret = GetLastError();
CloseHandle(token); if (token != NULL)
CloseHandle(junction); CloseHandle(token);
if (junction != NULL)
CloseHandle(junction);
PyMem_RawFree(rdb); PyMem_RawFree(rdb);
if (ret != 0) if (ret != 0)

View file

@ -227,12 +227,11 @@ getpath_isxfile(PyObject *Py_UNUSED(self), PyObject *args)
path = PyUnicode_AsWideCharString(pathobj, &cchPath); path = PyUnicode_AsWideCharString(pathobj, &cchPath);
if (path) { if (path) {
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
const wchar_t *ext;
DWORD attr = GetFileAttributesW(path); DWORD attr = GetFileAttributesW(path);
r = (attr != INVALID_FILE_ATTRIBUTES) && r = (attr != INVALID_FILE_ATTRIBUTES) &&
!(attr & FILE_ATTRIBUTE_DIRECTORY) && !(attr & FILE_ATTRIBUTE_DIRECTORY) &&
SUCCEEDED(PathCchFindExtension(path, cchPath + 1, &ext)) && (cchPath >= 4) &&
(CompareStringOrdinal(ext, -1, L".exe", -1, 1 /* ignore case */) == CSTR_EQUAL) (CompareStringOrdinal(path + cchPath - 4, -1, L".exe", -1, 1 /* ignore case */) == CSTR_EQUAL)
? Py_True : Py_False; ? Py_True : Py_False;
#else #else
struct stat st; struct stat st;

View file

@ -502,7 +502,7 @@ mmap_resize_method(mmap_object *self,
CloseHandle(self->map_handle); CloseHandle(self->map_handle);
/* if the file mapping still exists, it cannot be resized. */ /* if the file mapping still exists, it cannot be resized. */
if (self->tagname) { if (self->tagname) {
self->map_handle = OpenFileMapping(FILE_MAP_WRITE, FALSE, self->map_handle = OpenFileMappingA(FILE_MAP_WRITE, FALSE,
self->tagname); self->tagname);
if (self->map_handle) { if (self->map_handle) {
PyErr_SetFromWindowsErr(ERROR_USER_MAPPED_FILE); PyErr_SetFromWindowsErr(ERROR_USER_MAPPED_FILE);
@ -531,7 +531,7 @@ mmap_resize_method(mmap_object *self,
/* create a new file mapping and map a new view */ /* create a new file mapping and map a new view */
/* FIXME: call CreateFileMappingW with wchar_t tagname */ /* FIXME: call CreateFileMappingW with wchar_t tagname */
self->map_handle = CreateFileMapping( self->map_handle = CreateFileMappingA(
self->file_handle, self->file_handle,
NULL, NULL,
PAGE_READWRITE, PAGE_READWRITE,
@ -1514,12 +1514,12 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
off_lo = (DWORD)(offset & 0xFFFFFFFF); off_lo = (DWORD)(offset & 0xFFFFFFFF);
/* For files, it would be sufficient to pass 0 as size. /* For files, it would be sufficient to pass 0 as size.
For anonymous maps, we have to pass the size explicitly. */ For anonymous maps, we have to pass the size explicitly. */
m_obj->map_handle = CreateFileMapping(m_obj->file_handle, m_obj->map_handle = CreateFileMappingA(m_obj->file_handle,
NULL, NULL,
flProtect, flProtect,
size_hi, size_hi,
size_lo, size_lo,
m_obj->tagname); m_obj->tagname);
if (m_obj->map_handle != NULL) { if (m_obj->map_handle != NULL) {
m_obj->data = (char *) MapViewOfFile(m_obj->map_handle, m_obj->data = (char *) MapViewOfFile(m_obj->map_handle,
dwDesiredAccess, dwDesiredAccess,

View file

@ -10,16 +10,6 @@
#define PY_SSIZE_T_CLEAN #define PY_SSIZE_T_CLEAN
#include "Python.h" #include "Python.h"
// Include <windows.h> before pycore internal headers. FSCTL_GET_REPARSE_POINT
// is not exported by <windows.h> if the WIN32_LEAN_AND_MEAN macro is defined,
// whereas pycore_condvar.h defines the WIN32_LEAN_AND_MEAN macro.
#ifdef MS_WINDOWS
# include <windows.h>
# include <pathcch.h>
# include <lmcons.h> // UNLEN
# include "osdefs.h" // SEP
# define HAVE_SYMLINK
#endif
#ifdef __VXWORKS__ #ifdef __VXWORKS__
# include "pycore_bitutils.h" // _Py_popcount32() # include "pycore_bitutils.h" // _Py_popcount32()
@ -34,6 +24,15 @@
#include "pycore_pystate.h" // _PyInterpreterState_GET() #include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_signal.h" // Py_NSIG #include "pycore_signal.h" // Py_NSIG
#ifdef MS_WINDOWS
# include <windows.h>
# include <pathcch.h>
# include <winioctl.h>
# include <lmcons.h> // UNLEN
# include "osdefs.h" // SEP
# define HAVE_SYMLINK
#endif
#include "structmember.h" // PyMemberDef #include "structmember.h" // PyMemberDef
#ifndef MS_WINDOWS #ifndef MS_WINDOWS
# include "posixmodule.h" # include "posixmodule.h"
@ -1507,32 +1506,6 @@ error:
} }
#endif /* HAVE_SIGSET_T */ #endif /* HAVE_SIGSET_T */
#ifdef MS_WINDOWS
static int
win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
{
char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
_Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
DWORD n_bytes_returned;
if (0 == DeviceIoControl(
reparse_point_handle,
FSCTL_GET_REPARSE_POINT,
NULL, 0, /* in buffer */
target_buffer, sizeof(target_buffer),
&n_bytes_returned,
NULL)) /* we're not using OVERLAPPED_IO */
return FALSE;
if (reparse_tag)
*reparse_tag = rdb->ReparseTag;
return TRUE;
}
#endif /* MS_WINDOWS */
/* Return a dictionary corresponding to the POSIX environment table */ /* Return a dictionary corresponding to the POSIX environment table */
#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
/* On Darwin/MacOSX a shared library or framework has no access to /* On Darwin/MacOSX a shared library or framework has no access to
@ -8263,42 +8236,32 @@ os_setpgrp_impl(PyObject *module)
#ifdef HAVE_GETPPID #ifdef HAVE_GETPPID
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
#include <tlhelp32.h> #include <processsnapshot.h>
static PyObject* static PyObject*
win32_getppid() win32_getppid()
{ {
HANDLE snapshot; DWORD error;
PyObject* result = NULL; PyObject* result = NULL;
BOOL have_record; HANDLE process = GetCurrentProcess();
PROCESSENTRY32 pe;
DWORD mypid = GetCurrentProcessId(); /* This function never fails */ HPSS snapshot = NULL;
error = PssCaptureSnapshot(process, PSS_CAPTURE_NONE, 0, &snapshot);
snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (error != ERROR_SUCCESS) {
if (snapshot == INVALID_HANDLE_VALUE) return PyErr_SetFromWindowsErr(error);
return PyErr_SetFromWindowsErr(GetLastError());
pe.dwSize = sizeof(pe);
have_record = Process32First(snapshot, &pe);
while (have_record) {
if (mypid == pe.th32ProcessID) {
/* We could cache the ulong value in a static variable. */
result = PyLong_FromUnsignedLong(pe.th32ParentProcessID);
break;
}
have_record = Process32Next(snapshot, &pe);
} }
/* If our loop exits and our pid was not found (result will be NULL) PSS_PROCESS_INFORMATION info;
* then GetLastError will return ERROR_NO_MORE_FILES. This is an error = PssQuerySnapshot(snapshot, PSS_QUERY_PROCESS_INFORMATION, &info,
* error anyway, so let's raise it. */ sizeof(info));
if (!result) if (error == ERROR_SUCCESS) {
result = PyErr_SetFromWindowsErr(GetLastError()); result = PyLong_FromUnsignedLong(info.ParentProcessId);
}
CloseHandle(snapshot); else {
result = PyErr_SetFromWindowsErr(error);
}
PssFreeSnapshot(process, snapshot);
return result; return result;
} }
#endif /*MS_WINDOWS*/ #endif /*MS_WINDOWS*/
@ -15067,9 +15030,6 @@ error:
* on win32 * on win32
*/ */
typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
/*[clinic input] /*[clinic input]
os._add_dll_directory os._add_dll_directory
@ -15089,8 +15049,6 @@ static PyObject *
os__add_dll_directory_impl(PyObject *module, path_t *path) os__add_dll_directory_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
{ {
HMODULE hKernel32;
PAddDllDirectory AddDllDirectory;
DLL_DIRECTORY_COOKIE cookie = 0; DLL_DIRECTORY_COOKIE cookie = 0;
DWORD err = 0; DWORD err = 0;
@ -15098,14 +15056,8 @@ os__add_dll_directory_impl(PyObject *module, path_t *path)
return NULL; return NULL;
} }
/* For Windows 7, we have to load this. As this will be a fairly
infrequent operation, just do it each time. Kernel32 is always
loaded. */
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || if (!(cookie = AddDllDirectory(path->wide))) {
!(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
hKernel32, "AddDllDirectory")) ||
!(cookie = (*AddDllDirectory)(path->wide))) {
err = GetLastError(); err = GetLastError();
} }
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
@ -15134,8 +15086,6 @@ static PyObject *
os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
{ {
HMODULE hKernel32;
PRemoveDllDirectory RemoveDllDirectory;
DLL_DIRECTORY_COOKIE cookieValue; DLL_DIRECTORY_COOKIE cookieValue;
DWORD err = 0; DWORD err = 0;
@ -15148,14 +15098,8 @@ os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
cookie, "DLL directory cookie"); cookie, "DLL directory cookie");
/* For Windows 7, we have to load this. As this will be a fairly
infrequent operation, just do it each time. Kernel32 is always
loaded. */
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || if (!RemoveDllDirectory(cookieValue)) {
!(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
hKernel32, "RemoveDllDirectory")) ||
!(*RemoveDllDirectory)(cookieValue)) {
err = GetLastError(); err = GetLastError();
} }
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS

View file

@ -606,11 +606,6 @@ select_error(void)
# define SUPPRESS_DEPRECATED_CALL # define SUPPRESS_DEPRECATED_CALL
#endif #endif
#ifdef MS_WINDOWS
/* Does WSASocket() support the WSA_FLAG_NO_HANDLE_INHERIT flag? */
static int support_wsa_no_inherit = -1;
#endif
/* Convenience function to raise an error according to errno /* Convenience function to raise an error according to errno
and return a NULL pointer from a function. */ and return a NULL pointer from a function. */
@ -5342,6 +5337,13 @@ sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto,
set_error(); set_error();
return -1; return -1;
} }
if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) {
closesocket(fd);
PyErr_SetFromWindowsErr(0);
return -1;
}
family = info.iAddressFamily; family = info.iAddressFamily;
type = info.iSocketType; type = info.iSocketType;
proto = info.iProtocol; proto = info.iProtocol;
@ -5438,33 +5440,15 @@ sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto,
#endif #endif
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
if (support_wsa_no_inherit) { fd = WSASocketW(family, type, proto,
fd = WSASocketW(family, type, proto, NULL, 0,
NULL, 0, WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
if (fd == INVALID_SOCKET) {
/* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */
support_wsa_no_inherit = 0;
fd = socket(family, type, proto);
}
}
else {
fd = socket(family, type, proto);
}
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if (fd == INVALID_SOCKET) { if (fd == INVALID_SOCKET) {
set_error(); set_error();
return -1; return -1;
} }
if (!support_wsa_no_inherit) {
if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) {
closesocket(fd);
PyErr_SetFromWindowsErr(0);
return -1;
}
}
#else #else
/* UNIX */ /* UNIX */
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
@ -7340,12 +7324,6 @@ PyInit__socket(void)
if (!os_init()) if (!os_init())
return NULL; return NULL;
#ifdef MS_WINDOWS
if (support_wsa_no_inherit == -1) {
support_wsa_no_inherit = IsWindows7SP1OrGreater();
}
#endif
Py_SET_TYPE(&sock_type, &PyType_Type); Py_SET_TYPE(&sock_type, &PyType_Type);
m = PyModule_Create(&socketmodule); m = PyModule_Create(&socketmodule);
if (m == NULL) if (m == NULL)

View file

@ -1362,17 +1362,11 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
else else
flags = 0; flags = 0;
/* This check can be removed once support for Windows 7 ends. */ if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
#define CONSOLE_PSEUDOHANDLE(handle) (((ULONG_PTR)(handle) & 0x3) == 0x3 && \
GetFileType(handle) == FILE_TYPE_CHAR)
if (!CONSOLE_PSEUDOHANDLE(handle) &&
!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
if (raise) if (raise)
PyErr_SetFromWindowsErr(0); PyErr_SetFromWindowsErr(0);
return -1; return -1;
} }
#undef CONSOLE_PSEUDOHANDLE
return 0; return 0;
#else #else