mirror of
https://github.com/python/cpython.git
synced 2025-09-14 12:46:49 +00:00
[3.12] gh-58689: Fix os.kill() error handling on Windows (GH-128932) (#128938)
gh-58689: Fix os.kill() error handling on Windows (GH-128932)
(cherry picked from commit 939df0f9f6
)
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
1c85f1bc44
commit
dfd75dfeed
1 changed files with 10 additions and 19 deletions
|
@ -8981,42 +8981,33 @@ os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
|
||||||
|
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
#else /* !MS_WINDOWS */
|
#else /* !MS_WINDOWS */
|
||||||
PyObject *result;
|
|
||||||
DWORD sig = (DWORD)signal;
|
DWORD sig = (DWORD)signal;
|
||||||
DWORD err;
|
|
||||||
HANDLE handle;
|
|
||||||
|
|
||||||
#ifdef HAVE_WINDOWS_CONSOLE_IO
|
#ifdef HAVE_WINDOWS_CONSOLE_IO
|
||||||
/* Console processes which share a common console can be sent CTRL+C or
|
/* Console processes which share a common console can be sent CTRL+C or
|
||||||
CTRL+BREAK events, provided they handle said events. */
|
CTRL+BREAK events, provided they handle said events. */
|
||||||
if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
|
if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
|
||||||
if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
|
if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
|
||||||
err = GetLastError();
|
return PyErr_SetFromWindowsErr(0);
|
||||||
PyErr_SetFromWindowsErr(err);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
}
|
||||||
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
#endif /* HAVE_WINDOWS_CONSOLE_IO */
|
#endif /* HAVE_WINDOWS_CONSOLE_IO */
|
||||||
|
|
||||||
/* If the signal is outside of what GenerateConsoleCtrlEvent can use,
|
/* If the signal is outside of what GenerateConsoleCtrlEvent can use,
|
||||||
attempt to open and terminate the process. */
|
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) {
|
if (handle == NULL) {
|
||||||
err = GetLastError();
|
return PyErr_SetFromWindowsErr(0);
|
||||||
return PyErr_SetFromWindowsErr(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (TerminateProcess(handle, sig) == 0) {
|
|
||||||
err = GetLastError();
|
|
||||||
result = PyErr_SetFromWindowsErr(err);
|
|
||||||
} else {
|
|
||||||
result = Py_NewRef(Py_None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOL res = TerminateProcess(handle, sig);
|
||||||
CloseHandle(handle);
|
CloseHandle(handle);
|
||||||
return result;
|
if (res == 0) {
|
||||||
|
return PyErr_SetFromWindowsErr(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Py_RETURN_NONE;
|
||||||
#endif /* !MS_WINDOWS */
|
#endif /* !MS_WINDOWS */
|
||||||
}
|
}
|
||||||
#endif /* HAVE_KILL */
|
#endif /* HAVE_KILL */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue