]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-102141: replace use of getpid on Windows with GetCurrentProcessId (GH-102142)
authorMax Bachmann <kontakt@maxbachmann.de>
Fri, 24 Feb 2023 12:38:21 +0000 (13:38 +0100)
committerGitHub <noreply@github.com>
Fri, 24 Feb 2023 12:38:21 +0000 (12:38 +0000)
Misc/NEWS.d/next/Core and Builtins/2023-02-22-15-15-32.gh-issue-102027.Km4G-d.rst [new file with mode: 0644]
Modules/_randommodule.c
Modules/posixmodule.c
PC/pyconfig.h

diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-02-22-15-15-32.gh-issue-102027.Km4G-d.rst b/Misc/NEWS.d/next/Core and Builtins/2023-02-22-15-15-32.gh-issue-102027.Km4G-d.rst
new file mode 100644 (file)
index 0000000..514a8ef
--- /dev/null
@@ -0,0 +1,2 @@
+Use ``GetCurrentProcessId`` on Windows when ``getpid`` is unavailable. Patch by
+Max Bachmann.
index 95f1e505dd187304fe5d39f2d328b38ac500f158..68060c07033d345f314a9053b0a435aa309c7aef 100644 (file)
@@ -259,7 +259,9 @@ random_seed_time_pid(RandomObject *self)
     key[0] = (uint32_t)(now & 0xffffffffU);
     key[1] = (uint32_t)(now >> 32);
 
-#ifdef HAVE_GETPID
+#ifdef MS_WINDOWS_NON_DESKTOP
+    key[2] = (uint32_t)GetCurrentProcessId();
+#elif defined(HAVE_GETPID)
     key[2] = (uint32_t)getpid();
 #else
     key[2] = 0;
index 524dc7eb1ccc97cd91ebf2e05443b67a2607227a..51aa89ef715a19715d508fe8c8fc5d343f3c59d4 100644 (file)
@@ -7946,7 +7946,7 @@ os_getgid_impl(PyObject *module)
 #endif /* HAVE_GETGID */
 
 
-#ifdef HAVE_GETPID
+#if defined(HAVE_GETPID)
 /*[clinic input]
 os.getpid
 
@@ -7957,9 +7957,13 @@ static PyObject *
 os_getpid_impl(PyObject *module)
 /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
 {
+#ifdef MS_WINDOWS_NON_DESKTOP
+    return PyLong_FromUnsignedLong(GetCurrentProcessId());
+#else
     return PyLong_FromPid(getpid());
+#endif
 }
-#endif /* HAVE_GETPID */
+#endif /* defined(HAVE_GETPID) */
 
 #ifdef NGROUPS_MAX
 #define MAX_GROUPS NGROUPS_MAX
@@ -8265,12 +8269,11 @@ static PyObject*
 win32_getppid()
 {
     HANDLE snapshot;
-    pid_t mypid;
     PyObject* result = NULL;
     BOOL have_record;
     PROCESSENTRY32 pe;
 
-    mypid = getpid(); /* This function never fails */
+    DWORD mypid = GetCurrentProcessId(); /* This function never fails */
 
     snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
     if (snapshot == INVALID_HANDLE_VALUE)
@@ -8279,9 +8282,9 @@ win32_getppid()
     pe.dwSize = sizeof(pe);
     have_record = Process32First(snapshot, &pe);
     while (have_record) {
-        if (mypid == (pid_t)pe.th32ProcessID) {
+        if (mypid == pe.th32ProcessID) {
             /* We could cache the ulong value in a static variable. */
-            result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
+            result = PyLong_FromUnsignedLong(pe.th32ParentProcessID);
             break;
         }
 
index f5166a1506c945ec005f867fe8cf7a3c41e08a4f..a34d420ab7ecaab1419be2ce93ad734757945ef9 100644 (file)
@@ -72,6 +72,9 @@ WIN32 is still required for the locale module.
 #define USE_SOCKET
 #endif
 
+#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP)
+#define MS_WINDOWS_NON_DESKTOP
+#endif
 
 /* Compiler specific defines */