1010#define PY_SSIZE_T_CLEAN
1111
1212#include "Python.h"
13- // Include <windows.h> before pycore internal headers. FSCTL_GET_REPARSE_POINT
14- // is not exported by <windows.h> if the WIN32_LEAN_AND_MEAN macro is defined,
15- // whereas pycore_condvar.h defines the WIN32_LEAN_AND_MEAN macro.
16- #ifdef MS_WINDOWS
17- # include <windows.h>
18- # include <pathcch.h>
19- # include <lmcons.h> // UNLEN
20- # include "osdefs.h" // SEP
21- # define HAVE_SYMLINK
22- #endif
2313
2414#ifdef __VXWORKS__
2515# include "pycore_bitutils.h" // _Py_popcount32()
3424#include "pycore_pystate.h" // _PyInterpreterState_GET()
3525#include "pycore_signal.h" // Py_NSIG
3626
27+ #ifdef MS_WINDOWS
28+ # include <windows.h>
29+ # include <pathcch.h>
30+ # include <winioctl.h>
31+ # include <lmcons.h> // UNLEN
32+ # include "osdefs.h" // SEP
33+ # define HAVE_SYMLINK
34+ #endif
35+
3736#include "structmember.h" // PyMemberDef
3837#ifndef MS_WINDOWS
3938# include "posixmodule.h"
@@ -1507,32 +1506,6 @@ _Py_Sigset_Converter(PyObject *obj, void *addr)
15071506}
15081507#endif /* HAVE_SIGSET_T */
15091508
1510- #ifdef MS_WINDOWS
1511-
1512- static int
1513- win32_get_reparse_tag (HANDLE reparse_point_handle , ULONG * reparse_tag )
1514- {
1515- char target_buffer [_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE ];
1516- _Py_REPARSE_DATA_BUFFER * rdb = (_Py_REPARSE_DATA_BUFFER * )target_buffer ;
1517- DWORD n_bytes_returned ;
1518-
1519- if (0 == DeviceIoControl (
1520- reparse_point_handle ,
1521- FSCTL_GET_REPARSE_POINT ,
1522- NULL , 0 , /* in buffer */
1523- target_buffer , sizeof (target_buffer ),
1524- & n_bytes_returned ,
1525- NULL )) /* we're not using OVERLAPPED_IO */
1526- return FALSE;
1527-
1528- if (reparse_tag )
1529- * reparse_tag = rdb -> ReparseTag ;
1530-
1531- return TRUE;
1532- }
1533-
1534- #endif /* MS_WINDOWS */
1535-
15361509/* Return a dictionary corresponding to the POSIX environment table */
15371510#if defined(WITH_NEXT_FRAMEWORK ) || (defined(__APPLE__ ) && defined(Py_ENABLE_SHARED ))
15381511/* On Darwin/MacOSX a shared library or framework has no access to
@@ -8263,42 +8236,32 @@ os_setpgrp_impl(PyObject *module)
82638236#ifdef HAVE_GETPPID
82648237
82658238#ifdef MS_WINDOWS
8266- #include <tlhelp32 .h>
8239+ #include <processsnapshot .h>
82678240
82688241static PyObject *
82698242win32_getppid ()
82708243{
8271- HANDLE snapshot ;
8244+ DWORD error ;
82728245 PyObject * result = NULL ;
8273- BOOL have_record ;
8274- PROCESSENTRY32 pe ;
8275-
8276- DWORD mypid = GetCurrentProcessId (); /* This function never fails */
8277-
8278- snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS , 0 );
8279- if (snapshot == INVALID_HANDLE_VALUE )
8280- return PyErr_SetFromWindowsErr (GetLastError ());
8246+ HANDLE process = GetCurrentProcess ();
82818247
8282- pe .dwSize = sizeof (pe );
8283- have_record = Process32First (snapshot , & pe );
8284- while (have_record ) {
8285- if (mypid == pe .th32ProcessID ) {
8286- /* We could cache the ulong value in a static variable. */
8287- result = PyLong_FromUnsignedLong (pe .th32ParentProcessID );
8288- break ;
8289- }
8290-
8291- have_record = Process32Next (snapshot , & pe );
8248+ HPSS snapshot = NULL ;
8249+ error = PssCaptureSnapshot (process , PSS_CAPTURE_NONE , 0 , & snapshot );
8250+ if (error != ERROR_SUCCESS ) {
8251+ return PyErr_SetFromWindowsErr (error );
82928252 }
82938253
8294- /* If our loop exits and our pid was not found (result will be NULL)
8295- * then GetLastError will return ERROR_NO_MORE_FILES. This is an
8296- * error anyway, so let's raise it. */
8297- if (!result )
8298- result = PyErr_SetFromWindowsErr (GetLastError ());
8299-
8300- CloseHandle (snapshot );
8254+ PSS_PROCESS_INFORMATION info ;
8255+ error = PssQuerySnapshot (snapshot , PSS_QUERY_PROCESS_INFORMATION , & info ,
8256+ sizeof (info ));
8257+ if (error == ERROR_SUCCESS ) {
8258+ result = PyLong_FromUnsignedLong (info .ParentProcessId );
8259+ }
8260+ else {
8261+ result = PyErr_SetFromWindowsErr (error );
8262+ }
83018263
8264+ PssFreeSnapshot (process , snapshot );
83028265 return result ;
83038266}
83048267#endif /*MS_WINDOWS*/
@@ -15067,9 +15030,6 @@ os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
1506715030 * on win32
1506815031 */
1506915032
15070- typedef DLL_DIRECTORY_COOKIE (WINAPI * PAddDllDirectory )(PCWSTR newDirectory );
15071- typedef BOOL (WINAPI * PRemoveDllDirectory )(DLL_DIRECTORY_COOKIE cookie );
15072-
1507315033/*[clinic input]
1507415034os._add_dll_directory
1507515035
@@ -15089,23 +15049,15 @@ static PyObject *
1508915049os__add_dll_directory_impl (PyObject * module , path_t * path )
1509015050/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
1509115051{
15092- HMODULE hKernel32 ;
15093- PAddDllDirectory AddDllDirectory ;
1509415052 DLL_DIRECTORY_COOKIE cookie = 0 ;
1509515053 DWORD err = 0 ;
1509615054
1509715055 if (PySys_Audit ("os.add_dll_directory" , "(O)" , path -> object ) < 0 ) {
1509815056 return NULL ;
1509915057 }
1510015058
15101- /* For Windows 7, we have to load this. As this will be a fairly
15102- infrequent operation, just do it each time. Kernel32 is always
15103- loaded. */
1510415059 Py_BEGIN_ALLOW_THREADS
15105- if (!(hKernel32 = GetModuleHandleW (L"kernel32" )) ||
15106- !(AddDllDirectory = (PAddDllDirectory )GetProcAddress (
15107- hKernel32 , "AddDllDirectory" )) ||
15108- !(cookie = (* AddDllDirectory )(path -> wide ))) {
15060+ if (!(cookie = AddDllDirectory (path -> wide ))) {
1510915061 err = GetLastError ();
1511015062 }
1511115063 Py_END_ALLOW_THREADS
@@ -15134,8 +15086,6 @@ static PyObject *
1513415086os__remove_dll_directory_impl (PyObject * module , PyObject * cookie )
1513515087/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
1513615088{
15137- HMODULE hKernel32 ;
15138- PRemoveDllDirectory RemoveDllDirectory ;
1513915089 DLL_DIRECTORY_COOKIE cookieValue ;
1514015090 DWORD err = 0 ;
1514115091
@@ -15148,14 +15098,8 @@ os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
1514815098 cookieValue = (DLL_DIRECTORY_COOKIE )PyCapsule_GetPointer (
1514915099 cookie , "DLL directory cookie" );
1515015100
15151- /* For Windows 7, we have to load this. As this will be a fairly
15152- infrequent operation, just do it each time. Kernel32 is always
15153- loaded. */
1515415101 Py_BEGIN_ALLOW_THREADS
15155- if (!(hKernel32 = GetModuleHandleW (L"kernel32" )) ||
15156- !(RemoveDllDirectory = (PRemoveDllDirectory )GetProcAddress (
15157- hKernel32 , "RemoveDllDirectory" )) ||
15158- !(* RemoveDllDirectory )(cookieValue )) {
15102+ if (!RemoveDllDirectory (cookieValue )) {
1515915103 err = GetLastError ();
1516015104 }
1516115105 Py_END_ALLOW_THREADS
0 commit comments