diff options
author | Brian Curtin <brian.curtin@gmail.com> | 2010-04-12 17:16:38 (GMT) |
---|---|---|
committer | Brian Curtin <brian.curtin@gmail.com> | 2010-04-12 17:16:38 (GMT) |
commit | eb24d7498f3e34586fee24209f5630a58bb1a04b (patch) | |
tree | 2618500362c3b75e9ff541971954b57d14d66a86 /Modules/posixmodule.c | |
parent | b2416e54b15d90b4a1bc917897912061830b42fc (diff) | |
download | cpython-eb24d7498f3e34586fee24209f5630a58bb1a04b.zip cpython-eb24d7498f3e34586fee24209f5630a58bb1a04b.tar.gz cpython-eb24d7498f3e34586fee24209f5630a58bb1a04b.tar.bz2 |
Port #1220212 (os.kill for Win32) to py3k.
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r-- | Modules/posixmodule.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 5076d3b..e6ef410 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -4171,6 +4171,53 @@ posix_killpg(PyObject *self, PyObject *args) } #endif +#ifdef MS_WINDOWS +PyDoc_STRVAR(win32_kill__doc__, +"kill(pid, sig)\n\n\ +Kill a process with a signal."); + +static PyObject * +win32_kill(PyObject *self, PyObject *args) +{ + PyObject *result, handle_obj; + DWORD pid, sig, err; + HANDLE handle; + + if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) + return NULL; + + /* 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, pid) == 0) { + err = GetLastError(); + PyErr_SetFromWindowsErr(err); + } + else + Py_RETURN_NONE; + } + + /* If the signal is outside of what GenerateConsoleCtrlEvent can use, + attempt to open and terminate the process. */ + handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); + if (handle == NULL) { + err = GetLastError(); + return PyErr_SetFromWindowsErr(err); + } + + if (TerminateProcess(handle, sig) == 0) { + err = GetLastError(); + result = PyErr_SetFromWindowsErr(err); + } else { + Py_INCREF(Py_None); + result = Py_None; + } + + CloseHandle(handle); + return result; +} +#endif /* MS_WINDOWS */ + #ifdef HAVE_PLOCK #ifdef HAVE_SYS_LOCK_H @@ -7200,6 +7247,7 @@ static PyMethodDef posix_methods[] = { #endif /* HAVE_PLOCK */ #ifdef MS_WINDOWS {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, + {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, #endif #ifdef HAVE_SETUID {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |