]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-58689: Fix os.kill() error handling on Windows (GH-128932) (#128938)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 17 Jan 2025 12:48:24 +0000 (13:48 +0100)
committerGitHub <noreply@github.com>
Fri, 17 Jan 2025 12:48:24 +0000 (12:48 +0000)
gh-58689: Fix os.kill() error handling on Windows (GH-128932)
(cherry picked from commit 939df0f9f6a76e0916b3ba53841e1413ab90952e)

Co-authored-by: Victor Stinner <vstinner@python.org>
Modules/posixmodule.c

index deabc103056642a41dec7bd3ec7d57e47ecc05e9..c386f15b1714e1335e12573ceca7a78fefc098db 100644 (file)
@@ -8981,42 +8981,33 @@ os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
 
     Py_RETURN_NONE;
 #else /* !MS_WINDOWS */
-    PyObject *result;
     DWORD sig = (DWORD)signal;
-    DWORD err;
-    HANDLE handle;
 
 #ifdef HAVE_WINDOWS_CONSOLE_IO
     /* Console processes which share a common console can be sent CTRL+C or
        CTRL+BREAK events, provided they handle said events. */
     if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
         if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
-            err = GetLastError();
-            PyErr_SetFromWindowsErr(err);
-        }
-        else {
-            Py_RETURN_NONE;
+            return PyErr_SetFromWindowsErr(0);
         }
+        Py_RETURN_NONE;
     }
 #endif /* HAVE_WINDOWS_CONSOLE_IO */
 
     /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
        attempt to open and terminate the process. */
-    handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
+    HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
     if (handle == NULL) {
-        err = GetLastError();
-        return PyErr_SetFromWindowsErr(err);
+        return PyErr_SetFromWindowsErr(0);
     }
 
-    if (TerminateProcess(handle, sig) == 0) {
-        err = GetLastError();
-        result = PyErr_SetFromWindowsErr(err);
-    } else {
-        result = Py_NewRef(Py_None);
+    BOOL res = TerminateProcess(handle, sig);
+    CloseHandle(handle);
+    if (res == 0) {
+        return PyErr_SetFromWindowsErr(0);
     }
 
-    CloseHandle(handle);
-    return result;
+    Py_RETURN_NONE;
 #endif /* !MS_WINDOWS */
 }
 #endif /* HAVE_KILL */