Skip to content

Commit 1fa3890

Browse files
authored
gh-102141: replace use of getpid on Windows with GetCurrentProcessId (GH-102142)
1 parent 347f740 commit 1fa3890

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Use ``GetCurrentProcessId`` on Windows when ``getpid`` is unavailable. Patch by
2+
Max Bachmann.

Modules/_randommodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ random_seed_time_pid(RandomObject *self)
259259
key[0] = (uint32_t)(now & 0xffffffffU);
260260
key[1] = (uint32_t)(now >> 32);
261261

262-
#ifdef HAVE_GETPID
262+
#ifdef MS_WINDOWS_NON_DESKTOP
263+
key[2] = (uint32_t)GetCurrentProcessId();
264+
#elif defined(HAVE_GETPID)
263265
key[2] = (uint32_t)getpid();
264266
#else
265267
key[2] = 0;

Modules/posixmodule.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7946,7 +7946,7 @@ os_getgid_impl(PyObject *module)
79467946
#endif /* HAVE_GETGID */
79477947

79487948

7949-
#ifdef HAVE_GETPID
7949+
#if defined(HAVE_GETPID)
79507950
/*[clinic input]
79517951
os.getpid
79527952
@@ -7957,9 +7957,13 @@ static PyObject *
79577957
os_getpid_impl(PyObject *module)
79587958
/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
79597959
{
7960+
#ifdef MS_WINDOWS_NON_DESKTOP
7961+
return PyLong_FromUnsignedLong(GetCurrentProcessId());
7962+
#else
79607963
return PyLong_FromPid(getpid());
7964+
#endif
79617965
}
7962-
#endif /* HAVE_GETPID */
7966+
#endif /* defined(HAVE_GETPID) */
79637967

79647968
#ifdef NGROUPS_MAX
79657969
#define MAX_GROUPS NGROUPS_MAX
@@ -8265,12 +8269,11 @@ static PyObject*
82658269
win32_getppid()
82668270
{
82678271
HANDLE snapshot;
8268-
pid_t mypid;
82698272
PyObject* result = NULL;
82708273
BOOL have_record;
82718274
PROCESSENTRY32 pe;
82728275

8273-
mypid = getpid(); /* This function never fails */
8276+
DWORD mypid = GetCurrentProcessId(); /* This function never fails */
82748277

82758278
snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
82768279
if (snapshot == INVALID_HANDLE_VALUE)
@@ -8279,9 +8282,9 @@ win32_getppid()
82798282
pe.dwSize = sizeof(pe);
82808283
have_record = Process32First(snapshot, &pe);
82818284
while (have_record) {
8282-
if (mypid == (pid_t)pe.th32ProcessID) {
8285+
if (mypid == pe.th32ProcessID) {
82838286
/* We could cache the ulong value in a static variable. */
8284-
result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
8287+
result = PyLong_FromUnsignedLong(pe.th32ParentProcessID);
82858288
break;
82868289
}
82878290

PC/pyconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ WIN32 is still required for the locale module.
7272
#define USE_SOCKET
7373
#endif
7474

75+
#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP)
76+
#define MS_WINDOWS_NON_DESKTOP
77+
#endif
7578

7679
/* Compiler specific defines */
7780

0 commit comments

Comments
 (0)