Port #1220212 (os.kill for Win32) to py3k.

This commit is contained in:
Brian Curtin 2010-04-12 17:16:38 +00:00
parent b2416e54b1
commit eb24d7498f
10 changed files with 200 additions and 6 deletions

View file

@ -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__},

View file

@ -7,6 +7,7 @@
#include "intrcheck.h"
#ifdef MS_WINDOWS
#include <Windows.h>
#ifdef HAVE_PROCESS_H
#include <process.h>
#endif
@ -805,6 +806,18 @@ PyInit_signal(void)
PyDict_SetItemString(d, "ItimerError", ItimerError);
#endif
#ifdef CTRL_C_EVENT
x = PyLong_FromLong(CTRL_C_EVENT);
PyDict_SetItemString(d, "CTRL_C_EVENT", x);
Py_DECREF(x);
#endif
#ifdef CTRL_BREAK_EVENT
x = PyLong_FromLong(CTRL_BREAK_EVENT);
PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
Py_DECREF(x);
#endif
if (PyErr_Occurred()) {
Py_DECREF(m);
m = NULL;