8000 [3.13] gh-58689: Fix os.kill() error handling on Windows (GH-128932) … · python/cpython@d997ba4 · GitHub
[go: up one dir, main page]

Skip to content

Commit d997ba4

Browse files
[3.13] gh-58689: Fix os.kill() error handling on Windows (GH-128932) (#128937)
gh-58689: Fix os.kill() error handling on Windows (GH-128932) (cherry picked from commit 939df0f) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent c927fd9 commit d997ba4

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

Modules/posixmodule.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9533,42 +9533,33 @@ os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
95339533

95349534
Py_RETURN_NONE;
95359535
#else /* !MS_WINDOWS */
9536-
PyObject *result;
95379536
DWORD sig = (DWORD)signal;
9538-
DWORD err;
9539-
HANDLE handle;
95409537

95419538
#ifdef HAVE_WINDOWS_CONSOLE_IO
95429539
/* Console processes which share a common console can be sent CTRL+C or
95439540
CTRL+BREAK events, provided they handle said events. */
95449541
if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
95459542
if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
9546-
err = GetLastError();
9547-
PyErr_SetFromWindowsErr(err);
9548-
}
9549-
else {
9550-
Py_RETURN_NONE;
9543+
return PyErr_SetFromWindowsErr(0);
95519544
}
9545+
Py_RETURN_NONE;
95529546
}
95539547
#endif /* HAVE_WINDOWS_CONSOLE_IO */
95549548

95559549
/* If the signal is outside of what GenerateConsoleCtrlEvent can use,
95569550
attempt to open and terminate the process. */
9557-
handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
9551+
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
95589552
if (handle == NULL) {
9559-
err = GetLastError();
9560-
return PyErr_SetFromWindowsErr(err);
9553+
return PyErr_SetFromWindowsErr(0);
95619554
}
95629555

9563-
if (TerminateProcess(handle, sig) == 0) {
9564-
err = GetLastError();
9565-
result = PyErr_SetFromWindowsErr(err);
9566-
} else {
9567-
result = Py_NewRef(Py_None);
9556+
BOOL res = TerminateProcess(handle, sig);
9557+
CloseHandle(handle);
9558+
if (res == 0) {
9559+
return PyErr_SetFromWindowsErr(0);
95689560
}
95699561

9570-
CloseHandle(handle);
9571-
return result;
9562+
Py_RETURN_NONE;
95729563
#endif /* !MS_WINDOWS */
95739564
}
95749565
#endif /* HAVE_KILL */

0 commit comments

Comments
 (0)
0